|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +import openai |
| 5 | +import logging |
| 6 | +import analytics |
| 7 | +import json |
| 8 | + |
| 9 | +from telegram.ext import ApplicationBuilder, ContextTypes, CommandHandler, MessageHandler, filters |
| 10 | + |
| 11 | +logging.basicConfig( |
| 12 | + format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', |
| 13 | + level=logging.INFO |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +# Load config.json file |
| 18 | +with open('config.json') as f: |
| 19 | + config = json.load(f) |
| 20 | + |
| 21 | +# Set up ChatGPT API client |
| 22 | +openai.api_key = config['OpenAItoken'] |
| 23 | +async def start(update, context: ContextTypes.DEFAULT_TYPE): |
| 24 | + await context.bot.send_message(chat_id=update.effective_chat.id, text="Hello! I am a ChatGPT bot. How can I help you today? Just send me a message and I will keep talking.") |
| 25 | + |
| 26 | +async def statistic(update, context: ContextTypes.DEFAULT_TYPE): |
| 27 | + await context.bot.send_message(chat_id=update.effective_chat.id, text=analytics.analysis()) |
| 28 | + |
| 29 | + |
| 30 | +async def about(update, context: ContextTypes.DEFAULT_TYPE): |
| 31 | + await context.bot.send_message(chat_id=update.effective_chat.id, |
| 32 | + text="This is ChatGPT bot which can assist you in different tasks.\ |
| 33 | +You can ask to translate, rephrase or summarize text. \ |
| 34 | +You can also ask to create a letter or meeting agenda. \nEnjoy 🙂 \nBy @yuliya_rubtsova\n \n \n\ |
| 35 | +ChatGPT - это бот, который может помочь вам с различными задачами.\ |
| 36 | +Вы можете попросить его перевести, перефразировать или обобщить текст, а также создать письмо или повестку дня для встречи.\ |
| 37 | +\nПриятной работы 🙂 \nВопросы и предложения можете слать автору: @yuliya_rubtsova") |
| 38 | + |
| 39 | +async def donate(update, context: ContextTypes.DEFAULT_TYPE): |
| 40 | + |
| 41 | + await context.bot.send_message(chat_id=update.effective_chat.id, |
| 42 | + text='Sadly, OpenAI charges money for each request to the ChatGPT model, so if you appreciate the bot, <a href="some url">you can help to keep it running</a>\n \n \n\ |
| 43 | +К сожалению, OpenAI берет деньги за каждый запрос к модели ChatGPT, поэтому, если вы цените бота, вы можете <a href="some url">помочь и поддерживать его работу</a>', parse_mode='HTML') |
| 44 | + |
| 45 | +# Get response from ChatGPT API |
| 46 | +async def chat(update, context: ContextTypes.DEFAULT_TYPE): |
| 47 | + # Check if message is not None |
| 48 | + try: |
| 49 | + #if update.message and update.message.text: |
| 50 | + # Get user's message |
| 51 | + message = update.message.text |
| 52 | + # Get statistics |
| 53 | + analytics.statistics(update.effective_chat.id) |
| 54 | + # Send message to ChatGPT API |
| 55 | + response = openai.ChatCompletion.create( |
| 56 | + #engine="text-davinci-003", |
| 57 | + model="gpt-3.5-turbo", |
| 58 | + messages=[ |
| 59 | + {"role": "user", "content": message} |
| 60 | + ] |
| 61 | + ) |
| 62 | + |
| 63 | + |
| 64 | + # Get response from ChatGPT API |
| 65 | + response_text = response["choices"][0]["message"]["content"] |
| 66 | + |
| 67 | + analytics.statistics(update.effective_chat.id, update.effective_user.username, |
| 68 | + update.effective_user.first_name, |
| 69 | + update.effective_user.last_name, message, response_text) |
| 70 | + |
| 71 | + # Send response to user |
| 72 | + await context.bot.send_message(chat_id=update.effective_chat.id, text=response_text) |
| 73 | + |
| 74 | + except: |
| 75 | + await context.bot.send_message(chat_id=update.effective_chat.id, |
| 76 | + text="The bot's credentials have been depleted. To continue using it, please refill its account. \nSadly, OpenAI charges money for each request to the ChatGPT model, so if you appreciate the bot, <a href='https://yoomoney.ru/to/41001101129330'>you can help to keep it running</a>\n \n \n\ |
| 77 | + Если вы видете это сообщение, значит у бота закончились деньги. К сожалению, OpenAI берет деньги за каждый запрос к модели ChatGPT, поэтому, если вы цените бота, вы можете <a href='https://yoomoney.ru/to/41001101129330'>помочь и поддерживать его работу</a>", |
| 78 | + parse_mode='HTML') |
| 79 | + |
| 80 | + |
| 81 | +if __name__ == '__main__': |
| 82 | + application = ApplicationBuilder().token(config['TelegramBotToken']).build() |
| 83 | + |
| 84 | + start_handler = CommandHandler('start', start) |
| 85 | + chat_handler = MessageHandler(filters.TEXT & (~filters.COMMAND), chat) |
| 86 | + statistic_handler = CommandHandler('statistic', statistic) |
| 87 | + about_handler = CommandHandler('about', about) |
| 88 | + donate_handler = CommandHandler('donate', donate) |
| 89 | + |
| 90 | + application.add_handler(start_handler) |
| 91 | + application.add_handler(chat_handler) |
| 92 | + application.add_handler(statistic_handler) |
| 93 | + application.add_handler(about_handler) |
| 94 | + application.add_handler(donate_handler) |
| 95 | + |
| 96 | + application.run_polling() |
| 97 | + |
0 commit comments