O
O
oleg_ka2016-10-20 23:41:17
C++ / C#
oleg_ka, 2016-10-20 23:41:17

How to get an update from a Telegram bot using WebHook?

There is a telegram bot implemented in C# that receives updates using the getUpdates method as follows:

string response = webClient.DownloadString("https://api.telegram.org/bot" + token + "/getUpdates" + "?offset=" + (lastUpdateId + 1));
                var n = JSON.Parse(response);
                foreach (JSONNode jr in n["result"].AsArray)
                {
                    lastUpdateId = jr["update_id"].AsInt;
                    newmess = jr["message"]["text"];
                    chat_id = jr["message"]["chat"]["id"].AsInt;
                 }

Tell me how to implement the update using a webhook? And what is required for this?
I looked through many articles, and could not figure it out. I hope for your help.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anatoliy Mikhailov, 2016-10-21
@yamaoto

1) You create a project on WebApi2 or MVC 5, or on a brand new asp net core
2) You create data model classes that will come from the telegram
3) You create a controller and an action in it that will receive an incoming request from telegrams
something like this:

//пример для WebApi2:
//телеграм не дает возможности как-то проверять что запрос пришел от самого телеграма
//так что в официальном факе рекомендовано создавать сложный адрес
//так как такой адрес известен только разработчику и телеграму
//никто не сможет подделать запросы
[RoutePrefix("api/myveryawesomebot123802539483")]
public MyTelegramBotController:ApiController
{
    [HttpPost]
    [Route("sdfsdf2342f2q")]
    public void Update([FromBody] UpdateModel model)
     {
          ...
     }
}

I recommend hosting a web project on Azure - it will make it possible to place your bot in a data center for FREE with all protection + a free and immediately valid SSL certificate + remote debugging capabilities in VS

N
nllm, 2016-10-20
@nllm

With a webhook, telegram servers themselves send https requests with data. Those. approach is different. Now you have this: the program makes requests for updates and immediately responds. With a webhook, it will be like this: processing requests separately, processing responses separately
What is required:
1. access from outside to the bot scripts to receive an https request
2. ssl certificate (self-generated, received for free or purchased)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question