How to send a file into the telegram api in C# and getting a file_id to reference?
the question is how can I upload a file to the telegram server through a C# code?
I found this in telegram api, but I cannot understand the syntax.
The referenced link is in TL language. I have implemented a sample to find file_id and send it to a specified user in C# using TLSharp:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using TeleSharp.TL;
using TLSharp.Core;
using TLSharp.Core.Utils;
namespace TLSharpTest
{
class Program
{
static void Main(string[] args)
{
Task.Run(async () => { await doTask(); });
Console.ReadLine();
}
static async Task doTask()
{
var apiId = 87654321; //apiId: get from https://my.telegram.org/apps
var apiHash = "<apiHash>"; // get from https://my.telegram.org/apps
var client = new TelegramClient(apiId, apiHash);
await client.ConnectAsync();
var phone = "<phone_number>";
var hash = await client.SendCodeRequestAsync(phone);
var code = "<sent_code_by_telegram>";
var user = await client.MakeAuthAsync(phone, hash, code);
//get available contacts
var result = await client.GetContactsAsync();
//find recipient in contacts
var specifiedUser = result.users.lists
.Where(x => x.GetType() == typeof(TLUser))
.Cast<TLUser>()
.FirstOrDefault(x => x.phone == "<recipient_phone>");
// send file to the specified contact (sample from TLSharp github)
var fileResult = (TLInputFile)await client.UploadFile("cat.jpg", new StreamReader("data/cat.jpg"));
await client.SendUploadedPhoto(new TLInputPeerUser() { user_id = specifiedUser.id }, fileResult, "kitty");
}
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments