首页 > 解决方案 > 如何从电报机器人中的多个文件发送特定文件

问题描述

我用 C# 编写了一个电报机器人,它从用户那里下载文件,例如 .docx 文件并将这个文件转换为 PDF。我想在将文件转换为 PDF 后,在 word 文件和 PDF 文件之间将此文件(PDF 文件)返回给用户。

此代码将 word 文件返回给用户。

// Download File
Telegram.Bot.Types.File file = await bot.GetFileAsync(message.Document.FileId);
string fileName = file.FileId + "." + file.FilePath.Split('.').Last();

using (FileStream saveFileStream = File.Open(fileName, FileMode.Create))
{
    await bot.DownloadFileAsync(file.FilePath, saveFileStream);
}
await bot.SendTextMessageAsync(chatId, "File Saved");

// Convert Document to PDF
Spire.Doc.Document document = new Spire.Doc.Document();
document.LoadFromFile(fileName);
document.SaveToFile(fileName + ".pdf", Spire.Doc.FileFormat.PDF);

// Send PDF File
using (var sendFileStream = File.Open(fileName, FileMode.Open))
{
   await bot.SendDocumentAsync(chatId, new Telegram.Bot.Types.InputFiles.InputOnlineFile(sendFileStream, fileName));
}

await bot.SendTextMessageAsync(chatId, "Your pdf file");

标签: c#telegramtelegram-bot

解决方案


推荐阅读