Replies: 4 comments
-
import json API_KEYS = { api_mode = {} question_cache = {} def set_api_1(update: Update, _: CallbackContext) -> None: def set_api_2(update: Update, _: CallbackContext) -> None: def auto(update: Update, _: CallbackContext) -> None: def send_question_to_chatgpt(question: str, api_key: str) -> str:
def handle_message(update: Update, _: CallbackContext) -> None:
def main() -> None:
if name == 'main': |
Beta Was this translation helpful? Give feedback.
-
you need to do this : pip uninstall python-telegram-bot its need telegram python bot v13.7 |
Beta Was this translation helpful? Give feedback.
-
import json API_KEYS = { api_mode = {} question_cache = {} def set_api_1(update: Update, _: CallbackContext) -> None: def set_api_2(update: Update, _: CallbackContext) -> None: def auto(update: Update, _: CallbackContext) -> None: def send_question_to_chatgpt(question: str, api_key: str) -> str:
def handle_message(update: Update, _: CallbackContext) -> None:
def main() -> None:
if name == 'main': |
Beta Was this translation helpful? Give feedback.
-
What is what bro, I don't understand. Is it separated modules? If so - name them, actually, do you wanna work on this bot together though? I think its a good idea to make an additional module that stores all the available api keys and uses them simultaneously in async-way as the users ask, also we should add some cooldown for questions cuz 1 api can't handle more than 3 requests on 1 endpoint cuz right now its crap when there are >10 users |
Beta Was this translation helpful? Give feedback.
-
Now the bot will have an /auto command that toggles auto mode for each question . In auto mode, the bot will automatically alternate between API 1 and API 2 for each message the user sends without needing the /1 or /2 prefix.
Here's the updated code that alternates between the two APIs when the /auto command is used:
import requests
from telegram import Update, ForceReply
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext
API_KEY_1 = 'your_api_key_1'
API_KEY_2 = 'your_api_key_2'
TELEGRAM_BOT_TOKEN = 'your_telegram_bot_token'
api_auto_mode = {}
def start(update: Update, _: CallbackContext) -> None:
user = update.effective_user
update.message.reply_markdown_v2(
fr'Hi {user.mention_markdown_v2()}! Send me a question starting with /1 or /2 to use different APIs. Use /auto to toggle auto mode',
reply_markup=ForceReply(selective=True),
)
def auto(update: Update, context: CallbackContext) -> None:
user_id = update.effective_user.id
api_auto_mode[user_id] = not api_auto_mode.get(user_id, False)
update.message.reply_text('Auto mode is now {}'.format('ON' if api_auto_mode[user_id] else 'OFF'))
def handle_message(update: Update, _: CallbackContext) -> None:
text = update.message.text.strip()
user_id = update.effective_user.id
def send_question_to_api_1(question: str) -> str:
url = f'https://api1.example.com/ask?api_key={API_KEY_1}&question={question}'
response = requests.get(url)
return response.json()['answer']
def send_question_to_api_2(question: str) -> str:
url = f'https://api2.example.com/ask?api_key={API_KEY_2}&question={question}'
response = requests.get(url)
return response.json()['answer']
def main() -> None:
updater = Updater(TELEGRAM_BOT_TOKEN)
if name == 'main':
main()
Beta Was this translation helpful? Give feedback.
All reactions