C# 如何通过 Telegram 发送文件 API

C# how to send a file via Telegram API

我正在使用 WTelegramClient 库。

这是我发送消息的方式:

var client = new WTelegram.Client(Config);
await client.LoginUserIfNeeded();
var contacts = await client.Contacts_ImportContacts(new[]
{
   new InputPhoneContact { phone = "+998901234567" } 
});

if (contacts.imported.Length > 0)
await client.SendMessageAsync(contacts.users[contacts.imported[0].user_id], "Hello, world!");

如何发送多个文件?或至少一个文件。

我需要从列表或文件夹发送文件。我很乐意提供任何帮助。

List<byte[]> file = new List<byte[]>();

样本来自官方documentation

1.Get上传文件夹路径,像这样

const string Filepath = @"C:\...\photo.jpg";

2.Upload 文件使用客户端和路径

var inputFile = await client.UploadFileAsync(Filepath);

3.Send 文件到对等 (chats.chats[ChatId])

await client.SendMediaAsync(peer, "Here is the photo", inputFile);

示例代码

const int ChatId = 1234567890; // the chat we want
const string Filepath = @"C:\...\photo.jpg";

using var client = new WTelegram.Client(Environment.GetEnvironmentVariable);
await client.LoginUserIfNeeded();
var chats = await client.Messages_GetAllChats(null);
InputPeer peer = chats.chats[ChatId];
var inputFile = await client.UploadFileAsync(Filepath);
await client.SendMediaAsync(peer, "Here is the photo", inputFile);