|
1 | | -from rasa_sdk import Action |
| 1 | +from rasa_sdk import Action, Tracker |
2 | 2 | from rasa_sdk.events import UserUtteranceReverted, SlotSet |
3 | 3 | import requests |
4 | 4 | import json |
|
12 | 12 | import sys |
13 | 13 | from whatsapp_connector import WhatsAppOutput |
14 | 14 | import pytz |
15 | | -from typing import Any, Text, Dict, List |
| 15 | +from typing import Any, Text, Dict, List, Optional |
16 | 16 | sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) |
17 | 17 | logging.basicConfig(level=logging.DEBUG) # Força o nível global de debug |
18 | 18 | logger = logging.getLogger(__name__) |
19 | 19 | logger.setLevel(logging.DEBUG) |
20 | 20 | from .utils import * |
21 | 21 | import re |
| 22 | +from rasa_sdk.types import DomainDict |
22 | 23 |
|
23 | 24 | class ActionFallbackButtons(Action): |
24 | 25 | def name(self): |
@@ -83,13 +84,13 @@ def run(self, dispatcher, tracker, domain): |
83 | 84 | return [ |
84 | 85 | FollowupAction("action_listen") |
85 | 86 | ] |
86 | | - if last_action == "utter_menu_dicas": |
87 | | - logger.debug(f"Fallback de menu_dicas") |
88 | | - logger.debug(f"user_message: {user_message}") |
89 | | - dispatcher.utter_message(text="Não consegui entender. Por favor aperte um dos botões.") |
90 | | - return [ |
91 | | - FollowupAction("utter_menu_dicas") |
92 | | - ] |
| 87 | + # if last_action == "utter_menu_dicas": |
| 88 | + # logger.debug(f"Fallback de menu_dicas") |
| 89 | + # logger.debug(f"user_message: {user_message}") |
| 90 | + # dispatcher.utter_message(text="Não consegui entender. Por favor aperte um dos botões.") |
| 91 | + # return [ |
| 92 | + # FollowupAction("utter_menu_dicas") |
| 93 | + # ] |
93 | 94 | # Caso contrário, volta ao fallback padrão |
94 | 95 | last_bot_message = None |
95 | 96 | for event in reversed(tracker.events): |
@@ -883,6 +884,62 @@ def run(self, dispatcher, tracker, domain): |
883 | 884 |
|
884 | 885 | return [] |
885 | 886 |
|
| 887 | + |
| 888 | + |
| 889 | + |
| 890 | +class ActionPrecisoDeAjuda(Action): |
| 891 | + def name(self) -> Text: |
| 892 | + return "action_preciso_de_ajuda" |
| 893 | + |
| 894 | + def _formata_itens(self, itens: List[Any]) -> Optional[str]: |
| 895 | + bullets = [str(x).strip() for x in itens if str(x).strip()] |
| 896 | + if not bullets: |
| 897 | + return None |
| 898 | + return "Recomendamos que você:\n \n" + "".join(f"• {b}\n" for b in bullets) + " \n⚠️ Em caso de urgência, ligue para a *Defesa Civil: 199*." |
| 899 | + |
| 900 | + def run( |
| 901 | + self, |
| 902 | + dispatcher: CollectingDispatcher, |
| 903 | + tracker: Tracker, |
| 904 | + domain: DomainDict, |
| 905 | + ) -> List[Dict[Text, Any]]: |
| 906 | + logger.debug("rodando action: action_preciso_de_ajuda") |
| 907 | + |
| 908 | + FALLBACK_TEXTO = ( |
| 909 | + "Recomendamos que você:\n\n" |
| 910 | + "• Separe agora mesmo um kit de sobrevivência com documentos, remédios e itens essenciais.\n\n" |
| 911 | + "• Tenha cuidado com fios caídos ou locais com risco de choque elétrico.\n\n" |
| 912 | + "• Evite contato com a água de enchentes, pois pode estar contaminada.\n\n" |
| 913 | + "⚠️ Em caso de urgência, ligue para a *Defesa Civil: 199*." |
| 914 | + ) |
| 915 | + wordpress_url = os.getenv("WORDPRESS_URL") |
| 916 | + endpoint = f"{wordpress_url}/wp-json/dcp/v1/dicas?active=1" |
| 917 | + |
| 918 | + texto_final: Optional[str] = None |
| 919 | + |
| 920 | + if endpoint: |
| 921 | + try: |
| 922 | + # aceita JSON ou texto puro |
| 923 | + headers = {"Accept": "application/json, text/plain;q=0.5"} |
| 924 | + resp = requests.get(endpoint, headers=headers, timeout=8) |
| 925 | + resp.raise_for_status() |
| 926 | + |
| 927 | + ctype = resp.headers.get("content-type", "") |
| 928 | + if "application/json" in ctype: |
| 929 | + data = resp.json() |
| 930 | + recomendacoes = data[0].get("recomendacoes") |
| 931 | + if recomendacoes: |
| 932 | + texto_final = self._formata_itens(recomendacoes) |
| 933 | + |
| 934 | + except Exception as e: |
| 935 | + logger.error(f"[action_preciso_de_ajuda] erro consultando {endpoint}: {e}") |
| 936 | + |
| 937 | + if not texto_final: |
| 938 | + texto_final = FALLBACK_TEXTO |
| 939 | + |
| 940 | + dispatcher.utter_message(text=texto_final) |
| 941 | + return [] |
| 942 | + |
886 | 943 | class ActionListarAbrigos(Action): |
887 | 944 | def name(self): |
888 | 945 | return "action_listar_abrigos" |
@@ -957,44 +1014,43 @@ def run(self, dispatcher, tracker, domain): |
957 | 1014 |
|
958 | 1015 | return [] |
959 | 1016 |
|
960 | | -class ActionBuscarDicas(Action): |
961 | | - def name(self): |
962 | | - return "action_buscar_dicas" |
963 | | - |
964 | | - def run(self, dispatcher, tracker, domain): |
965 | | - logger.debug("rodando action: action_buscar_dicas") |
966 | | - tipo_dica = tracker.get_slot("dicas") |
967 | | - if not tipo_dica: |
968 | | - dispatcher.utter_message(text="Desculpe, não entendi o tipo de dica que você deseja. Por favor aperte em um dos botões.") |
969 | | - return [] |
970 | | - |
971 | | - wordpress_url = os.getenv("WORDPRESS_URL") |
972 | | - if not wordpress_url: |
973 | | - dispatcher.utter_message(text="Erro: URL do WordPress não configurada.") |
974 | | - return [] |
975 | | - |
976 | | - endpoint = f"{wordpress_url}/wp-json/dcp/v1/dicas?tipo={tipo_dica}" |
977 | | - try: |
978 | | - response = requests.get(endpoint, timeout=5) |
979 | | - response.raise_for_status() |
980 | | - dicas = response.json() |
981 | | - |
982 | | - if not dicas: |
983 | | - dispatcher.utter_message(text=f"Não encontrei dicas para '{tipo_dica}'.") |
984 | | - return [] |
985 | | - |
986 | | - mensagem = f"Dicas para {tipo_dica}:\n" |
987 | | - for dica in dicas: |
988 | | - mensagem += f"- {dica}\n" |
989 | | - |
990 | | - dispatcher.utter_message(text=mensagem) |
991 | | - except requests.RequestException as e: |
992 | | - dispatcher.utter_message(text="Desculpe, ocorreu um erro ao buscar as dicas.") |
993 | | - # Aqui você pode adicionar um log do erro, se desejar |
994 | | - return [] |
| 1017 | +# class ActionBuscarDicas(Action): |
| 1018 | +# def name(self): |
| 1019 | +# return "action_buscar_dicas" |
| 1020 | + |
| 1021 | +# def run(self, dispatcher, tracker, domain): |
| 1022 | +# logger.debug("rodando action: action_buscar_dicas") |
| 1023 | +# tipo_dica = tracker.get_slot("dicas") |
| 1024 | +# if not tipo_dica: |
| 1025 | +# dispatcher.utter_message(text="Desculpe, não entendi o tipo de dica que você deseja. Por favor aperte em um dos botões.") |
| 1026 | +# return [] |
| 1027 | + |
| 1028 | +# wordpress_url = os.getenv("WORDPRESS_URL") |
| 1029 | +# if not wordpress_url: |
| 1030 | +# dispatcher.utter_message(text="Erro: URL do WordPress não configurada.") |
| 1031 | +# return [] |
| 1032 | + |
| 1033 | +# endpoint = f"{wordpress_url}/wp-json/dcp/v1/dicas?tipo={tipo_dica}" |
| 1034 | +# try: |
| 1035 | +# response = requests.get(endpoint, timeout=5) |
| 1036 | +# response.raise_for_status() |
| 1037 | +# dicas = response.json() |
| 1038 | + |
| 1039 | +# if not dicas: |
| 1040 | +# dispatcher.utter_message(text=f"Não encontrei dicas para '{tipo_dica}'.") |
| 1041 | +# return [] |
| 1042 | + |
| 1043 | +# mensagem = f"Dicas para {tipo_dica}:\n" |
| 1044 | +# for dica in dicas: |
| 1045 | +# mensagem += f"- {dica}\n" |
| 1046 | + |
| 1047 | +# dispatcher.utter_message(text=mensagem) |
| 1048 | +# except requests.RequestException as e: |
| 1049 | +# dispatcher.utter_message(text="Desculpe, ocorreu um erro ao buscar as dicas.") |
| 1050 | +# # Aqui você pode adicionar um log do erro, se desejar |
| 1051 | +# return [] |
995 | 1052 |
|
996 | 1053 |
|
997 | | - from rasa_sdk import Action |
998 | 1054 |
|
999 | 1055 | class ActionPerguntaNotificacoes(Action): |
1000 | 1056 | def name(self) -> str: |
|
0 commit comments