3
votes

I'm working with telegram bot api, and I want to send messages to user in a determined time, but the bot needs to recipt an "message" to send something, my question is: Is possible to send an Update simulating the user interactions?

I means something like this: Here I create the update to simulate an user interaction (sendUpdate) is a custom method just for example this doesn't works actually

public void sendUpdate() {
    //sending the update to simulate user interaction
    Update upd = new Update();

    //method that telegram bot api uses to reply when you send a message to the bot
    onUpdateReceived(upd);
}

@Override
//Here I want to recipt my update to simulate the user interaction, and send a message witout user input
public void onUpdateReceived(Update update) {
    System.out.println(update);
    LOGGER.setLevel(Level.ALL);
    LOGGER.addHandler(new ConsoleHandler());

    LOGGER.info("2");
    if (update.hasMessage() && update.getMessage().hasText()) {
        // Set variables
        String message_text = "Message";
        long chat_id = update.getMessage().getChatId();
        SendMessage message = new SendMessage() 
                .setChatId(chat_id)
                .setText(message_text);
        try {
            this.sendMessage(message); 
        } catch (TelegramApiException e) {
            e.printStackTrace();
        }
    }
}
1
Why do you say you need to receive a message to send a message? Why can't you just call this.sendMessage(message); - shazin
Because I'm stupid haha :( , you're right, I had no idea that just using this.sendMessage (message) would work, I thought I had to use the onUpdateReceived method, thank you very much! - Csanchez

1 Answers

3
votes

If you want to test your API webhook and simulate user interaction, I prefer to do mock the whole POST request to your webhook URI (the URI, where is your Telegram Bot listening, and receiving updates from Telegram).

You can use any tool you want, I am using for example Fiddler (tab Composer). Inside your body, you will put JSON (it will be transformed to yourUpdate object)

Method Type: POST

Domain: https://whereyourwebhookislistening.com

Headers: content-type: application/json

Request Body for example:

{
   "update_id":123456789,
   "message":{
      "message_id":123,
      "from":{
         "id":123456789,
         "is_bot":false,
         "first_name":"Test",
         "language_code":"ru-RU"
      },
      "date":1517384207,
      "chat":{
         "id":123456789,
         "type":"private",
         "first_name":"Testr",
         "all_members_are_administrators":false,
      },
      "forward_from_message_id":0,
      "text":"Test text",
      "delete_chat_photo":false,
      "group_chat_created":false,
      "supergroup_chat_created":false,
      "channel_chat_created":false,
      "migrate_to_chat_id":0,
      "migrate_from_chat_id":0,
   },
}

With this approach, you can simulate real webhook call from Telegram Bot. Adding screenshot of example request:

Fiddler composed POST request to webhook