如何使用ASP在Telegram中发送sendMessage方法?

时间:2016-12-28 12:06:41

标签: asp.net json telegram telegram-bot

我希望用电报机器人发送消息,但是当用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)

1 个答案:

答案 0 :(得分:0)

所需的终点是:

https://api.telegram.org/bot {TOKEN} / {METHOD}

在这种情况下,{METHOD}将为sendMessage