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.
If you have the token of the bot, you can directly call the Telegram Bot API using your browser or any other program that allows you to perform requests on the web. But you would not be able to see what the bot answers since you can not access the messages written by the bot via the API.
Telegram API This API allows you to build your own customized Telegram clients. It is 100% open for all developers who wish to create Telegram applications on our platform.
Is there a way I can send client messages in the same way I can build bots?
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:
from telethon import TelegramClient
# you will get these when registering your client at https://my.telegram.org/apps
api_id = 12345
api_hash = '0123456789abcdef0123456789abcdef'
client = TelegramClient('arbitrary_session_name', api_id, api_hash)
client.start()
print(client.get_me().stringify())
client.send_message('username', 'Hello! Talking to you from Telethon')
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With