Hello everyone,
I’m trying to integrate a Telegram bot with a custom assistant I created in OpenAI’s Playground. The assistant is fully configured, and I have its ID: asst_********
. I already have the Telegram bot set up, but I can’t get it to work properly with the assistant.
From the documentation, I understand that I need to create a thread (Thread
) and run interactions via a Run
. I wrote the code with the help of ChatGPT, but there seems to be an error in the syntax or logic, and I can’t figure out what’s wrong.
Issues I’m Facing:
- On the first message, the bot responds: “Could not get a response from the assistant.”
- On the second message, it responds: “An error occurred while processing your message. Please try again later.”
- In the program console, I get the error:
4.*Error while sending message: Unknown error in HTTP implementation: TypeError('Object of type TextContentBlock is not JSON serializable')
**
What I’ve Tried:
- I set up the assistant and ensured the assistant ID is correct.
- The thread seems to be created successfully, but messages do not get processed properly. I suspect there’s an issue with how I’m adding messages or running the thread with the assistant.
Here’s the code I’m currently using (API keys are replaced with ***
):
import openai
from openai import OpenAI
from telegram import Update
from telegram.ext import Application, CommandHandler, MessageHandler, ContextTypes
from telegram.ext.filters import TEXT
# OpenAI setup
openai.api_key = "***"
client = OpenAI(api_key=openai.api_key)
# Custom Assistant ID
ASSISTANT_ID = "asst_********"
# Telegram API Key
TELEGRAM_API_KEY = "***"
# Storing threads for each user
user_threads = {}
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
user_id = update.effective_user.id
try:
thread = client.beta.threads.create()
thread_id = thread.id
user_threads[user_id] = thread_id
except Exception as e:
await update.message.reply_text("Could not establish a connection with the assistant. Please try again later.")
print(f"Error creating thread: {e}")
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
user_id = update.effective_user.id
user_message = update.message.text
if user_id not in user_threads:
await update.message.reply_text("Please start with the /start command.")
return
thread_id = user_threads[user_id]
try:
client.beta.threads.messages.create(thread_id=thread_id, role="user", content=user_message)
client.beta.threads.runs.create(thread_id=thread_id, assistant_id=ASSISTANT_ID)
messages = client.beta.threads.messages.list(thread_id=thread_id)
assistant_response = next((msg.content for msg in messages if msg.role == 'assistant'), "Could not get a response from the assistant.")
await update.message.reply_text(assistant_response)
except Exception as e:
await update.message.reply_text("An error occurred while processing your message. Please try again later.")
print(f"Error while sending message: {e}")
def main():
app = Application.builder().token(TELEGRAM_API_KEY).build()
app.add_handler(CommandHandler("start", start))
app.add_handler(MessageHandler(TEXT, handle_message))
app.run_polling()
if __name__ == "__main__":
main()