How to send Media files via Telegram Bot to a local server

55 Views Asked by At

When I try to send regular files like .pdf, .txt, .doc and etc Telegram bot does not compress them and forwards files to the designated directory as intended.

Yet, when for example, I send photos with compresion or especially audio files Telegram bot does not recognise it as a file, hence does not forward it to a necessary directory. What needs to be added to the following C# code, so that all data including the media one would be recognised as a file and as a result forwarded to the necessary directory (code taken from FilesController.cs)?

using System.Threading.Tasks;
using Deployf.Botf;
using Microsoft.Extensions.Configuration;
using Telegram.Bot;
using Telegram.Bot.Types.InputFiles;

namespace FileSenderTelegram
{
    public class FilesController: BotController
    {
        private readonly IConfiguration _configuration;

        public FilesController(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        [Action("/start", "Start to work with bot")]
        public void Start()
        {
            PushL("Хай, все данные включая медиа загружай как файл!");
        }
        
        [On(Handle.Unknown)]
        public async Task Unknown()
        {
            if (Context.Update.Message?.Document is not null)
            {
                var document = Context.Update.Message?.Document;
                var file = await Context.Bot.Client.GetFileAsync(document!.FileId);
                var filePath = System.IO.Path.Combine(_configuration["FileStoragePath"], document.FileName!);

                await using var fs = new FileStream(filePath, FileMode.Create);
                await Context.Bot.Client.DownloadFileAsync(file.FilePath!, fs);
                
                PushL($"Файл был загружен");
            }
        }
    }
}




0

There are 0 best solutions below