B
B
belugasinister2021-11-28 14:59:19
Python
belugasinister, 2021-11-28 14:59:19

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()


2. Main.py (running this module brings the bot to life):

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


3. Callbacks.py:

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)


Both bots have an identical structure but are aimed at different audiences and tasks. I would like them to be united by a common process and allow information and triggers to flow from one bot to another. How can the orange block from the diagram below be implemented? :

61a36ebcd0ff2812739773.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
HemulGM, 2021-11-28
@HemulGM

Run bot code in different threads. Python is so-so with this. So don't be foolish, run two processes.

R
rPman, 2021-11-28
@rPman

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()

see the code of the _process_polling method, and create your own pooling_step only it should be just in the original inside the while loop, something like
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)

it is not clear why the creators of the library did not do it themselves, perhaps they did not have time yet, or I did not understand their code correctly?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question