skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
  3. 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')
    
    Login or Signup to reply.
  4. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search