无法从Telegram webHook ASP.NET接收传入的JSON [英] Cannot receive incoming JSON from Telegram webHook ASP.NET

查看:239
本文介绍了无法从Telegram webHook ASP.NET接收传入的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以问题是我为我的Telegram机器人设置了一个webHook地址,如下所示: https://api.telegram.org/bot<TOKEN>/setWebHook?url=https://evolhookah.com/Home/ReceiveWebhook

So the problem is that I have set a webHook address for my Telegram bot like this: https://api.telegram.org/bot<TOKEN>/setWebHook?url=https://evolhookah.com/Home/ReceiveWebhook

此后,我收到了JSON格式的确认,指出从现在开始,Telegram将向该URL发送消息.

Afterwards, I received confirmation in JSON format stating that from now Telegram will send messages to that URL.

然后,我创建了一个方法ReceiveWebhook(),该方法负责处理传入的请求,该方法以前看起来像这些方法(没有一个起作用):

Then I created a method ReceiveWebhook(), which is responsible for handling incoming requests, which used to look like these methods (none of them worked):

    public ActionResult ReceiveJSON(int? id)
    {
        Stream req = Request.InputStream;
        req.Seek(0, System.IO.SeekOrigin.Begin);
        string receivedJson = new StreamReader(req).ReadToEnd();
        var bs = new TgDesserialize(); //Class containing JSON desserializing function
        PeopleAttributes people = bs.desserializer(receivedJson); //desserializer manages with desserializing received JSON and returns an object people filled up with necessary values
        return Content(people.firstName);
    }

不幸的是,关于流的想法不起作用,然后我决定以字符串形式接收传入的JSON,这是这样的:

Unfortunately idea with a stream did NOT work, then I decided to receive incoming JSON as a string, and this is how it looked like:

    public ActionResult JSONString(String receivedJSON)
    {
        var bs = new TgDesserialize(); 
        PeopleAttributes people = bs.desserializer(receivedJSON); 
        return Content(people.firstName);
    }

问题:每次我收到一个Webhook时,要么获取null JSON,要么无法在控制器中正确接收它.

Problem: Everytime I receive a webhook, I either get null JSON or cannot receive it properly in the controller.

问题:

  1. 在发送webHook时,有什么方法可以检查Telegram是否在发送带有数据的JSON?
  2. 为什么我总是得到NULL,而https://api.telegram.org/bot<TOKEN>/getUpdates显示我在JSON中有数据?
  3. 我是否在控制器中以错误的方式接收了我的JSON?如果是,处理传入JSON的最佳实践是什么?
  1. Is there any possible way I can check that Telegram is sending JSON with data inside while sending a webHook?
  2. Why do I always get NULLs, while https://api.telegram.org/bot<TOKEN>/getUpdates shows that I have data inside JSON?
  3. Am I receiving my JSON in a wrong way in a controller? If yes, what is the best practice of handling incoming JSON?

推荐答案

首先,我建议您使用 telegram.bot 库,并在此示例中查找

First of all, I suggest you to using telegram.bot library and looking this example for Webhook. But in general:

  1. 安装 Postman 并将手动消息(如电报Webhook消息)发布到您的控制器.然后确保您的控制器中没有错误.
  1. install the Postman and post a manual message like the telegram webhook message to your controller. Then make sure there are no bugs in your controller.

邮递员样本:

{
    "update_id":10000,
    "message":{
      "date":1441645532,
      "chat":{
         "last_name":"Test Lastname",
         "id":1111111,
         "first_name":"Test",
         "username":"Test"
      },
      "message_id":1365,
      "from":{
         "last_name":"Test Lastname",
         "id":1111111,
         "first_name":"Test",
         "username":"Test"
      },
      "text":"/start"
    }

  1. 如果您的控制器为true,则必须确保Webhook消息是由Telegram发送的.您可以下载 ngrok 并为您的本地主机创建一个https代理.
  1. if your controller is true you must make sure that the Webhook message is sent by Telegram. You can download ngrok and create a https proxy to your localhost.

在ngrok中使用此命令:

Use this command in ngrok:

ngrok http 20201

20201是您的本地主机端口(localhost:20201). 现在ngrok给您一个https链接,并且您必须将该链接设置为电报webhook(就像您所说的一样). 此时,如果Telegram为您的机器人发送了一个Webhook消息,那么您可以在本地主机中对其进行调试.

20201 is your localhost port (localhost:20201). Now ngrok give you a https link, and you must set that link as your telegram webhook (just like the way you said). At this moment if telegram sends a webhook message for your bot then you can debug it in the localhost.

  1. 最后,如果您没有找到问题,则必须阅读 Marvin的《万物专利挂号指南》 Webhook 再次检查所有要求.
  1. Finally, if you do not find the problem you must read the Marvin's Patent Pending Guide to All Things Webhook to check all the requirements again.

  1. 支持IPv4,Webhooks当前不支持IPv6.
  2. 在端口443、80、88或8443上接受来自149.154.167.197-233的传入POST.
  3. 能够处理TLS1.0 + HTTPS流量.
  4. 提供受支持的,非通配符,已验证或自签名的证书.
  5. 使用与您在设置时提供的域匹配的CN或SAN.
  6. 提供所有中间证书以完成验证链.
  1. Supports IPv4, IPv6 is currently not supported for Webhooks.
  2. Accepts incoming POSTs from 149.154.167.197-233 on port 443,80,88 or 8443.
  3. Is able to handle TLS1.0+ HTTPS-traffic.
  4. Provides a supported,non-wildcard, verified or self-signed certificate.
  5. Uses a CN or SAN.that matches the domain you’ve supplied on setup.
  6. Supplies all intermediate certificates to complete a verification chain.

这篇关于无法从Telegram webHook ASP.NET接收传入的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆