我希望用电报机器人发送消息,但是当用webhook接收json数据时我无法发送消息。
我的代码相同:
private void SendMessage(SendMessage SendMessageObject = null)
{
JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
string message = jsonSerializer.Serialize(SendMessageObject);
var webAddr = "https://api.telegram.org/bot"+Token;
var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);
httpWebRequest.ContentType = "application/json; charset=utf-8";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = message;
streamWriter.Write(json);
streamWriter.Flush();
}
}
和我的sendMessage
班级:
public class sendMessage
{
public string method { get { return "sendMessage"; }}
public int chat_id { get; set; }
public string text { get; set; }
public string parse_mode { get; set; }
public bool disable_web_page_preview { get; set; }
public bool disable_notification { get; set; }
public int reply_to_message_id { get; set; }
public ReplyKeyboardMarkup reply_markup { get; set; }
}
我的错误是:
Exception Found:
Type: System.Net.WebException
Message: The remote server returned an error: (502) Bad Gateway.
Source: System
Stacktrace:
at System.Net.HttpWebRequest.GetResponse()
at ShoppingTelegramBotService.SendMessage(sendMessage SendMessageObject)
答案 0 :(得分:0)