chatgraph 0.4.2__tar.gz → 0.5.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of chatgraph might be problematic. Click here for more details.
- {chatgraph-0.4.2 → chatgraph-0.5.1}/PKG-INFO +1 -1
- {chatgraph-0.4.2 → chatgraph-0.5.1}/chatgraph/__init__.py +5 -3
- {chatgraph-0.4.2 → chatgraph-0.5.1}/chatgraph/bot/chatbot_model.py +70 -76
- chatgraph-0.5.1/chatgraph/gRPC/gRPCCall.py +137 -0
- chatgraph-0.5.1/chatgraph/messages/message_consumer.py +150 -0
- chatgraph-0.5.1/chatgraph/pb/router.proto +127 -0
- chatgraph-0.5.1/chatgraph/pb/router_pb2.py +77 -0
- chatgraph-0.5.1/chatgraph/pb/router_pb2_grpc.py +669 -0
- chatgraph-0.5.1/chatgraph/types/background_task.py +23 -0
- chatgraph-0.5.1/chatgraph/types/message_types.py +111 -0
- chatgraph-0.5.1/chatgraph/types/request_types.py +294 -0
- {chatgraph-0.4.2 → chatgraph-0.5.1}/pyproject.toml +1 -1
- chatgraph-0.4.2/chatgraph/gRPC/gRPCCall.py +0 -210
- chatgraph-0.4.2/chatgraph/messages/message_consumer.py +0 -215
- chatgraph-0.4.2/chatgraph/pb/userstate.proto +0 -41
- chatgraph-0.4.2/chatgraph/pb/userstate_pb2.py +0 -47
- chatgraph-0.4.2/chatgraph/pb/userstate_pb2_grpc.py +0 -269
- chatgraph-0.4.2/chatgraph/pb/voll.proto +0 -90
- chatgraph-0.4.2/chatgraph/pb/voll_pb2.py +0 -63
- chatgraph-0.4.2/chatgraph/pb/voll_pb2_grpc.py +0 -470
- chatgraph-0.4.2/chatgraph/types/message_types.py +0 -108
- chatgraph-0.4.2/chatgraph/types/request_types.py +0 -369
- {chatgraph-0.4.2 → chatgraph-0.5.1}/LICENSE +0 -0
- {chatgraph-0.4.2 → chatgraph-0.5.1}/README.md +0 -0
- {chatgraph-0.4.2 → chatgraph-0.5.1}/chatgraph/auth/credentials.py +0 -0
- {chatgraph-0.4.2 → chatgraph-0.5.1}/chatgraph/bot/chatbot_router.py +0 -0
- {chatgraph-0.4.2 → chatgraph-0.5.1}/chatgraph/cli/__init__.py +0 -0
- {chatgraph-0.4.2 → chatgraph-0.5.1}/chatgraph/error/chatbot_error.py +0 -0
- {chatgraph-0.4.2 → chatgraph-0.5.1}/chatgraph/error/route_error.py +0 -0
- {chatgraph-0.4.2 → chatgraph-0.5.1}/chatgraph/types/end_types.py +0 -0
- {chatgraph-0.4.2 → chatgraph-0.5.1}/chatgraph/types/route.py +0 -0
|
@@ -2,10 +2,11 @@ from .auth.credentials import Credential
|
|
|
2
2
|
from .bot.chatbot_model import ChatbotApp
|
|
3
3
|
from .bot.chatbot_router import ChatbotRouter
|
|
4
4
|
from .messages.message_consumer import MessageConsumer
|
|
5
|
-
from .types.request_types import UserCall, UserState
|
|
5
|
+
from .types.request_types import UserCall, UserState, ChatID
|
|
6
6
|
from .types.end_types import RedirectResponse, EndChatResponse, TransferToHuman
|
|
7
|
-
from .types.message_types import Message, Button
|
|
7
|
+
from .types.message_types import Message, Button
|
|
8
8
|
from .types.route import Route
|
|
9
|
+
from .types.background_task import BackgroundTask
|
|
9
10
|
|
|
10
11
|
__all__ = [
|
|
11
12
|
'ChatbotApp',
|
|
@@ -18,8 +19,9 @@ __all__ = [
|
|
|
18
19
|
'Route',
|
|
19
20
|
'EndChatResponse',
|
|
20
21
|
'TransferToHuman',
|
|
22
|
+
'ChatID',
|
|
21
23
|
'UserState',
|
|
22
24
|
'Message',
|
|
23
25
|
'Button',
|
|
24
|
-
'
|
|
26
|
+
'BackgroundTask',
|
|
25
27
|
]
|
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import inspect
|
|
2
2
|
from functools import wraps
|
|
3
|
-
from logging import debug
|
|
4
|
-
import
|
|
5
|
-
from logging import error
|
|
3
|
+
from logging import debug, error
|
|
4
|
+
import asyncio
|
|
6
5
|
|
|
7
|
-
from ..error.chatbot_error import
|
|
6
|
+
from ..error.chatbot_error import ChatbotMessageError
|
|
8
7
|
from ..messages.message_consumer import MessageConsumer
|
|
9
8
|
from ..types.request_types import UserCall
|
|
10
|
-
from ..types.message_types import
|
|
9
|
+
from ..types.message_types import Message, Button
|
|
11
10
|
from ..types.end_types import RedirectResponse, EndChatResponse, TransferToHuman
|
|
12
11
|
from ..types.route import Route
|
|
13
12
|
from .chatbot_router import ChatbotRouter
|
|
@@ -27,20 +26,16 @@ class ChatbotApp:
|
|
|
27
26
|
"""
|
|
28
27
|
if not message_consumer:
|
|
29
28
|
message_consumer = MessageConsumer.load_dotenv()
|
|
30
|
-
|
|
29
|
+
|
|
31
30
|
self.__message_consumer = message_consumer
|
|
32
31
|
self.__routes = {}
|
|
33
|
-
|
|
32
|
+
|
|
34
33
|
def include_router(self, router: ChatbotRouter):
|
|
35
34
|
"""
|
|
36
35
|
Inclui um roteador de chatbot com um prefixo nas rotas da aplicação.
|
|
37
36
|
|
|
38
37
|
Args:
|
|
39
38
|
router (ChatbotRouter): O roteador contendo as rotas a serem adicionadas.
|
|
40
|
-
prefix (str): O prefixo a ser adicionado às rotas do roteador.
|
|
41
|
-
|
|
42
|
-
Raises:
|
|
43
|
-
ChatbotError: Se a rota 'start' não for encontrada no roteador.
|
|
44
39
|
"""
|
|
45
40
|
self.__routes.update(router.routes)
|
|
46
41
|
|
|
@@ -57,7 +52,7 @@ class ChatbotApp:
|
|
|
57
52
|
route_name = route_name.strip().lower()
|
|
58
53
|
|
|
59
54
|
def decorator(func):
|
|
60
|
-
params =
|
|
55
|
+
params = {}
|
|
61
56
|
signature = inspect.signature(func)
|
|
62
57
|
output_param = signature.return_annotation
|
|
63
58
|
|
|
@@ -65,20 +60,20 @@ class ChatbotApp:
|
|
|
65
60
|
param_type = (
|
|
66
61
|
param.annotation
|
|
67
62
|
if param.annotation != inspect.Parameter.empty
|
|
68
|
-
else
|
|
63
|
+
else "Any"
|
|
69
64
|
)
|
|
70
65
|
params[param_type] = name
|
|
71
|
-
debug(f
|
|
66
|
+
debug(f"Parameter: {name}, Type: {param_type}")
|
|
72
67
|
|
|
73
68
|
self.__routes[route_name] = {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
69
|
+
"function": func,
|
|
70
|
+
"params": params,
|
|
71
|
+
"return": output_param,
|
|
77
72
|
}
|
|
78
73
|
|
|
79
74
|
@wraps(func)
|
|
80
|
-
def wrapper(*args, **kwargs):
|
|
81
|
-
return func(*args, **kwargs)
|
|
75
|
+
async def wrapper(*args, **kwargs):
|
|
76
|
+
return await func(*args, **kwargs)
|
|
82
77
|
|
|
83
78
|
return wrapper
|
|
84
79
|
|
|
@@ -89,103 +84,102 @@ class ChatbotApp:
|
|
|
89
84
|
Inicia o consumo de mensagens pelo chatbot, processando cada mensagem recebida.
|
|
90
85
|
"""
|
|
91
86
|
self.__message_consumer.reprer()
|
|
92
|
-
self.__message_consumer.start_consume(self.process_message)
|
|
93
|
-
|
|
94
|
-
def process_message(self, userCall: UserCall):
|
|
87
|
+
asyncio.run(self.__message_consumer.start_consume(self.process_message))
|
|
88
|
+
|
|
89
|
+
async def process_message(self, userCall: UserCall):
|
|
95
90
|
"""
|
|
96
91
|
Processa uma mensagem recebida, identificando a rota correspondente e executando a função associada.
|
|
97
92
|
|
|
98
93
|
Args:
|
|
99
|
-
userCall (
|
|
94
|
+
userCall (UserCall): A mensagem a ser processada.
|
|
100
95
|
|
|
101
96
|
Raises:
|
|
102
97
|
ChatbotMessageError: Se nenhuma rota for encontrada para o menu atual do usuário.
|
|
103
|
-
ChatbotError: Se o tipo de retorno da função associada à rota for inválido.
|
|
104
|
-
|
|
105
|
-
Returns:
|
|
106
|
-
str: A resposta gerada pela função da rota, que pode ser uma mensagem ou o resultado de uma redireção.
|
|
107
98
|
"""
|
|
108
|
-
|
|
99
|
+
user_id = userCall.user_id
|
|
109
100
|
route = userCall.route.lower()
|
|
110
|
-
route_handler = route.split(
|
|
111
|
-
|
|
112
|
-
obs = userCall.obs
|
|
101
|
+
route_handler = route.split(".")[-1]
|
|
102
|
+
|
|
113
103
|
handler = self.__routes.get(route_handler, None)
|
|
114
104
|
|
|
115
105
|
if not handler:
|
|
116
|
-
raise ChatbotMessageError(
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
userCall_name = handler['params'].get(UserCall, None)
|
|
122
|
-
route_state_name = handler['params'].get(Route, None)
|
|
106
|
+
raise ChatbotMessageError(user_id, f"Rota não encontrada para {route}!")
|
|
107
|
+
|
|
108
|
+
func = handler["function"]
|
|
109
|
+
userCall_name = handler["params"].get(UserCall, None)
|
|
110
|
+
route_state_name = handler["params"].get(Route, None)
|
|
123
111
|
|
|
124
|
-
kwargs =
|
|
112
|
+
kwargs = {}
|
|
125
113
|
if userCall_name:
|
|
126
114
|
kwargs[userCall_name] = userCall
|
|
127
115
|
if route_state_name:
|
|
128
116
|
kwargs[route_state_name] = Route(route, list(self.__routes.keys()))
|
|
129
117
|
|
|
130
|
-
|
|
131
|
-
|
|
118
|
+
if asyncio.iscoroutinefunction(func):
|
|
119
|
+
userCall_response = await func(**kwargs)
|
|
120
|
+
else:
|
|
121
|
+
loop = asyncio.get_running_loop()
|
|
122
|
+
userCall_response = await loop.run_in_executor(None, lambda: func(**kwargs))
|
|
123
|
+
|
|
132
124
|
if isinstance(userCall_response, (list, tuple)):
|
|
133
125
|
for response in userCall_response:
|
|
134
|
-
self.__process_func_response(response, userCall, route=route)
|
|
126
|
+
await self.__process_func_response(response, userCall, route=route)
|
|
135
127
|
else:
|
|
136
|
-
self.__process_func_response(userCall_response, userCall, route=route)
|
|
128
|
+
await self.__process_func_response(userCall_response, userCall, route=route)
|
|
129
|
+
|
|
130
|
+
async def __process_func_response(
|
|
131
|
+
self, userCall_response, userCall: UserCall, route: str
|
|
132
|
+
):
|
|
133
|
+
"""
|
|
134
|
+
Processa a resposta de uma função associada a uma rota, enviando mensagens ou ajustando estados.
|
|
135
|
+
|
|
136
|
+
Args:
|
|
137
|
+
userCall_response: A resposta gerada pela função da rota.
|
|
138
|
+
userCall (UserCall): O objeto UserCall associado à mensagem processada.
|
|
139
|
+
route (str): O nome da rota atual.
|
|
140
|
+
"""
|
|
141
|
+
loop = asyncio.get_running_loop()
|
|
137
142
|
|
|
138
|
-
def __process_func_response(self, userCall_response, userCall: UserCall, route: str):
|
|
139
|
-
|
|
140
143
|
if isinstance(userCall_response, (str, float, int)):
|
|
141
|
-
|
|
144
|
+
# Envia o resultado como mensagem (executando a chamada síncrona no executor)
|
|
145
|
+
await loop.run_in_executor(None, userCall.send, Message(userCall_response))
|
|
142
146
|
return
|
|
143
147
|
|
|
144
148
|
elif isinstance(userCall_response, Route):
|
|
145
149
|
userCall.route = userCall_response.current
|
|
146
150
|
return
|
|
147
|
-
|
|
148
|
-
elif isinstance(userCall_response, (Message, Button
|
|
149
|
-
|
|
150
|
-
|
|
151
|
+
|
|
152
|
+
elif isinstance(userCall_response, (Message, Button)):
|
|
153
|
+
# Envia o objeto Message ou Button
|
|
154
|
+
await loop.run_in_executor(None, userCall.send, userCall_response)
|
|
151
155
|
return
|
|
152
156
|
|
|
153
157
|
elif isinstance(userCall_response, EndChatResponse):
|
|
154
|
-
|
|
158
|
+
await loop.run_in_executor(
|
|
159
|
+
None,
|
|
160
|
+
userCall.end_chat,
|
|
161
|
+
userCall_response.observations,
|
|
162
|
+
userCall_response.tabulation_id,
|
|
163
|
+
)
|
|
155
164
|
return
|
|
156
|
-
|
|
165
|
+
|
|
157
166
|
elif isinstance(userCall_response, TransferToHuman):
|
|
158
|
-
|
|
167
|
+
await loop.run_in_executor(
|
|
168
|
+
None,
|
|
169
|
+
userCall.transfer_to_human,
|
|
170
|
+
userCall_response.observations,
|
|
171
|
+
userCall_response.campaign_id,
|
|
172
|
+
)
|
|
159
173
|
return
|
|
160
174
|
|
|
161
175
|
elif isinstance(userCall_response, RedirectResponse):
|
|
162
|
-
route = route +
|
|
176
|
+
route = route + "." + userCall_response.route
|
|
163
177
|
userCall.route = route
|
|
164
|
-
|
|
178
|
+
await self.process_message(userCall)
|
|
165
179
|
|
|
166
180
|
elif not userCall_response:
|
|
167
181
|
return
|
|
168
182
|
|
|
169
183
|
else:
|
|
170
|
-
error(
|
|
184
|
+
error("Tipo de retorno inválido!")
|
|
171
185
|
return None
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
def __adjust_route(self, route: str, absolute_route: str) -> str:
|
|
175
|
-
"""
|
|
176
|
-
Ajusta a rota fornecida para incluir o prefixo necessário, se não estiver presente.
|
|
177
|
-
|
|
178
|
-
Args:
|
|
179
|
-
route (str): A rota que precisa ser ajustada.
|
|
180
|
-
absolute_route (str): A rota completa atual, usada como referência.
|
|
181
|
-
|
|
182
|
-
Returns:
|
|
183
|
-
str: A rota ajustada.
|
|
184
|
-
"""
|
|
185
|
-
if not route:
|
|
186
|
-
return absolute_route
|
|
187
|
-
|
|
188
|
-
if 'start' not in route:
|
|
189
|
-
route = absolute_route + route
|
|
190
|
-
|
|
191
|
-
return route
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import grpc
|
|
3
|
+
import json
|
|
4
|
+
|
|
5
|
+
import chatgraph.pb.router_pb2 as chatbot_pb2
|
|
6
|
+
import chatgraph.pb.router_pb2_grpc as chatbot_pb2_grpc
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class RouterServiceClient:
|
|
10
|
+
def __init__(self, grpc_uri=None):
|
|
11
|
+
self.grpc_uri = grpc_uri or os.getenv("GRPC_URI")
|
|
12
|
+
|
|
13
|
+
if not self.grpc_uri:
|
|
14
|
+
raise ValueError("A variável de ambiente 'GRPC_URI' não está definida.")
|
|
15
|
+
|
|
16
|
+
# Cria o canal gRPC
|
|
17
|
+
self.channel = grpc.insecure_channel(self.grpc_uri)
|
|
18
|
+
|
|
19
|
+
# Cria os stubs para os serviços gRPC
|
|
20
|
+
self.user_state_stub = chatbot_pb2_grpc.UserStateServiceStub(self.channel)
|
|
21
|
+
self.send_message_stub = chatbot_pb2_grpc.SendMessageStub(self.channel)
|
|
22
|
+
self.transfer_stub = chatbot_pb2_grpc.TransferStub(self.channel)
|
|
23
|
+
self.end_chat_stub = chatbot_pb2_grpc.EndChatStub(self.channel)
|
|
24
|
+
|
|
25
|
+
def insert_update_user_state(self, user_state_data):
|
|
26
|
+
request = chatbot_pb2.UserState(**user_state_data)
|
|
27
|
+
try:
|
|
28
|
+
response = self.user_state_stub.InsertUpdateUserState(request)
|
|
29
|
+
if not response.status:
|
|
30
|
+
print(f"Erro ao chamar SendMessage: {response.message}")
|
|
31
|
+
return response
|
|
32
|
+
except grpc.RpcError as e:
|
|
33
|
+
print(f"Erro ao chamar InsertUpdateUserState: {e}")
|
|
34
|
+
return None
|
|
35
|
+
|
|
36
|
+
def delete_user_state(self, chat_id_data):
|
|
37
|
+
request = chatbot_pb2.ChatID(**chat_id_data)
|
|
38
|
+
try:
|
|
39
|
+
response = self.user_state_stub.DeleteUserState(request)
|
|
40
|
+
if not response.status:
|
|
41
|
+
print(f"Erro ao chamar SendMessage: {response.message}")
|
|
42
|
+
return response
|
|
43
|
+
except grpc.RpcError as e:
|
|
44
|
+
print(f"Erro ao chamar DeleteUserState: {e}")
|
|
45
|
+
return None
|
|
46
|
+
|
|
47
|
+
def get_user_state(self, chat_id_data):
|
|
48
|
+
request = chatbot_pb2.ChatID(**chat_id_data)
|
|
49
|
+
try:
|
|
50
|
+
response = self.user_state_stub.GetUserState(request)
|
|
51
|
+
return response
|
|
52
|
+
except grpc.RpcError as e:
|
|
53
|
+
print(f"Erro ao chamar GetUserState: {e}")
|
|
54
|
+
return None
|
|
55
|
+
|
|
56
|
+
def send_message(self, message_data):
|
|
57
|
+
# print(json.dumps(message_data))
|
|
58
|
+
|
|
59
|
+
request = chatbot_pb2.Message(**message_data)
|
|
60
|
+
|
|
61
|
+
try:
|
|
62
|
+
response = self.send_message_stub.SendMessage(request)
|
|
63
|
+
if not response.status:
|
|
64
|
+
print(f"Erro ao chamar SendMessage: {response.message}")
|
|
65
|
+
return response
|
|
66
|
+
except grpc.RpcError as e:
|
|
67
|
+
print(f"Erro ao chamar SendMessage: {e}")
|
|
68
|
+
return None
|
|
69
|
+
|
|
70
|
+
def transfer_to_human(self, transfer_request_data):
|
|
71
|
+
request = chatbot_pb2.TransferToHumanRequest(**transfer_request_data)
|
|
72
|
+
try:
|
|
73
|
+
response = self.transfer_stub.TransferToHuman(request)
|
|
74
|
+
if not response.status:
|
|
75
|
+
print(f"Erro ao chamar SendMessage: {response.message}")
|
|
76
|
+
return response
|
|
77
|
+
except grpc.RpcError as e:
|
|
78
|
+
print(f"Erro ao chamar TransferToHuman: {e}")
|
|
79
|
+
return None
|
|
80
|
+
|
|
81
|
+
def transfer_to_menu(self, transfer_request_data):
|
|
82
|
+
request = chatbot_pb2.TransferToMenuRequest(**transfer_request_data)
|
|
83
|
+
try:
|
|
84
|
+
response = self.transfer_stub.TransferToMenu(request)
|
|
85
|
+
if not response.status:
|
|
86
|
+
print(f"Erro ao chamar SendMessage: {response.message}")
|
|
87
|
+
return response
|
|
88
|
+
except grpc.RpcError as e:
|
|
89
|
+
print(f"Erro ao chamar TransferToMenu: {e}")
|
|
90
|
+
return None
|
|
91
|
+
|
|
92
|
+
def end_chat(self, end_chat_request_data):
|
|
93
|
+
request = chatbot_pb2.EndChatRequest(**end_chat_request_data)
|
|
94
|
+
try:
|
|
95
|
+
response = self.end_chat_stub.EndChat(request)
|
|
96
|
+
if not response.status:
|
|
97
|
+
print(f"Erro ao chamar SendMessage: {response.message}")
|
|
98
|
+
return response
|
|
99
|
+
except grpc.RpcError as e:
|
|
100
|
+
print(f"Erro ao chamar EndChat: {e}")
|
|
101
|
+
return None
|
|
102
|
+
|
|
103
|
+
def get_campaign_id(self, campaign_name):
|
|
104
|
+
request = chatbot_pb2.CampaignName(**campaign_name)
|
|
105
|
+
try:
|
|
106
|
+
response = self.transfer_stub.GetCampaignID(request)
|
|
107
|
+
return response
|
|
108
|
+
except grpc.RpcError as e:
|
|
109
|
+
print(f"Erro ao chamar GetCampaignID: {e}")
|
|
110
|
+
return None
|
|
111
|
+
|
|
112
|
+
def get_all_campaigns(self):
|
|
113
|
+
request = chatbot_pb2.Void()
|
|
114
|
+
try:
|
|
115
|
+
response = self.transfer_stub.GetAllCampaigns(request)
|
|
116
|
+
return response
|
|
117
|
+
except grpc.RpcError as e:
|
|
118
|
+
print(f"Erro ao chamar GetAllCampaigns: {e}")
|
|
119
|
+
return None
|
|
120
|
+
|
|
121
|
+
def get_tabulation_id(self, tabulation_name):
|
|
122
|
+
request = chatbot_pb2.TabulationName(**tabulation_name)
|
|
123
|
+
try:
|
|
124
|
+
response = self.end_chat_stub.GetTabulationID(request)
|
|
125
|
+
return response
|
|
126
|
+
except grpc.RpcError as e:
|
|
127
|
+
print(f"Erro ao chamar GetTabulationID: {e}")
|
|
128
|
+
return None
|
|
129
|
+
|
|
130
|
+
def get_all_tabulations(self):
|
|
131
|
+
request = chatbot_pb2.Void()
|
|
132
|
+
try:
|
|
133
|
+
response = self.end_chat_stub.GetAllTabulations(request)
|
|
134
|
+
return response
|
|
135
|
+
except grpc.RpcError as e:
|
|
136
|
+
print(f"Erro ao chamar GetAllTabulations: {e}")
|
|
137
|
+
return None
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import asyncio
|
|
3
|
+
from logging import debug, info
|
|
4
|
+
import os
|
|
5
|
+
import aio_pika
|
|
6
|
+
from typing import Callable
|
|
7
|
+
from ..auth.credentials import Credential
|
|
8
|
+
from ..types.request_types import UserCall, UserState, ChatID
|
|
9
|
+
from rich.console import Console
|
|
10
|
+
from rich.table import Table
|
|
11
|
+
from rich.text import Text
|
|
12
|
+
from rich.panel import Panel
|
|
13
|
+
from urllib.parse import quote
|
|
14
|
+
|
|
15
|
+
class MessageConsumer:
|
|
16
|
+
def __init__(
|
|
17
|
+
self,
|
|
18
|
+
credential: Credential,
|
|
19
|
+
amqp_url: str,
|
|
20
|
+
grpc_uri: str,
|
|
21
|
+
queue_consume: str,
|
|
22
|
+
prefetch_count: int = 1,
|
|
23
|
+
virtual_host: str = "/",
|
|
24
|
+
) -> None:
|
|
25
|
+
self.__virtual_host = virtual_host
|
|
26
|
+
self.__prefetch_count = prefetch_count
|
|
27
|
+
self.__queue_consume = queue_consume
|
|
28
|
+
self.__amqp_url = amqp_url
|
|
29
|
+
self.__grpc_uri = grpc_uri
|
|
30
|
+
self.__credentials = credential
|
|
31
|
+
|
|
32
|
+
@classmethod
|
|
33
|
+
def load_dotenv(
|
|
34
|
+
cls,
|
|
35
|
+
user_env: str = "RABBIT_USER",
|
|
36
|
+
pass_env: str = "RABBIT_PASS",
|
|
37
|
+
uri_env: str = "RABBIT_URI",
|
|
38
|
+
queue_env: str = "RABBIT_QUEUE",
|
|
39
|
+
prefetch_env: str = "RABBIT_PREFETCH",
|
|
40
|
+
vhost_env: str = "RABBIT_VHOST",
|
|
41
|
+
grpc_uri: str = "GRPC_URI",
|
|
42
|
+
) -> "MessageConsumer":
|
|
43
|
+
username = os.getenv(user_env)
|
|
44
|
+
password = os.getenv(pass_env)
|
|
45
|
+
url = os.getenv(uri_env)
|
|
46
|
+
queue = os.getenv(queue_env)
|
|
47
|
+
prefetch = os.getenv(prefetch_env, 1)
|
|
48
|
+
vhost = os.getenv(vhost_env, "/")
|
|
49
|
+
grpc = os.getenv(grpc_uri)
|
|
50
|
+
|
|
51
|
+
if not username or not password or not url or not queue or not grpc:
|
|
52
|
+
raise ValueError("Corrija as variáveis de ambiente!")
|
|
53
|
+
|
|
54
|
+
return cls(
|
|
55
|
+
credential=Credential(username=username, password=password),
|
|
56
|
+
amqp_url=url,
|
|
57
|
+
queue_consume=queue,
|
|
58
|
+
prefetch_count=int(prefetch),
|
|
59
|
+
virtual_host=vhost,
|
|
60
|
+
grpc_uri=grpc,
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
async def start_consume(self, process_message: Callable):
|
|
64
|
+
try:
|
|
65
|
+
user = quote(self.__credentials.username)
|
|
66
|
+
pwd = quote(self.__credentials.password)
|
|
67
|
+
vhost = quote(self.__virtual_host)
|
|
68
|
+
uri = self.__amqp_url
|
|
69
|
+
amqp_url = f"amqp://{user}:{pwd}@{uri}/{vhost}"
|
|
70
|
+
connection = await aio_pika.connect_robust(amqp_url)
|
|
71
|
+
|
|
72
|
+
async with connection:
|
|
73
|
+
channel = await connection.channel()
|
|
74
|
+
await channel.set_qos(prefetch_count=self.__prefetch_count)
|
|
75
|
+
|
|
76
|
+
try:
|
|
77
|
+
queue = await channel.get_queue(self.__queue_consume, ensure=True)
|
|
78
|
+
except aio_pika.exceptions.ChannelNotFoundEntity:
|
|
79
|
+
arguments = {
|
|
80
|
+
"x-dead-letter-exchange": "log_error", # Dead Letter Exchange
|
|
81
|
+
"x-expires": 86400000, # Expiração da fila (em milissegundos)
|
|
82
|
+
"x-message-ttl": 300000 # Tempo de vida das mensagens (em milissegundos)
|
|
83
|
+
}
|
|
84
|
+
queue = await channel.declare_queue(self.__queue_consume, durable=True, arguments=arguments)
|
|
85
|
+
|
|
86
|
+
info("[x] Server inicializado! Aguardando solicitações RPC")
|
|
87
|
+
|
|
88
|
+
async for message in queue:
|
|
89
|
+
async with message.process():
|
|
90
|
+
await self.on_request(message.body, process_message)
|
|
91
|
+
except Exception as e:
|
|
92
|
+
print(f"Erro durante o consumo de mensagens: {e}")
|
|
93
|
+
# Reiniciar a conexão em caso de falha
|
|
94
|
+
# await self.start_consume(process_message)
|
|
95
|
+
|
|
96
|
+
async def on_request(self, body: bytes, process_message: Callable):
|
|
97
|
+
try:
|
|
98
|
+
message = body.decode()
|
|
99
|
+
message_json = json.loads(message)
|
|
100
|
+
pure_message = self.__transform_message(message_json)
|
|
101
|
+
await process_message(pure_message)
|
|
102
|
+
except Exception as e:
|
|
103
|
+
print(f"Erro ao processar mensagem: {e}")
|
|
104
|
+
|
|
105
|
+
def __transform_message(self, message: dict) -> UserCall:
|
|
106
|
+
user_state = message.get("user_state", {})
|
|
107
|
+
observation = user_state.get("observation", {})
|
|
108
|
+
if isinstance(observation, str):
|
|
109
|
+
observation = json.loads(observation)
|
|
110
|
+
|
|
111
|
+
usercall = UserCall(
|
|
112
|
+
user_state=UserState(
|
|
113
|
+
chatID=ChatID(
|
|
114
|
+
user_id=user_state['chat_id'].get("user_id", ""),
|
|
115
|
+
company_id=user_state['chat_id'].get("company_id", ""),
|
|
116
|
+
),
|
|
117
|
+
menu=user_state.get("menu", ""),
|
|
118
|
+
route=user_state.get("route", ""),
|
|
119
|
+
protocol=user_state.get("protocol", ""),
|
|
120
|
+
observation=observation,
|
|
121
|
+
),
|
|
122
|
+
type_message=message.get("type_message", ""),
|
|
123
|
+
content_message=message.get("content_message", ""),
|
|
124
|
+
grpc_uri=self.__grpc_uri,
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
return usercall
|
|
128
|
+
|
|
129
|
+
def reprer(self):
|
|
130
|
+
console = Console()
|
|
131
|
+
|
|
132
|
+
title_text = Text("ChatGraph", style="bold red", justify="center")
|
|
133
|
+
title_panel = Panel.fit(title_text, title=" ", border_style="bold red", padding=(1, 4))
|
|
134
|
+
|
|
135
|
+
separator = Text("🐇🐇🐇 RabbitMessageConsumer 📨📨📨", style="cyan", justify="center")
|
|
136
|
+
|
|
137
|
+
table = Table(show_header=True, header_style="bold magenta", title="RabbitMQ Consumer")
|
|
138
|
+
table.add_column("Atributo", justify="center", style="cyan", no_wrap=True)
|
|
139
|
+
table.add_column("Valor", justify="center", style="magenta")
|
|
140
|
+
|
|
141
|
+
table.add_row("Virtual Host", self.__virtual_host)
|
|
142
|
+
table.add_row("Prefetch Count", str(self.__prefetch_count))
|
|
143
|
+
table.add_row("Queue Consume", self.__queue_consume)
|
|
144
|
+
table.add_row("AMQP URL", self.__amqp_url)
|
|
145
|
+
table.add_row("Username", self.__credentials.username)
|
|
146
|
+
table.add_row("Password", "******")
|
|
147
|
+
|
|
148
|
+
console.print(title_panel, justify="center")
|
|
149
|
+
console.print(separator, justify="center")
|
|
150
|
+
console.print(table, justify="center")
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
syntax = "proto3";
|
|
2
|
+
|
|
3
|
+
package chatbot;
|
|
4
|
+
|
|
5
|
+
option go_package = "./chatbot";
|
|
6
|
+
|
|
7
|
+
///// Serviços de Estado do Usuário /////
|
|
8
|
+
service UserStateService {
|
|
9
|
+
rpc InsertUpdateUserState(UserState) returns (RequestStatus);
|
|
10
|
+
rpc DeleteUserState(ChatID) returns (RequestStatus);
|
|
11
|
+
rpc GetUserState(ChatID) returns (UserState);
|
|
12
|
+
rpc GetAllUserStates(Void) returns (UserStateList);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
///// Serviços de Mensagens /////
|
|
16
|
+
service SendMessage {
|
|
17
|
+
rpc SendMessage(Message) returns (RequestStatus);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
///// Serviços de Transfer /////
|
|
21
|
+
service Transfer {
|
|
22
|
+
rpc GetAllCampaigns(Void) returns (CampaignsList);
|
|
23
|
+
rpc GetCampaignID(CampaignName) returns (CampaignDetails);
|
|
24
|
+
rpc TransferToHuman(TransferToHumanRequest) returns (RequestStatus);
|
|
25
|
+
rpc TransferToMenu(TransferToMenuRequest) returns (RequestStatus);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
///// Serviços de EndChat /////
|
|
29
|
+
service EndChat {
|
|
30
|
+
rpc GetAllTabulations(Void) returns (TabulationsList);
|
|
31
|
+
rpc GetTabulationID(TabulationName) returns (TabulationDetails);
|
|
32
|
+
rpc EndChat(EndChatRequest) returns (RequestStatus);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
///// Mensagens Compartilhadas /////
|
|
36
|
+
message Void {}
|
|
37
|
+
|
|
38
|
+
message RequestStatus {
|
|
39
|
+
bool status = 1;
|
|
40
|
+
string message = 2;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
message ChatID {
|
|
44
|
+
string user_id = 1;
|
|
45
|
+
string company_id = 2;
|
|
46
|
+
}
|
|
47
|
+
///// Mensagens para Estado do Usuário /////
|
|
48
|
+
message UserState {
|
|
49
|
+
ChatID chat_id = 1;
|
|
50
|
+
string menu = 2;
|
|
51
|
+
string route = 3;
|
|
52
|
+
string protocol = 4;
|
|
53
|
+
string observation = 5;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
message UserStateList {
|
|
57
|
+
repeated UserState user_states = 1;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
///// Mensagens para Serviços de Mensagens /////
|
|
61
|
+
message TextMessage {
|
|
62
|
+
string type = 1;
|
|
63
|
+
string url = 2;
|
|
64
|
+
string filename = 3;
|
|
65
|
+
string title = 4;
|
|
66
|
+
string detail = 5;
|
|
67
|
+
string caption = 6;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
message Button{
|
|
71
|
+
string type = 1;
|
|
72
|
+
string title = 2;
|
|
73
|
+
string detail = 3;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
message Message {
|
|
77
|
+
ChatID chat_id = 1;
|
|
78
|
+
TextMessage message = 2;
|
|
79
|
+
repeated Button buttons = 3;
|
|
80
|
+
Button display_button = 4;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
///// Mensagens para Serviços de Transfer /////
|
|
84
|
+
message TransferToHumanRequest{
|
|
85
|
+
ChatID chat_id = 1;
|
|
86
|
+
string campaign_id = 2;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
message TransferToMenuRequest{
|
|
90
|
+
ChatID chat_id = 1;
|
|
91
|
+
string menu = 2;
|
|
92
|
+
string user_message = 3;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
message TabulationName{
|
|
96
|
+
string name = 1;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
message TabulationDetails{
|
|
100
|
+
string id = 1;
|
|
101
|
+
string name = 2;
|
|
102
|
+
string last_update = 3;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
message TabulationsList{
|
|
106
|
+
repeated TabulationDetails tabulations = 1;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
///// Serviços de EndChat /////
|
|
110
|
+
message EndChatRequest {
|
|
111
|
+
ChatID chat_id = 1;
|
|
112
|
+
string tabulation_id = 2;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
message CampaignName {
|
|
116
|
+
string name = 1;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
message CampaignDetails {
|
|
120
|
+
string id = 1;
|
|
121
|
+
string name = 2;
|
|
122
|
+
string last_update = 3;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
message CampaignsList {
|
|
126
|
+
repeated CampaignDetails campaigns = 1;
|
|
127
|
+
}
|