C# 无法从电报webHook ASP.NET接收传入的JSON
所以问题是我为我的电报机器人设置了一个webHook地址,如下所示: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(),该方法负责处理传入的请求,该方法过去看起来像这些方法(它们都不起作用): 不幸的是,使用流的想法不起作用,然后我决定以字符串形式接
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,要么无法在控制器中正确接收它
问题:
https://api.telegram.org/bot/getUpdates
显示我在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);
}
首先,我建议您使用这个库并查看这个示例。 但总的来说:
{
"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"
}
ngrok http 20201
20201是您的本地主机端口(localhost:20201)。
现在ngrok给你一个https链接,你必须把这个链接设置成你的电报webhook(就像你说的那样)。
此时,如果telegram为您的bot发送webhook消息,那么您可以在本地主机中调试它
首先,我建议您使用这个库并查看这个示例。 但总的来说:
{
"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"
}
ngrok http 20201
20201是您的本地主机端口(localhost:20201)。
现在ngrok给你一个https链接,你必须把这个链接设置成你的电报webhook(就像你说的那样)。
此时,如果telegram为您的bot发送webhook消息,那么您可以在本地主机中调试它
[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