how to i send sendMessage method in Telegram using ASP?

927 Views Asked by At

I want send message with telegram bot, but I can't send message when recieve json data with webhook.

My same code:

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();
        }
    }

and my sendMessage class:

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; }
}

and my error is:

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

There are 1 best solutions below

0
On

The required endpoint is:

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

In this case {METHOD} will be sendMessage