所以问题是我为我的Telegram机器人设置了一个webHook地址,如下所示:
https://api.telegram.org/bot<TOKEN>/setWebHook?url=https://evolhookah.com/Home/ReceiveWebhook
之后,我收到了JSON格式的确认,声明从现在起Telegram将向该URL发送消息。
然后我创建了一个方法ReceiveWebhook(),它负责处理传入的请求,这些请求看起来像这些方法(没有一个工作):
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作为字符串,这就是它的样子:
public ActionResult JSONString(String receivedJSON)
{
var bs = new TgDesserialize();
PeopleAttributes people = bs.desserializer(receivedJSON);
return Content(people.firstName);
}
问题:每次收到webhook时,我都会得到null JSON或无法在控制器中正确接收它。
问题:
https://api.telegram.org/bot<TOKEN>/getUpdates
表示我在JSON中有数据?答案 0 :(得分:3)
首先,我建议您使用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中使用此命令:
ngrok http 20201
20201是您的本地主机端口(localhost:20201)。 现在ngrok给你一个https链接,你必须将该链接设置为你的电报webhook(就像你说的那样)。 此时如果电报为您的机器人发送webhook消息,那么您可以在localhost中调试它。
- 支持IPv4,Webhooks目前不支持IPv6。
- 在端口443,80,88或8443上接受来自149.154.167.197-233的传入POST。
- 能够处理TLS1.0 + HTTPS流量。
- 提供受支持的非通配符,已验证或自签名证书。
- 使用CN或SAN。它与您在设置时提供的域匹配。
- 提供所有中间证书以完成验证链。
醇>
答案 1 :(得分:2)
您需要根据电报中的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
专业提示:如果您有一个示例JSON文件,您可以复制它,得到Visual Studio =&gt; Edit =&gt; Paste Special =&gt;将JSON粘贴为类。这将为您生成课程
然后,您可以将此类作为参数添加到您的webhook:
public ActionResult JSONString(TelegramUpdate update)
{
return Content(update.message.from.first_name);
}
答案 2 :(得分:0)
[HttpPost]
public async Task<ActionResult> Index()
{
try
{
var req = Request.InputStream; //get Request from telegram
var responsString = new StreamReader(req).ReadToEnd(); //read request
RootObject ro = JsonConvert.DeserializeObject<RootObject>(responsString);
}
catch (Exception ex)
{
return Content("Error");
}
// This code hint to telegram Ttat , I Get Message And dont need to send this message again
return Content("{\"ok\":true}");
}
然后创建一个类:
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; }
}
享受吧!