chatgraph 0.2.3__tar.gz → 0.2.5__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.2.3 → chatgraph-0.2.5}/PKG-INFO +2 -1
- {chatgraph-0.2.3 → chatgraph-0.2.5}/chatgraph/bot/chatbot_model.py +13 -4
- {chatgraph-0.2.3 → chatgraph-0.2.5}/chatgraph/messages/base_message_consumer.py +7 -0
- chatgraph-0.2.5/chatgraph/messages/base_message_sender.py +23 -0
- {chatgraph-0.2.3 → chatgraph-0.2.5}/chatgraph/messages/rabbitMQ_message_consumer.py +37 -5
- chatgraph-0.2.5/chatgraph/messages/rabbitMQ_message_sender.py +79 -0
- {chatgraph-0.2.3 → chatgraph-0.2.5}/chatgraph/types/output_state.py +1 -1
- {chatgraph-0.2.3 → chatgraph-0.2.5}/pyproject.toml +2 -1
- {chatgraph-0.2.3 → chatgraph-0.2.5}/LICENSE +0 -0
- {chatgraph-0.2.3 → chatgraph-0.2.5}/README.md +0 -0
- {chatgraph-0.2.3 → chatgraph-0.2.5}/chatgraph/__init__.py +0 -0
- {chatgraph-0.2.3 → chatgraph-0.2.5}/chatgraph/auth/credentials.py +0 -0
- {chatgraph-0.2.3 → chatgraph-0.2.5}/chatgraph/bot/chatbot_router.py +0 -0
- {chatgraph-0.2.3 → chatgraph-0.2.5}/chatgraph/error/chatbot_error.py +0 -0
- {chatgraph-0.2.3 → chatgraph-0.2.5}/chatgraph/error/route_error.py +0 -0
- {chatgraph-0.2.3 → chatgraph-0.2.5}/chatgraph/messages/test_message_consumer.py +0 -0
- {chatgraph-0.2.3 → chatgraph-0.2.5}/chatgraph/types/message_types.py +0 -0
- {chatgraph-0.2.3 → chatgraph-0.2.5}/chatgraph/types/route.py +0 -0
- {chatgraph-0.2.3 → chatgraph-0.2.5}/chatgraph/types/user_state.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: chatgraph
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.5
|
|
4
4
|
Summary: A user-friendly chatbot library
|
|
5
5
|
Home-page: https://github.com/irissonnlima/chatgraph
|
|
6
6
|
License: MIT
|
|
@@ -12,6 +12,7 @@ Classifier: License :: OSI Approved :: MIT License
|
|
|
12
12
|
Classifier: Programming Language :: Python :: 3
|
|
13
13
|
Classifier: Programming Language :: Python :: 3.12
|
|
14
14
|
Requires-Dist: pika (>=1.3.2,<2.0.0)
|
|
15
|
+
Requires-Dist: rich (>=13.8.1,<14.0.0)
|
|
15
16
|
Project-URL: Repository, https://github.com/irissonnlima/chatgraph
|
|
16
17
|
Description-Content-Type: text/markdown
|
|
17
18
|
|
|
@@ -98,8 +98,9 @@ class ChatbotApp(ABC):
|
|
|
98
98
|
"""
|
|
99
99
|
Inicia o consumo de mensagens pelo chatbot, processando cada mensagem recebida.
|
|
100
100
|
"""
|
|
101
|
+
self.__message_consumer.reprer()
|
|
101
102
|
self.__message_consumer.start_consume(self.process_message)
|
|
102
|
-
|
|
103
|
+
|
|
103
104
|
def process_message(self, message: Message):
|
|
104
105
|
"""
|
|
105
106
|
Processa uma mensagem recebida, identificando a rota correspondente e executando a função associada.
|
|
@@ -138,7 +139,13 @@ class ChatbotApp(ABC):
|
|
|
138
139
|
|
|
139
140
|
if type(message_response) in (str, float, int):
|
|
140
141
|
response = ChatbotResponse(message_response)
|
|
141
|
-
|
|
142
|
+
response = message_response.json()
|
|
143
|
+
response['user_state'] = {
|
|
144
|
+
'customer_id': customer_id,
|
|
145
|
+
'menu': menu,
|
|
146
|
+
'obs': user_state.obs,
|
|
147
|
+
}
|
|
148
|
+
return json.dumps(response)
|
|
142
149
|
elif type(message_response) == ChatbotResponse:
|
|
143
150
|
route = self.__adjust_route(message_response.route, menu)
|
|
144
151
|
response = message_response.json()
|
|
@@ -149,7 +156,9 @@ class ChatbotApp(ABC):
|
|
|
149
156
|
}
|
|
150
157
|
return json.dumps(response)
|
|
151
158
|
elif type(message_response) in (ChatbotResponse, EndChatResponse, TransferToHuman):
|
|
152
|
-
|
|
159
|
+
response = message_response.json()
|
|
160
|
+
response['customer_id'] = customer_id
|
|
161
|
+
return json.dumps(response)
|
|
153
162
|
elif type(message_response) == RedirectResponse:
|
|
154
163
|
route = self.__adjust_route(message_response.route, menu)
|
|
155
164
|
message.user_state.menu = route
|
|
@@ -174,4 +183,4 @@ class ChatbotApp(ABC):
|
|
|
174
183
|
if 'start' not in route:
|
|
175
184
|
route = absolute_route + route
|
|
176
185
|
|
|
177
|
-
return route
|
|
186
|
+
return route
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
from abc import ABC, abstractmethod
|
|
2
|
+
|
|
3
|
+
class MessageSender(ABC):
|
|
4
|
+
|
|
5
|
+
@abstractmethod
|
|
6
|
+
def load_dotenv(self) -> 'MessageSender':
|
|
7
|
+
"""
|
|
8
|
+
Carrega variáveis de ambiente para configurar o disparador de mensagens.
|
|
9
|
+
|
|
10
|
+
Returns:
|
|
11
|
+
MessageSender: A instância do disparador de mensagens configurado.
|
|
12
|
+
"""
|
|
13
|
+
pass
|
|
14
|
+
|
|
15
|
+
@abstractmethod
|
|
16
|
+
def send_message(self) -> None:
|
|
17
|
+
"""
|
|
18
|
+
Envia uma mensagem para o destino especificado.
|
|
19
|
+
|
|
20
|
+
Args:
|
|
21
|
+
message (str): A mensagem a ser enviada.
|
|
22
|
+
"""
|
|
23
|
+
pass
|
|
@@ -6,7 +6,10 @@ from typing import Callable
|
|
|
6
6
|
from ..auth.credentials import Credential
|
|
7
7
|
from ..types.message_types import Message, UserState
|
|
8
8
|
from .base_message_consumer import MessageConsumer
|
|
9
|
-
|
|
9
|
+
from rich.console import Console
|
|
10
|
+
from rich.table import Table
|
|
11
|
+
from rich.text import Text
|
|
12
|
+
from rich.panel import Panel
|
|
10
13
|
|
|
11
14
|
class RabbitMessageConsumer(MessageConsumer):
|
|
12
15
|
"""
|
|
@@ -161,17 +164,46 @@ class RabbitMessageConsumer(MessageConsumer):
|
|
|
161
164
|
Returns:
|
|
162
165
|
Message: Uma instância da classe Message com os dados extraídos do dicionário.
|
|
163
166
|
"""
|
|
167
|
+
|
|
168
|
+
user_state = message.get('user_state', {})
|
|
164
169
|
return Message(
|
|
165
170
|
type=message.get('type', ''),
|
|
166
171
|
text=message.get('text', ''),
|
|
167
172
|
user_state=UserState(
|
|
168
|
-
customer_id=
|
|
169
|
-
menu=
|
|
170
|
-
lst_update=
|
|
171
|
-
obs=
|
|
173
|
+
customer_id=user_state.get('customer_id', ''),
|
|
174
|
+
menu=user_state.get('menu', ''),
|
|
175
|
+
lst_update=user_state.get('lst_update', ''),
|
|
176
|
+
obs=user_state.get('obs', {}),
|
|
172
177
|
),
|
|
173
178
|
channel=message.get('channel', ''),
|
|
174
179
|
customer_phone=message.get('customer_phone', ''),
|
|
175
180
|
company_phone=message.get('company_phone', ''),
|
|
176
181
|
status=message.get('status'),
|
|
177
182
|
)
|
|
183
|
+
|
|
184
|
+
def reprer(self):
|
|
185
|
+
console = Console()
|
|
186
|
+
|
|
187
|
+
# Título "ChatGraph" destacado em vermelho e negrito dentro de um painel
|
|
188
|
+
title_text = Text("ChatGraph", style="bold red", justify="center")
|
|
189
|
+
title_panel = Panel.fit(title_text, title=" ", border_style="bold red", padding=(1, 4))
|
|
190
|
+
|
|
191
|
+
# Linha separadora com emojis
|
|
192
|
+
separator = Text("🐇🐇🐇 RabbitMessageConsumer 📨📨📨", style="cyan", justify="center")
|
|
193
|
+
|
|
194
|
+
# Criação da tabela com os atributos
|
|
195
|
+
table = Table(show_header=True, header_style="bold magenta", title="RabbitMQ Consumer")
|
|
196
|
+
table.add_column("Atributo", justify="center", style="cyan", no_wrap=True)
|
|
197
|
+
table.add_column("Valor", justify="center", style="magenta")
|
|
198
|
+
|
|
199
|
+
table.add_row("Virtual Host", self.__virtual_host)
|
|
200
|
+
table.add_row("Prefetch Count", str(self.__prefetch_count))
|
|
201
|
+
table.add_row("Queue Consume", self.__queue_consume)
|
|
202
|
+
table.add_row("AMQP URL", self.__amqp_url)
|
|
203
|
+
table.add_row("Username", self.__credentials.username)
|
|
204
|
+
table.add_row("Password", "******") # Oculta a senha
|
|
205
|
+
|
|
206
|
+
# Imprime o título, separador e a tabela centralizada
|
|
207
|
+
console.print(title_panel, justify="center")
|
|
208
|
+
console.print(separator, justify="center")
|
|
209
|
+
console.print(table, justify="center")
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
from .base_message_sender import MessageSender
|
|
2
|
+
from ..auth.credentials import Credential
|
|
3
|
+
import pika
|
|
4
|
+
import os
|
|
5
|
+
|
|
6
|
+
class RabbitMessageSender(MessageSender):
|
|
7
|
+
def __init__(
|
|
8
|
+
self,
|
|
9
|
+
credential: Credential,
|
|
10
|
+
amqp_url: str,
|
|
11
|
+
send_queue: str,
|
|
12
|
+
virtual_host: str = '/',
|
|
13
|
+
) -> None:
|
|
14
|
+
"""
|
|
15
|
+
Inicializa o consumidor de mensagens RabbitMQ com as configurações fornecidas.
|
|
16
|
+
|
|
17
|
+
Args:
|
|
18
|
+
credential (Credential): Credenciais de autenticação para o RabbitMQ.
|
|
19
|
+
amqp_url (str): A URL de conexão AMQP do RabbitMQ.
|
|
20
|
+
queue_consume (str): O nome da fila de consumo.
|
|
21
|
+
prefetch_count (int, opcional): O número de mensagens pré-carregadas. Padrão é 1.
|
|
22
|
+
virtual_host (str, opcional): O host virtual do RabbitMQ. Padrão é '/'.
|
|
23
|
+
"""
|
|
24
|
+
self.__virtual_host = virtual_host
|
|
25
|
+
self.__send_queue = send_queue
|
|
26
|
+
self.__amqp_url = amqp_url
|
|
27
|
+
self.__credentials = pika.PlainCredentials(
|
|
28
|
+
credential.username, credential.password
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
@classmethod
|
|
32
|
+
def load_dotenv(
|
|
33
|
+
cls,
|
|
34
|
+
user_env: str = 'RABBIT_USER',
|
|
35
|
+
pass_env: str = 'RABBIT_PASS',
|
|
36
|
+
uri_env: str = 'RABBIT_URI',
|
|
37
|
+
queue_env: str = 'RABBIT_QUEUE',
|
|
38
|
+
prefetch_env: str = 'RABBIT_PREFETCH',
|
|
39
|
+
vhost_env: str = 'RABBIT_VHOST',
|
|
40
|
+
) -> 'RabbitMessageSender':
|
|
41
|
+
"""
|
|
42
|
+
Carrega variáveis de ambiente para configurar o disparador de mensagens.
|
|
43
|
+
Args:
|
|
44
|
+
user_env (str): Nome da variável de ambiente para o usuário do RabbitMQ. Padrão é 'RABBIT_USER'.
|
|
45
|
+
pass_env (str): Nome da variável de ambiente para a senha do RabbitMQ. Padrão é 'RABBIT_PASS'.
|
|
46
|
+
uri_env (str): Nome da variável de ambiente para a URL do RabbitMQ. Padrão é 'RABBIT_URI'.
|
|
47
|
+
queue_env (str): Nome da variável de ambiente para a fila de consumo do RabbitMQ. Padrão é 'RABBIT_QUEUE'.
|
|
48
|
+
prefetch_env (str): Nome da variável de ambiente para o prefetch count. Padrão é 'RABBIT_PREFETCH'.
|
|
49
|
+
vhost_env (str): Nome da variável de ambiente para o host virtual do RabbitMQ. Padrão é 'RABBIT_VHOST'.
|
|
50
|
+
Raises:
|
|
51
|
+
ValueError: Se as variáveis de ambiente não forem configuradas corret
|
|
52
|
+
Returns:
|
|
53
|
+
RabbitMessageSender: A instância do disparador de mensagens configurado.
|
|
54
|
+
"""
|
|
55
|
+
username = os.getenv(user_env)
|
|
56
|
+
password = os.getenv(pass_env)
|
|
57
|
+
url = os.getenv(uri_env)
|
|
58
|
+
queue = os.getenv(queue_env)
|
|
59
|
+
vhost = os.getenv(vhost_env, '/')
|
|
60
|
+
|
|
61
|
+
if not username or not password or not url or not queue:
|
|
62
|
+
raise ValueError('Corrija as variáveis de ambiente!')
|
|
63
|
+
|
|
64
|
+
return cls(
|
|
65
|
+
credential=Credential(username=username, password=password),
|
|
66
|
+
amqp_url=url,
|
|
67
|
+
send_queue=queue,
|
|
68
|
+
virtual_host=vhost,
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def send_message(self) -> None:
|
|
73
|
+
"""
|
|
74
|
+
Envia uma mensagem para o destino especificado.
|
|
75
|
+
|
|
76
|
+
Args:
|
|
77
|
+
message (str): A mensagem a ser enviada.
|
|
78
|
+
"""
|
|
79
|
+
pass
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[tool.poetry]
|
|
2
2
|
name = "chatgraph"
|
|
3
|
-
version = "0.2.
|
|
3
|
+
version = "0.2.5"
|
|
4
4
|
description = "A user-friendly chatbot library"
|
|
5
5
|
authors = ["Irisson N. Lima <irisson.lima@verdecard.com.br>"]
|
|
6
6
|
readme = "README.md"
|
|
@@ -12,6 +12,7 @@ license = "MIT"
|
|
|
12
12
|
[tool.poetry.dependencies]
|
|
13
13
|
python = "^3.12"
|
|
14
14
|
pika = "^1.3.2"
|
|
15
|
+
rich = "^13.8.1"
|
|
15
16
|
|
|
16
17
|
[tool.poetry.group.dev.dependencies]
|
|
17
18
|
ruff = "^0.5.1"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|