skip to Main Content

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.

3

Answers


  1. Chosen as BEST ANSWER

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

  2. you have two options

    1. Yo can use telegram bot api which you can find in this page, there is a method to send file that called sendDocument which is all explained here and this method gives you file_id after uploaded. But there is some limitations about how long the file will last or what kind of files you can send and what size is acceptable.
    2. You can use telegram it self instead of using bot api which means you have to sign-up with number like a regular telegram user to send and receive file. There is c# library and also telegram open source for Windows phone which is c# you can dig into it and manage it for uploading files and get file id for your bot or just send the file directly to your bot from that acount.

    this is list of all telegram open source app (not bot api)

    Use “mtproto telegram c#” for google to get repository for c# mtproto library.
    The good library that can handle easy your need is TLShorp (wich needs phone number to work obviously)

    Login or Signup to reply.
  3. using TLSharp.Core.Utils;
    
    var file = (TLInputFile)(await this.Client.UploadFile(FileName, new StreamReader(FilePath)));
    return file.Id;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search