-
Notifications
You must be signed in to change notification settings - Fork 0
/
platzi_bot.py
93 lines (75 loc) · 2.76 KB
/
platzi_bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import os
import time
import requests
from openai import OpenAI, OpenAIError
from dotenv import load_dotenv
load_dotenv()
openai_client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
def get_updates(offset=None):
telegram_url = os.environ.get("TELEGRAM_BOT_URL")
telegram_token = os.environ.get("TELEGRAM_BOT_TOKEN")
url = f"{telegram_url}{telegram_token}/getUpdates"
params = {
"timeout": 100,
"offset": offset,
}
try:
response = requests.get(url=url, params=params)
return response.json()["result"]
except Exception as e:
print(f"Error al obtener actualizaciones del bot de telegram: {e}")
def send_messages(chat_id, text):
telegram_url = os.environ.get("TELEGRAM_BOT_URL")
telegram_token = os.environ.get("TELEGRAM_BOT_TOKEN")
url = f"{telegram_url}{telegram_token}/sendMessage"
params = {
"chat_id": chat_id,
"text": text,
}
try:
response = requests.post(url=url, params=params)
return response
except Exception as e:
print(f"Error al enviar actualizaciones del bot de telegram: {e}")
def get_openai_response(prompt):
system = """Eres un asitente de atención a clientes y estudiantes de la
plataforma de educación en línea en tecnología, inglés y liderazgo llamado Platzi"""
try:
response = openai_client.chat.completions.create(
model=os.environ.get("OPENAI_MODEL_FINE_TUNING"),
messages=[
{"role": "system", "content": system},
{"role": "user", "content": prompt},
],
max_tokens=150,
n=1,
temperature=0.5,
)
return response.choices[0].message.content.strip()
except OpenAIError as e:
print(f"La API de OpenAI devolvió un error: {e}")
def main():
print("starting bot...")
offset = 0
while True:
updates = get_updates(offset=offset)
if updates:
for update in updates:
if "message" in update:
offset = update["update_id"] + 1
chat_id = update["message"]["chat"]["id"]
user_message = update["message"]["text"]
user_name = (
update["message"]["from"]["first_name"]
+ " "
+ update["message"]["from"]["last_name"]
+ " - "
+ update["message"]["from"]["username"]
)
print(f"Message received: {user_message} from: {user_name}")
gpt = get_openai_response(user_message)
send_messages(chat_id, gpt)
else:
time.sleep(1)
if __name__ == "__main__":
main()