Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Telegram client for bot testing (not bot api)

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.

like image 263
Punnerud Avatar asked Apr 27 '16 13:04

Punnerud


People also ask

How can I use Telegram bot without Telegram?

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.

Is Telegram an 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.


3 Answers

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')
like image 129
YakovL Avatar answered Sep 25 '22 06:09

YakovL


I asked the same question and did not find an answer. So I made two libraries for testing telegram bots:

  1. telegram-test can be used if you made bot using node-telegram-bot-api. It catches bot requests and allows to pretend that we have a valid answer from client.
  2. telegram-test-api can be used with any bot and any technological stack. It is a web server which emulates Telegram API. You can make client requests to it using any client, protocol is very simple.

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.

like image 39
Jehy Avatar answered Sep 25 '22 06:09

Jehy


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.

like image 25
Eana Hufwe Avatar answered Sep 25 '22 06:09

Eana Hufwe