C# 无法从电报webHook ASP.NET接收传入的JSON

C# 无法从电报webHook ASP.NET接收传入的JSON,c#,asp.net,json,telegram,C#,Asp.net,Json,Telegram,所以问题是我为我的电报机器人设置了一个webHook地址,如下所示: https://api.telegram.org/bot/setWebHook?url=https://evolhookah.com/Home/ReceiveWebhook 之后,我收到了JSON格式的确认,声明从现在起电报将向该URL发送消息 然后我创建了一个方法ReceiveWebhook(),该方法负责处理传入的请求,该方法过去看起来像这些方法(它们都不起作用): 不幸的是,使用流的想法不起作用,然后我决定以字符串形式接

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

之后,我收到了JSON格式的确认,声明从现在起电报将向该URL发送消息

然后我创建了一个方法ReceiveWebhook(),该方法负责处理传入的请求,该方法过去看起来像这些方法(它们都不起作用):

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

    public ActionResult JSONString(String receivedJSON)
    {
        var bs = new TgDesserialize(); 
        PeopleAttributes people = bs.desserializer(receivedJSON); 
        return Content(people.firstName);
    }
问题:每次我收到webhook时,我要么得到空JSON,要么无法在控制器中正确接收它

问题:

  • 在发送webHook时,是否有任何可能的方法可以检查电报是否发送包含数据的JSON
  • 为什么我总是得到空值,而
    https://api.telegram.org/bot/getUpdates
    显示我在JSON中有数据
  • 我是否在控制器中以错误的方式接收JSON?如果是,处理传入JSON的最佳实践是什么
    您需要基于来自telegram的JSON创建一个类

    例如:

    电报JSON:

    { 
        "update_id": 1
        "message": { ... fill in message json here},
        .... other properties here
    }
    
    然后创建一个类:

    public class TelegramUpdate
    {
        public int update_id {get; set;}
        public Message message {get; set;}
        // other properties here
    }
    
    public class Message
    {
        // message properties here
    }
    // ... More nested classes go here
    
     public class From
    {
        public int id { get; set; }
        public bool is_bot { get; set; }
        public string first_name { get; set; }
        public string username { get; set; }
        public string language_code { get; set; }
    }
    
    public class Chat
    {
        public long id { get; set; }
        public string title { get; set; }
        public string type { get; set; }
        public string username { get; set; }
    }
    
    public class ForwardFromChat
    {
        public long id { get; set; }
        public string title { get; set; }
        public string type { get; set; }
    }
    
    public class Document
    {
        public string file_name { get; set; }
        public string mime_type { get; set; }
        public string file_id { get; set; }
        public int file_size { get; set; }
    }
    public class Audio
    {
        public int duration { get; set; }
        public string mime_type { get; set; }
        public string title { get; set; }
        public string performer { get; set; }
        public string file_id { get; set; }
        public int file_size { get; set; }
    }
    public class Photo
    {
        public string file_id { get; set; }
        public int file_size { get; set; }
        public int width { get; set; }
        public int height { get; set; }
    }
    public class Video
    {
        public int duration { get; set; }
        public int width { get; set; }
        public int height { get; set; }
        public string mime_type { get; set; }
        public Thumb thumb { get; set; }
        public string file_id { get; set; }
        public int file_size { get; set; }
    }
    public class Thumb
    {
        public string file_id { get; set; }
        public int file_size { get; set; }
        public int width { get; set; }
        public int height { get; set; }
    }
    public class Message
    {
        public int message_id { get; set; }
        public From from { get; set; }
        public Chat chat { get; set; }
        public int date { get; set; }
        public string text { get; set; }
    }
    
    public class CaptionEntity
    {
        public int offset { get; set; }
        public int length { get; set; }
        public string type { get; set; }
    }
    
    public class EditedChannelPost
    {
        public int message_id { get; set; }
        public Chat chat { get; set; }
        public int date { get; set; }
        public int edit_date { get; set; }
        public string caption { get; set; }
        public List<CaptionEntity> caption_entities { get; set; }
        public string text { get; set; }
        public Document document { get; set; }
        public Video video { get; set; }
        public Audio audio { get; set; }
        public List<Photo> photo { get; set; }
    }
    public class ChannelPost
    {
        public int message_id { get; set; }
        public Chat chat { get; set; }
        public int date { get; set; }
        public ForwardFromChat forward_from_chat { get; set; }
        public int forward_from_message_id { get; set; }
        public string forward_signature { get; set; }
        public int forward_date { get; set; }
        public string caption { get; set; }
        public string text { get; set; }
        public List<CaptionEntity> caption_entities { get; set; }
        public Document document { get; set; }
        public Video video { get; set; }
        public Audio audio { get; set; }
        public List<Photo> photo { get; set; }
    }
    
    public class RootObject
    {
        public long update_id { get; set; }
        public ChannelPost channel_post { get; set; }
        public EditedChannelPost edited_channel_post { get; set; }
        public Message message { get; set; }
    }
    
    专业提示:如果您有一个示例JSON文件,您可以复制它,获得VisualStudio=>Edit=>Paste Special=>Paste JSON as class。这将为您生成类

    然后可以将此类作为参数添加到webhook:

    public ActionResult JSONString(TelegramUpdate update)
    {
        return Content(update.message.from.first_name);
    }
    

    您需要基于来自telegram的JSON创建一个类

    例如:

    电报JSON:

    { 
        "update_id": 1
        "message": { ... fill in message json here},
        .... other properties here
    }
    
    然后创建一个类:

    public class TelegramUpdate
    {
        public int update_id {get; set;}
        public Message message {get; set;}
        // other properties here
    }
    
    public class Message
    {
        // message properties here
    }
    // ... More nested classes go here
    
     public class From
    {
        public int id { get; set; }
        public bool is_bot { get; set; }
        public string first_name { get; set; }
        public string username { get; set; }
        public string language_code { get; set; }
    }
    
    public class Chat
    {
        public long id { get; set; }
        public string title { get; set; }
        public string type { get; set; }
        public string username { get; set; }
    }
    
    public class ForwardFromChat
    {
        public long id { get; set; }
        public string title { get; set; }
        public string type { get; set; }
    }
    
    public class Document
    {
        public string file_name { get; set; }
        public string mime_type { get; set; }
        public string file_id { get; set; }
        public int file_size { get; set; }
    }
    public class Audio
    {
        public int duration { get; set; }
        public string mime_type { get; set; }
        public string title { get; set; }
        public string performer { get; set; }
        public string file_id { get; set; }
        public int file_size { get; set; }
    }
    public class Photo
    {
        public string file_id { get; set; }
        public int file_size { get; set; }
        public int width { get; set; }
        public int height { get; set; }
    }
    public class Video
    {
        public int duration { get; set; }
        public int width { get; set; }
        public int height { get; set; }
        public string mime_type { get; set; }
        public Thumb thumb { get; set; }
        public string file_id { get; set; }
        public int file_size { get; set; }
    }
    public class Thumb
    {
        public string file_id { get; set; }
        public int file_size { get; set; }
        public int width { get; set; }
        public int height { get; set; }
    }
    public class Message
    {
        public int message_id { get; set; }
        public From from { get; set; }
        public Chat chat { get; set; }
        public int date { get; set; }
        public string text { get; set; }
    }
    
    public class CaptionEntity
    {
        public int offset { get; set; }
        public int length { get; set; }
        public string type { get; set; }
    }
    
    public class EditedChannelPost
    {
        public int message_id { get; set; }
        public Chat chat { get; set; }
        public int date { get; set; }
        public int edit_date { get; set; }
        public string caption { get; set; }
        public List<CaptionEntity> caption_entities { get; set; }
        public string text { get; set; }
        public Document document { get; set; }
        public Video video { get; set; }
        public Audio audio { get; set; }
        public List<Photo> photo { get; set; }
    }
    public class ChannelPost
    {
        public int message_id { get; set; }
        public Chat chat { get; set; }
        public int date { get; set; }
        public ForwardFromChat forward_from_chat { get; set; }
        public int forward_from_message_id { get; set; }
        public string forward_signature { get; set; }
        public int forward_date { get; set; }
        public string caption { get; set; }
        public string text { get; set; }
        public List<CaptionEntity> caption_entities { get; set; }
        public Document document { get; set; }
        public Video video { get; set; }
        public Audio audio { get; set; }
        public List<Photo> photo { get; set; }
    }
    
    public class RootObject
    {
        public long update_id { get; set; }
        public ChannelPost channel_post { get; set; }
        public EditedChannelPost edited_channel_post { get; set; }
        public Message message { get; set; }
    }
    
    专业提示:如果您有一个示例JSON文件,您可以复制它,获得VisualStudio=>Edit=>Paste Special=>Paste JSON as class。这将为您生成类

    然后可以将此类作为参数添加到webhook:

    public ActionResult JSONString(TelegramUpdate update)
    {
        return Content(update.message.from.first_name);
    }
    

    首先,我建议您使用这个库并查看这个示例。 但总的来说:

  • 安装并向控制器发布手动消息,如Telegrame webhook消息。然后确保控制器中没有bug
  • 邮递员样本:

    {
        "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"
        }
    

  • 如果控制器为true,则必须确保Webhook消息通过电报发送。您可以下载并创建https代理到本地主机
  • 在ngrok中使用此命令:

    ngrok http 20201
    
    20201是您的本地主机端口(localhost:20201)。 现在ngrok给你一个https链接,你必须把这个链接设置成你的电报webhook(就像你说的那样)。 此时,如果telegram为您的bot发送webhook消息,那么您可以在本地主机中调试它

  • 最后,如果您没有发现问题,则必须阅读以再次检查所有需求
  • 支持IPv4,Webhook当前不支持IPv6
  • 接受来自端口443、80、88或8443上149.154.167.197-233的传入邮局
  • 能够处理TLS1.0+HTTPS流量
  • 提供受支持的、非通配符的、经过验证的或自签名的证书
  • 使用与安装程序中提供的域匹配的CN或SAN
  • 提供所有中间证书以完成验证链

  • 首先,我建议您使用这个库并查看这个示例。 但总的来说:

  • 安装并向控制器发布手动消息,如Telegrame webhook消息。然后确保控制器中没有bug
  • 邮递员样本:

    {
        "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"
        }
    

  • 如果控制器为true,则必须确保Webhook消息通过电报发送。您可以下载并创建https代理到本地主机
  • 在ngrok中使用此命令:

    ngrok http 20201
    
    20201是您的本地主机端口(localhost:20201)。 现在ngrok给你一个https链接,你必须把这个链接设置成你的电报webhook(就像你说的那样)。 此时,如果telegram为您的bot发送webhook消息,那么您可以在本地主机中调试它

  • 最后,如果您没有发现问题,则必须阅读以再次检查所有需求
  • 支持IPv4,Webhook当前不支持IPv6
  • 接受来自端口443、80、88或8443上149.154.167.197-233的传入邮局
  • 能够处理TLS1.0+HTTPS流量
  • 提供受支持的、非通配符的、经过验证的或自签名的证书
  • 使用与安装程序中提供的域匹配的CN或SAN
  • 提供所有中间证书以完成验证链
  • [HttpPost]
    公共异步任务索引()
    {
    尝试
    {
    var req=Request.InputStream;//从电报获取请求
    var responseString=new StreamReader(req).ReadToEnd();//读取请求
    RootObject ro=JsonConvert.DeserializeObject(responseString);
    }
    捕获(例外情况除外)
    {
    返回内容(“错误”);
    }
    //此代码提示电报Ttat,我收到消息,不需要再次发送此消息
    返回内容(“{\'ok\':true}”);
    }
    
    然后创建一个类:

    public class TelegramUpdate
    {
        public int update_id {get; set;}
        public Message message {get; set;}
        // other properties here
    }
    
    public class Message
    {
        // message properties here
    }
    // ... More nested classes go here
    
     public class From
    {
        public int id { get; set; }
        public bool is_bot { get; set; }
        public string first_name { get; set; }
        public string username { get; set; }
        public string language_code { get; set; }
    }
    
    public class Chat
    {
        public long id { get; set; }
        public string title { get; set; }
        public string type { get; set; }
        public string username { get; set; }
    }
    
    public class ForwardFromChat
    {
        public long id { get; set; }
        public string title { get; set; }
        public string type { get; set; }
    }
    
    public class Document
    {
        public string file_name { get; set; }
        public string mime_type { get; set; }
        public string file_id { get; set; }
        public int file_size { get; set; }
    }
    public class Audio
    {
        public int duration { get; set; }
        public string mime_type { get; set; }
        public string title { get; set; }
        public string performer { get; set; }
        public string file_id { get; set; }
        public int file_size { get; set; }
    }
    public class Photo
    {
        public string file_id { get; set; }
        public int file_size { get; set; }
        public int width { get; set; }
        public int height { get; set; }
    }
    public class Video
    {
        public int duration { get; set; }
        public int width { get; set; }
        public int height { get; set; }
        public string mime_type { get; set; }
        public Thumb thumb { get; set; }
        public string file_id { get; set; }
        public int file_size { get; set; }
    }
    public class Thumb
    {
        public string file_id { get; set; }
        public int file_size { get; set; }
        public int width { get; set; }
        public int height { get; set; }
    }
    public class Message
    {
        public int message_id { get; set; }
        public From from { get; set; }
        public Chat chat { get; set; }
        public int date { get; set; }
        public string text { get; set; }
    }
    
    public class CaptionEntity
    {
        public int offset { get; set; }
        public int length { get; set; }
        public string type { get; set; }
    }
    
    public class EditedChannelPost
    {
        public int message_id { get; set; }
        public Chat chat { get; set; }
        public int date { get; set; }
        public int edit_date { get; set; }
        public string caption { get; set; }
        public List<CaptionEntity> caption_entities { get; set; }
        public string text { get; set; }
        public Document document { get; set; }
        public Video video { get; set; }
        public Audio audio { get; set; }
        public List<Photo> photo { get; set; }
    }
    public class ChannelPost
    {
        public int message_id { get; set; }
        public Chat chat { get; set; }
        public int date { get; set; }
        public ForwardFromChat forward_from_chat { get; set; }
        public int forward_from_message_id { get; set; }
        public string forward_signature { get; set; }
        public int forward_date { get; set; }
        public string caption { get; set; }
        public string text { get; set; }
        public List<CaptionEntity> caption_entities { get; set; }
        public Document document { get; set; }
        public Video video { get; set; }
        public Audio audio { get; set; }
        public List<Photo> photo { get; set; }
    }
    
    public class RootObject
    {
        public long update_id { get; set; }
        public ChannelPost channel_post { get; set; }
        public EditedChannelPost edited_channel_post { get; set; }
        public Message message { get; set; }
    }
    
    来自的公共类
    {
    公共int id{get;set;}
    公共布尔是{get;set;}
    公共字符串first_name{get;set;}
    公共字符串用户名{get;set;}
    公共字符串语言_代码{get;set;}
    }
    公共课堂聊天
    {
    公共长id{get;set;}
    公共字符串标题{get;set;}
    公共字符串类型{get;set;}
    公共字符串用户名{get;set;}
    }
    公共类ForwardFromChat
    {
    公共长id{get;set;}
    公共字符串标题{get;set;}
    公共字符串类型{get;set;}
    }
    公共类文档
    {
    公共字符串文件名{get;set;}
    公共字符串mime_类型{get;set;}
    公共字符串文件_id{get;set;}
    公共int文件大小{get;set;}
    }
    公共类音频
    {
    公共整数持续时间{get;set;}
    公共字符串mime_类型{get