Error white creating a telegram bot

Hi @laditi454

There are few errors in your code related to OpenAI library. Also mention your actual telegram bot token on line number 7. Below I’m attaching entire code for telegram chatbot. Please do try this and let me know. Make sure that you have set up the telegram bot correctly before writing the code.

main.py:

import os

import openai

from aiogram import Bot, Dispatcher, executor, types

from example import example

bot = Bot(token='REPLACE THIS WITH YOUR TELEGRAM BOT TOKEN')

dp = Dispatcher(bot)

#Replace 'OPENAI_API_KEY' with your actual OpenAI API key OR keep it as it is and enter the OpenAI api key in the secrets section on Replit
openai.api_key = os.environ['OPENAI_API_KEY']

example()


@dp.message_handler(commands=['start', 'help'])
async def welcome(message: types.Message):

  await message.reply('Hello! I am GPT Chat BOT. You can ask me anything :)')


@dp.message_handler()
async def gpt(message: types.Message):

  response = openai.completions.create(model="gpt-3.5-turbo-instruct",
                                       prompt=message.text,
                                       temperature=0.5,
                                       max_tokens=1024,
                                       top_p=1,
                                       frequency_penalty=0.0,
                                       presence_penalty=0.0)

  await message.reply(response.choices[0].text)


if __name__ == "__main__":

  executor.start_polling(dp)

Replace the Bot token and OpenAI API key with your actual keys.

example.py:

from flask import Flask, render_template

from threading import Thread

app = Flask(__name__)

@app.route('/')

def index():

 return "example" 

def run():

 app.run(host='0.0.0.0',port=8080) 

def example():

 t = Thread(target=run)

 t.start()


Do try this and let us know. If you still face issues, please feel free to get back to us.