I am building a Telegram Bot now and are testing it manually with a Telegram Client.
Is there a way I can send client messages in the same way I can build bots?
I know that I could build unit-tests in the code, that is not what I am looking for.
I am building a Telegram Bot now and are testing it manually with a Telegram Client.
Is there a way I can send client messages in the same way I can build bots?
I know that I could build unit-tests in the code, that is not what I am looking for.
4
Answers
I asked the same question and did not find an answer. So I made two libraries for testing telegram bots:
node-telegram-bot-api
. It catches bot requests and allows to pretend that we have a valid answer from client.Both projects are in deep alpha version for now but I haven’t seen anything better. You can read an article about those projects (in Russian) here.
Alternatively you can use an MTProto Telegram user account library like MadelineProto in PHP, Telethon and Pyrogram in Python 3. You can use these library to automate an user account to test your bot.
Just to note that you might hit the flood ban if you send message too frequently.
This can be done via Telegram API (not the same as Telegram Bot API) which is basically an API to build your own Telegram client. To start with it, you should register an "app" and then you can login to your app like you do with any other client.
Now, probably you shouldn’t dig the API itself, use a library instead to save time. The most popular one is Telethon and you can find various examples of its usage. Depeding on what language you use to create a bot you may want to use some other libraries, but most of dedicated ones are actually wrappers of Telethon, an example is Gramjs (but I recommend to try Telethon first, since its docs are more dedicated and it’s easier to google use cases for it).
Still, you should remember that this approach is ok for unit-tests involving just one user (you can login using your accound), awkward for multi-user scenarios and is not suitable for highload testing.
Here’s a simple example of what you can do with Telethon:
I wrote unit-test framework for python-telegram-bot to do smth like this:
https://github.com/dontsovcmc/telegram-bot-unittest
I create web server and call tested bot with it’s local url. Now I can send messages and check answers.