Use Telegram client for bot testing (not bot api)
Asked Answered
A

4

16

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.

Adjudication answered 27/4, 2016 at 13:34 Comment(3)
You could probably change the base url (instead https://api.telegram.org your own server), and reply with the right stuff.Mike
The problem is that there is no Client API and Bots can't initiate conversations, so I don't see how this would work.Adjudication
Basically, you replicate the Telegram API.Mike
N
18

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.

Notorious answered 27/2, 2017 at 15:14 Comment(0)
G
5

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')
Grazynagreabe answered 4/10, 2021 at 9:10 Comment(0)
Q
4

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.

Quintero answered 25/12, 2019 at 15:53 Comment(0)
J
1

I wrote unit-test framework for python-telegram-bot to do smth like this:

def test_echobot_message(bot, user):
    user.send_message('testing message')
    message = user.get_message()
    assert message['text'] == 'testing message'

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.

Joly answered 13/4, 2022 at 0:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.