TelegramBots icon indicating copy to clipboard operation
TelegramBots copied to clipboard

Example for AbilityWebHookBot.

Open sapelkinAV opened this issue 5 years ago • 3 comments

Hi I'm trying to make serverless bot, and I can't use standard long polling. I can't configure webhook bot. Can someone help with example?)

I've already tried to launch it with ngrok locally, and I see, that telegram sends post requests. But bot didn't handle it.

I'm using spring boot, and ability version of bot.

Thank you for help)

sapelkinAV avatar Apr 25 '19 10:04 sapelkinAV

I found concrete problem, but I don't know how to fix it.

public void registerBot(WebhookBot bot) throws TelegramApiRequestException {
 if (useWebhook) {
            webhook.registerWebhook(bot);
            bot.setWebhook(externalUrl + bot.getBotPath(), pathToCertificate);
        }
  }

Even after I've extended from AbilityWebhookBot, useWebhook still false and it's not register it at all

sapelkinAV avatar Apr 25 '19 15:04 sapelkinAV

Maybe it's late but i will leave an usage example:`

    AbilityWebhookBot bot = new MyAbilityWebhookBot();
    
    try {
        
        Webhook webhook = new DefaultWebhook();
        //this is the URL where the application will receive requests
        webhook.setInternalUrl("http://0.0.0.0:8081/");

        TelegramBotsApi botsApi = new TelegramBotsApi(DefaultBotSession.class, webhook);
        
        SetWebhook setWebhook = new SetWebhook();
        //this is the public URL accessed by Telegram servers
        //it must be reverse proxied to the internalUrl if they are different
        setWebhook.setUrl("https://public.domain.com/");
        
        botsApi.registerBot(bot, setWebhook);
        
    } catch (TelegramApiException e) {
        e.printStackTrace();
    }

    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            try {
                System.out.println("Removing the webhook...");
                WebhookUtils.clearWebhook(bot);
            } catch (TelegramApiException e) {
                e.printStackTrace();
            }
        }
    });

`

pafgoncalves avatar Nov 20 '21 23:11 pafgoncalves

Maybe it's late but i will leave an usage example:`

    AbilityWebhookBot bot = new MyAbilityWebhookBot();
    
    try {
        
        Webhook webhook = new DefaultWebhook();
        //this is the URL where the application will receive requests
        webhook.setInternalUrl("http://0.0.0.0:8081/");

        TelegramBotsApi botsApi = new TelegramBotsApi(DefaultBotSession.class, webhook);
        
        SetWebhook setWebhook = new SetWebhook();
        //this is the public URL accessed by Telegram servers
        //it must be reverse proxied to the internalUrl if they are different
        setWebhook.setUrl("https://public.domain.com/");
        
        botsApi.registerBot(bot, setWebhook);
        
    } catch (TelegramApiException e) {
        e.printStackTrace();
    }

    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            try {
                System.out.println("Removing the webhook...");
                WebhookUtils.clearWebhook(bot);
            } catch (TelegramApiException e) {
                e.printStackTrace();
            }
        }
    });

`

Thanks to your code I managed to successfully initialize a Webhook bot. Keep in mind that the updates will be received at https://public.domain.com/callback/{botPath}, where botPath is the one defined into the class extending TelegramWebhookBot.

However, even though the library defines the enpoint for handling the web hook updates, it did not work for me. So I had to define my POST request handler for /callback/{botPath} and "manually" call onWebhookUpdateReceived method.

gturi avatar May 17 '22 20:05 gturi