Answer the question
In order to leave comments, you need to log in
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;
}
Answer the question
In order to leave comments, you need to log in
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)
{
...
}
}
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 questionAsk a Question
731 491 924 answers to any question