我想在C#中使用Telegram API向一个数字发送一条简单的消息.我在GitHub上发现了一些lib,但是我无法使用它们.
谁能提供简单的代码?我可以简单地拨打HTTP电话吗?
小智 18
这是我目前找到的最简单的方法。我在这里找到了,感谢 Paolo Montalto https://medium.com/@xabaras/sending-a-message-to-a-telegram-channel-the-easy-way-eb0a0b32968
通过 BotFather 创建 Telegram bot 并通过以下方式获取目标 ID 后,https://api.telegram.org/bot[YourApiToken]/getUpdates
您可以通过使用以下 URL 向 Telegram BOT API 发出 HTTP GET 请求来向您的 ID 发送消息https://api.telegram.org/bot[YourApiToken]/sendMessage?chat_id=[DestitationID]&text=[MESSAGE_TEXT]
可以在此处找到有关创建机器人和获取 ID 的简单方法的详细信息:https : //programmingistheway.wordpress.com/2015/12/03/send-telegram-messages-from-c/
您甚至可以直接在浏览器中测试这些 url 字符串。这是我在 C# 中使用的一种简单方法来发送消息,不依赖任何与 bot api 相关的 dll 和异步调用复杂性:
using System.Net;
...
public string TelegramSendMessage(string apilToken, string destID, string text)
{
string urlString = $"https://api.telegram.org/bot{apilToken}/sendMessage?chat_id={destID}&text={text}";
WebClient webclient = new WebClient();
return webclient.DownloadString(urlString);
}
Run Code Online (Sandbox Code Playgroud)
小智 12
var bot = new Api("your api key here");
var t = await bot.SendTextMessage("@channelname or chat_id", "text message");
您现在可以在所有方法中的chat_id位置传递通道用户名(格式为@channelusername)(而不是forwardMessage中的from_chat_id).为此,机器人必须是渠道中的管理员.
https://core.telegram.org/bots/api
使用此代码:)与https://github.com/sochix/TLSharp
using TeleSharp.TL;
using TLSharp;
using TLSharp.Core;
namespace TelegramSend
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
TelegramClient client;
private async void button1_Click(object sender, EventArgs e)
{
client = new TelegramClient(<your api_id>, <your api_key>);
await client.ConnectAsync();
}
string hash;
private async void button2_Click(object sender, EventArgs e)
{
hash = await client.SendCodeRequestAsync(textBox1.Text);
//var code = "<code_from_telegram>"; // you can change code in debugger
}
private async void button3_Click(object sender, EventArgs e)
{
var user = await client.MakeAuthAsync(textBox1.Text, hash, textBox2.Text);
}
private async void button4_Click(object sender, EventArgs e)
{
//get available contacts
var result = await client.GetContactsAsync();
//find recipient in contacts
var user = result.users.lists
.Where(x => x.GetType() == typeof(TLUser))
.Cast<TLUser>()
.Where(x => x.first_name == "ZRX");
if (user.ToList().Count != 0)
{
foreach (var u in user)
{
if (u.phone.Contains("3965604"))
{
//send message
await client.SendMessageAsync(new TLInputPeerUser() { user_id = u.id }, textBox3.Text);
}
}
}
}
}}
Run Code Online (Sandbox Code Playgroud)
现在有WTelegramClient,使用最新的 Telegram Client API 协议(以用户身份连接,而不是机器人)。
该库非常完整,而且非常易于使用。请按照GitHub 上的 README进行简单介绍。
向某人发送消息可以很简单:
using TL;
using var client = new WTelegram.Client(); // or Client(Environment.GetEnvironmentVariable)
await client.LoginUserIfNeeded();
var result = await client.Contacts_ResolveUsername("USERNAME");
await client.SendMessageAsync(result.User, "Hello");
//or by phone number:
//var result = await client.Contacts_ImportContacts(new[] { new InputPhoneContact { phone = "+PHONENUMBER" } });
//client.SendMessageAsync(result.users[result.imported[0].user_id], "Hello");
Run Code Online (Sandbox Code Playgroud)
1-先在电报中创建一个频道(例如@mychanel)
2-创建一个电报机器人(例如@myTestBot)并获取下一步的api令牌
3-将@myTestBot 以管理员用户身份添加到您的频道(@mychanel)
4-使用以下代码发送消息:
var bot = new TelegramBotClient("api_token_bot");
var s = await bot.SendTextMessageAsync("@mychanel", "your_message");
Run Code Online (Sandbox Code Playgroud)
小智 -5
只需查看并学习如何使用您喜欢的语言发出 POST HTTP 请求即可。
然后通过文档了解如何使用 Telegram Bot API: