电报C#示例发送消息

时间:2015-03-31 08:26:49

标签: c# .net telegram

我找不到从C#通过电报协议发送消息的示例。我尝试使用this但失败了。 你能举个例子吗?

5 个答案:

答案 0 :(得分:13)

TLSharp是C#上Telegram API的基本实现。在此处查看https://github.com/sochix/TLSharp

答案 1 :(得分:0)

对于我的机器人,我使用Telegram.Bot nuget包。完整示例代码为here

以下是发送邮件以回复收到邮件的示例。

// create bot instance
var bot = new TelegramBotClient("YourApiToken");

// test your api configured correctly
var me = await bot.GetMeAsync();
Console.WriteLine($"{me.Username} started");

// start listening for incoming messages
while (true) 
{
    //get incoming messages
    var updates = await bot.GetUpdatesAsync(offset);
    foreach (var update in updates)
    {
        // send response to incoming message
        await bot.SendTextMessageAsync(message.Chat.Id,"The Matrix has you...");
    }
}

答案 2 :(得分:0)

最简单的方法是将http请求作为url字符串直接发送到Telegram BOT API,您甚至可以在浏览器中测试这些url字符串,请在此处查看我的另一个答案中的详细信息: https://stackoverflow.com/a/57341990/11687179

答案 3 :(得分:0)

第一步,您必须在botfather中生成一个bot,然后在C#中使用下面的代码

    private void SendMessage(string msg)
    {
        string url = "https://api.telegram.org/{botid}:{botkey}/sendMessage?chat_id={@ChanalName}&text={0}";
        WebClient Client = new WebClient();
        /// If you need to use proxy 
        if (Program.UseProxy)
        {
            /// proxy elements are variable in Program.cs
            Client.Proxy = new WebProxy(Program.ProxyUrl, Program.ProxyPort);
            Client.Proxy.Credentials = new NetworkCredential("hjolany", "klojorasic");
        }
        Client.DownloadString(string.Format(url, msg));
        }));
    }

答案 4 :(得分:-2)

Telegram有一个官方的API可以完全满足您的需求,但您必须查看http请求。

以下是有关发送消息的文档:

功能

messages.sendMessage

<强> PARAMS

peer    InputPeer   User or chat where a message will be sent
message string  Message text
random_id   long    Unique client message ID required to prevent message resending

查询示例

(messages.sendMessage (inputPeerSelf) "Hello, me!" 12345678901)

返回错误

Code    Type    Description
400 BAD_REQUEST PEER_ID_INVALID Invalid peer
400 BAD_REQUEST MESSAGE_EMPTY   Empty or invalid UTF8 message was sent
400 BAD_REQUEST MESSAGE_TOO_LONG    Message was too long.

Current maximum length is 4096 UTF8 characters

有关完整文档,请转到here