Created
March 25, 2023 13:12
-
-
Save fskuratov/35e0f0ca1b6c2354fddf38496014a01a to your computer and use it in GitHub Desktop.
A Python script for bot that transforms every message sent to it into a task.
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
import logging | |
import openai | |
from telegram import Update | |
from telegram.ext import ApplicationBuilder, ContextTypes, CommandHandler, MessageHandler, filters | |
# Set up logging for easier debugging | |
logging.basicConfig( | |
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', | |
level=logging.INFO | |
) | |
# Set OpenAI API key | |
openai.api_key = "INSERT YOUR KEY HERE" | |
# Function to generate a response using GPT-3.5-turbo | |
async def generate_response(prompt): | |
# Create a list of messages to send to GPT-3.5-turbo as input | |
messages = [{"role": "system", "content": "You are a helpful assistant that converts user messages into tasks."}, | |
{"role": "user", "content": prompt}] | |
# Call the OpenAI API with the list of messages | |
response = openai.ChatCompletion.create( | |
model="gpt-3.5-turbo", | |
messages=messages, | |
max_tokens=150, | |
n=1, | |
stop=None, | |
temperature=0.2, | |
) | |
# Extract the content of the response and return it | |
return response.choices[0].message['content'].strip() | |
# Function to process a message and generate a response using GPT-3 | |
async def process_message(message): | |
# Pass the message to GPT-3 and get the response | |
gpt3_response = await generate_response( | |
f"Create a task from the following text: \"{message}\". Include task name, due date (if any), a brief summary (no more than 30 words), and sub-tasks (if applicable).") | |
return gpt3_response | |
# Function to handle the /start command | |
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE): | |
gpt3_response = await generate_response("Make a welcome message that says that the bot is able to convert text messages sent to it into tasks") | |
await context.bot.send_message(chat_id=update.effective_chat.id, text=gpt3_response) | |
# Function to handle incoming text messages (excluding commands) | |
async def message_handler(update: Update, context: ContextTypes.DEFAULT_TYPE): | |
processed_message = await process_message(update.message.text) | |
await context.bot.send_message(chat_id=update.effective_chat.id, text=processed_message) | |
# Main function to set up and run the bot | |
if __name__ == '__main__': | |
# Create the application object with the bot token | |
application = ApplicationBuilder().token('INSERT YOUR TELEGRAM BOT TOKEN HERE').build() | |
# Create and add the /start command handler | |
start_handler = CommandHandler('start', start) | |
application.add_handler(start_handler) | |
# Create and add the message handler for non-command text messages | |
message_handler = MessageHandler(filters.TEXT & (~filters.COMMAND), message_handler) | |
application.add_handler(message_handler) | |
# Start polling for updates | |
application.run_polling() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment