forked from munonzito/telegram-gpt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 212d745
Showing
3 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
TELEGRAM_TOKEN=pon_tu_token_de_telegram | ||
OPENAI_API_KEY=pon_tu_api_key_de_openai |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from telegram.ext import Application, ContextTypes, MessageHandler, filters | ||
from telegram import Update | ||
from dotenv import load_dotenv | ||
import os | ||
import openai | ||
|
||
load_dotenv() | ||
openai.api_key = os.getenv("OPENAI_API_KEY") | ||
|
||
mensajes = {} | ||
|
||
#Guardar el mensaje del usuario | ||
def handle_user_message(message): | ||
#Si el usuario no ha enviado ningun mensaje, se crea un nuevo diccionario | ||
if mensajes.get(message.from_user.id) == None: | ||
mensajes[message.from_user.id] = {"messages": [{ | ||
"role": "user", | ||
"content": message.text}]} | ||
#Si el usuario ya ha enviado mensajes, se añade el mensaje al diccionario | ||
else: | ||
mensajes[message.from_user.id]["messages"].append({ | ||
"role": "user", | ||
"content": message.text}) | ||
|
||
#Generar la respuesta del bot | ||
def generate_response(message): | ||
#Generamos la respuesta con ChatGPT | ||
completion = openai.chat.completions.create( | ||
model="gpt-3.5-turbo", | ||
messages=mensajes[message.from_user.id]["messages"],) | ||
|
||
#Obtenemos la respuesta | ||
response = completion.choices[0].message.content | ||
|
||
#Añadimos la respuesta al diccionario | ||
mensajes[message.from_user.id]["messages"].append({ | ||
"role": "assistant", | ||
"content": response}) | ||
return response | ||
|
||
#Funcion que se ejecuta cuando se recibe un mensaje | ||
async def message_handler(update: Update, context: ContextTypes.DEFAULT_TYPE): | ||
handle_user_message(update.message) | ||
response = generate_response(update.message) | ||
await update.message.reply_text(response) | ||
|
||
def main(): | ||
#Cargamos el token de la API de Telegram | ||
TELEGRAM_TOKEN = os.getenv("TELEGRAM_TOKEN") | ||
#Creamos el bot | ||
bot = Application.builder().token(TELEGRAM_TOKEN).build() | ||
|
||
#Funcion que se ejecuta cuando se recibe un mensaje | ||
bot.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, message_handler)) | ||
|
||
#Ejecutamos el bot | ||
bot.run_polling(allowed_updates=Update.ALL_TYPES) | ||
|
||
if __name__ == "__main__": | ||
main() |
Binary file not shown.