Core: Does this library support multi-bots?

Created on 8 Jan 2017  路  13Comments  路  Source: php-telegram-bot/core

I'm developing an application with multi bots running at the same time. I've noticed that this library stores all states in the db tables without indicating the Bot associated with it, for e.g. the 'Conversation' table.

Is this bot compatible with multi-bots? what happens the same command issued to different bots by the same user?

Most helpful comment

You can use different hook.php and same codebase. Just change SQL connection information info in hook.php for a bot.

All 13 comments

You can use different hook.php and same codebase. Just change SQL connection information info in hook.php for a bot.

Maybe it comes to the point that I've 1000s of bots or bots on demand. Its not a good idea to use a separate database for each bot.

@ahmadazizi You can have a single database with different tables, just set the table prefix when enabling the MySQL connection ($telegram->enableMySql($mysql_credentials, $BOT_NAME . '_');).
Because the framework is very static at the moment and only 1 bot can run in parallel within the same PHP process, you can only use multiple bots if you run multiple instances of the code. Hopefully this will change in the future.

@jacklul What do you think about having the bot linked in the tables too to allow multiple bots use the same tables? We could add a new table called bots and just store bot_name and an ID (and possible open up the world of bot settings here!)

Adding multibots suport would I believe change db schema for all tables, alternatively we could have second DB handler build for multibots and primary one for one bot only. This could open up a road for supporting different DB types.

_Also, why not changing $BOT_NAME to $BOT_USERNAME ? Makes more sense and can prevent confusion!_

Thanks @noplanman, this approach would be fine for just a few bots but is absolutely an abnormal database scheme; just suppose a platform that lets users to add their own bot API KEY and they want each user receive updates with their own bot because of telegram limits or other considerations.
I really believe that this framework is strong enough to be confidently used in large-scale enterprise applications but the lack of multibot support is the only drawback.

@jacklul I wont believe this could be so complicated. Just adding the BOT_USERNAME to all tables and considering this in queries would be enough. For simplicity I would use part of bot API KEY as BOT_USERNAME

For me I've to say thats absolutely is possible to run multiple bots without any changes to the code with the same instances. You dont neet do double hook.php or something else, you only need some config arrays and a unique get parameter in the url. You even can handle the messages per bot with only one extra database table where you store the botid with the message ids which he has received. There is absolutely no need to change the entire library for that. My bots can even handle an unlimited number of different bot subscriptions for the same user without any problem. You need for sure some lines of own code and some extra database tables, but it's working perfectly.

@tchibomann can you give some code samples how you've implemented this?

@ahmadazizi I've build this on a very simple basis, I have to recode this because I currently have this splitted into multiple hooks. This was my first way for using this script for multiple bots. But I did it with the intention to simplify this into one file. Maybe I have next week some time to finalize and show what I mean.

@tchibomann so that will be great and so appreciated if you show us how you did.

Just adding the BOT_USERNAME to all tables

@ahmadazizi I don't think that's a good idea, as there would be a whole bunch of redundant data. Better to have a separate table called bots, which have an ID, name, maybe some settings too. That is also easier if someone chooses to rename their bot but want to keep the data.

@tchibomann Looking forward to learn from your solution 馃憤

I don't think that's a good idea, as there would be a whole bunch of redundant data.

@noplanman You're right, I was looking at it as some temporary data tables.
Are you planning to implement this feature? and if yes does it involve major changes in schema or it would be a quick enhancement update?

@noplanman @ahmadazizi that was my idea, reep away redundances. So my idea was a separate table for bots and a link-table for bots and messages, but last may be inefficient, needs unnecessary more space. Better is a new column for the bot id in all necessary tables.

But I'll show my solution in some days :)

You can create also database on demand if you really need to create on the fly new bots. Is the easiest thing. Is to much complicated add a field with the botname

Was this page helpful?
0 / 5 - 0 ratings

Related issues

NabiKAZ picture NabiKAZ  路  3Comments

Zoha picture Zoha  路  3Comments

noplanman picture noplanman  路  3Comments

Bl0ck154 picture Bl0ck154  路  3Comments

esomkin picture esomkin  路  3Comments