Answer the question
In order to leave comments, you need to log in
How to control two telegram bots with one Python process?
I have two telegram bots which consist of:
1. App.py:
import telebot
from telebot import types
API_TOKEN = 'XXXXXXXXXXXXX'
bot = telebot.TeleBot(API_TOKEN, threaded=False)
import driverbot_main, driverbot_callbacks
bot.polling()
from telebot.apihelper import delete_webhook
from driverbot_app import bot as bot
from telebot import types
@bot.message_handler(commands=['start'])
def start(message):
try:
markup = types.ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard = False)
item1 = types.KeyboardButton("Main menu")
item2 = types.KeyboardButton("Settings")
markup.add(item1,item2)
bot.send_message(message.chat.id, "Welcome!".format(message.from_user, bot.get_me()),
parse_mode='html', reply_markup=markup)
except:
pass
from driverbot_app import bot as bot
from telebot import types
@bot.callback_query_handler(func=lambda call: call.data == 'wash_car')
def query(call):
keyboard = types.InlineKeyboardMarkup()
item1 = types.InlineKeyboardButton('5 min', callback_data = '5min')
item2 = types.InlineKeyboardButton('15 min', callback_data = '15min')
keyboard.add(item1,item2)
bot.send_message(call.message.chat.id, 'Location recieved. When do you want to wash your car?', parse_mode = 'html', reply_markup = keyboard)
Answer the question
In order to leave comments, you need to log in
Run bot code in different threads. Python is so-so with this. So don't be foolish, run two processes.
I rummaged through the source codes
, everything that needs to be prepared for normal asynchrony is in them, but the main processing method itself is not turned inside out so that you can do something like:
bot1 = telebot.AsyncTeleBot(API_TOKEN1, threaded=False)
bot2 = telebot.AsyncTeleBot(API_TOKEN2, threaded=False)
...
loop = asyncio.get_event_loop()
loop.create_task(bot1.pooling_step())
loop.create_task(bot2.pooling_step())
loop.run_forever()
if updates:
self.offset = updates[-1].update_id + 1
self._loop_create_task(self.process_new_updates(updates)) # Seperate task for processing updates
if interval: await asyncio.sleep(interval)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question