I was wondering how to deploy multiple telegram bot on a single Heroku application. I have tried to deploy two telegram bot on a single Heroku app by using python oop (bot class) and each bot was initiated using threading. I also used webhook to fetch updates. The problem is only one bot was working.
If I use pooling both of the bots work for a while before the app crash. I also have tried using multiprocessing instead of Threading but it is still the same. Base on the log, it appears that the webhook fetches the update but the dispatcher was not able to send messages. I wonder if there's a simple solution to these problems because I'm fairly new to python and programming overall.
the program was something like this:
def main():
hello2 = Test.Type1("hello", "343241", "Bot Token")
hello = Test.Type2("hello", "343241", "Bot Token")
#p1 = threading.Thread(target=hello2.update_getter)
#p2 = threading.Thread(target=hello.update_getter)
p1 = multiprocessing.Process(target=hello2.update_getter)
p2 = multiprocessing.Process(target=hello.update_getter)
p1.start()
p2.start()
if __name__ == '__main__':
main()
and the bot class was something like this:
class Bot():
def __int__(self, bot_name, bot_id, bot_api_token):
super()
self.updater = Updater(token=bot_api_token,
persistence=PicklePersistence(filename='bot_data'))
self.bot = telegram.Bot(token=bot_api_token)
self.BotName = bot_name
self.BotID = bot_id
self.BotAPIToken = bot_api_token
self.Port = int(os.environ.get('PORT', 5000))
def get_BotName(self):
return self.BotName
def get_BotID(self):
return self.BotID
def get_BotAPIToken(self):
return self.BotAPIToken
def get_Port(self):
return self.Port
class Type1(Bot):
def __init__(self,bot_name, bot_id, bot_api_token):
super().__int__(bot_name, bot_id, bot_api_token)
def start(self,update,context):
self.bot.sendMessage(chat_id=update.effective_chat.id, text="Hello")
def update_getter(self):
self.updater.dispatcher.add_handler(CommandHandler('start', self.start))
self.updater.start_webhook(listen="0.0.0.0", port=int(self.get_Port()), url_path=self.get_BotAPIToken())
self.updater.bot.setWebhook('https://hidden-falls-12181.herokuapp.com/' + self.get_BotAPIToken())
#self.updater.start_polling()
self.updater.idle()
class Type2(Bot):
def __init__(self,bot_name, bot_id, bot_api_token):
super().__int__(bot_name, bot_id, bot_api_token)
def start(self,update,context):
print("Hello")
self.bot.sendMessage(chat_id=update.effective_chat.id, text="Hi")
def update_getter(self):
self.updater.dispatcher.add_handler(CommandHandler('start', self.start))
self.updater.start_webhook(listen="0.0.0.0", port=int(self.get_Port()), url_path=self.get_BotAPIToken())
self.updater.bot.setWebhook('https://hidden-falls-12181.herokuapp.com/' + self.get_BotAPIToken())
#self.updater.start_polling()
self.updater.idle()