| from telegram.ext import Updater, CommandHandler, CallbackContext, MessageHandler |
| from telegram import Update |
| import openai |
|
|
| def hello(update: Update, context: CallbackContext) -> None: |
| update.message.reply_text(f'Hello Mister {update.effective_user.first_name}. \n My Bot gives you constrainsless talk with chatGPT-3 .\n Need help ? type : /help') |
|
|
| def help(update: Update, context: CallbackContext) -> None: |
| update.message.reply_text(f'help menu \n To send your query just type : /input ') |
|
|
| def start_input(update: Update, context: CallbackContext) -> None: |
| context.user_data['state'] = 1 |
| update.message.reply_text("Please type your question/query :") |
|
|
| def get_gpt_response(user_input: str) -> str: |
| openai.api_key = "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" |
| response = openai.Completion.create(engine="text-davinci-003", prompt=user_input, max_tokens = 2048) |
| response_text = response["choices"][0]["text"] |
| return f'"""{response_text}"""' |
|
|
| def get_user_input(update: Update, context: CallbackContext) -> None: |
| if 'state' in context.user_data and context.user_data['state'] == 1: |
| user_input = update.message.text |
| context.user_data['user_input'] = user_input |
| |
|
|
| with open("user_input.txt", "a") as f: |
| f.write(f'{user_input}\n') |
|
|
| gpt_response = get_gpt_response(user_input) |
| update.message.reply_text(f'GPT-3 Response: {gpt_response.strip()}') |
| else: |
| update.message.reply_text("Invalid input") |
|
|
| updater = Updater('5884885093:AAETERETtLt6LFf8rV_WYqQa0LwlbnLYbGs') |
| updater.dispatcher.add_handler(CommandHandler('start', hello)) |
| updater.dispatcher.add_handler(CommandHandler('help', help)) |
| updater.dispatcher.add_handler(CommandHandler('input', start_input)) |
| updater.dispatcher.add_handler(MessageHandler(None, get_user_input)) |
| updater.start_polling() |
| updater.idle() |
|
|
|
|