skip to Main Content

I want a Telegram bot notify me when a user do some action in my website. I have been testing Telegram bots to send messages and using getUpdates via polling to receive, and everything works perfectly. I have read than the polling method consume more CPU than webhooks (because it is constantly checking for new messages), but this is more complicated to implement, so I discard webhooks.

Actually, I don’t need to use the polling or webhooks, because what I want is to send messages, but I have to implement the getUpdates method compulsory. Is there any way to use only the send message function and avoid the receive messages? Like a only-read bot or a telegram channel.

Thanks!

EDIT. Here is my code in Java:

public class TelegramBot extends TelegramLongPollingBot {

    @Override
    public void onUpdateReceived(Update update) {
        // Stuff when the bot receive a message
        // I don't need this method, but it is compulsory to implement it
    }

    public synchronized void sendMsg(String msg) {
        // Stuff to send a message 
        // Here goes the code I only need
    }

    @Override
    public String getBotUsername() {
        // Compulsory to implement
        return "my_bot_user_name";
    }

    @Override
    public String getBotToken() {
        // Compulsory to implement
        return "my_token";
    }
}

2

Answers


  1. Chosen as BEST ANSWER

    I have solved all using Jersey WS (jersey-client and jersey-common packages)

    This is how my code looks now, just calling it when I need it and not polling at all:

    private static int sendMessage(String message) {
        HttpResponse<String> response = null;
        
        HttpClient client = HttpClient.newBuilder()
                .connectTimeout(Duration.ofSeconds(5))
                .version(HttpClient.Version.HTTP_2)
                .build();
    
        UriBuilder builder = UriBuilder
                .fromUri("https://api.telegram.org")
                .path("/{token}/sendMessage")
                .queryParam("chat_id", CHAT_ID)
                .queryParam("text", message)
                .queryParam("parse_mode", "html");
    
        HttpRequest request = HttpRequest.newBuilder()
                .GET()
                .uri(builder.build("bot" + TELEGRAM_TOKEN))
                .timeout(Duration.ofSeconds(5))
                .build();
        try {
            response = client.send(request, HttpResponse.BodyHandlers.ofString());
        }
        catch(IOException | InterruptedException ex) {
            LOGGER.warn("Error sending message to Telegram Bot");
        }
    
        return (response != null) ? response.statusCode() : -1;
    }
    

  2. It is possible to configure and start the chatbot without actually performing the polling

     updater = Updater('token', use_context=True)
    
     dp = updater.dispatcher
     updater.idle()
    
     updater.bot.send_message(chat_id='YYYY', text='hoi')
    

    In this case there is no polling but a message is sent to the chat.

    Notice:

    • you need to know the chat id
    • updater.idle() is needed only if you need keep running the chatbot (otherwise you can just send the message(s) and shutdown the application)
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search