node js - 如何保存从电报机器人接收到的文件

溴化钾

如何从流或 nodejs 中的 url 保存文件?我有一个电报机器人,可以从用户那里获取文件。我能够获取文件 url 或流,但我不确定如何在处理之前将其保存在临时目录中。

这是我正在使用的库的链接

我该如何继续?这是代码


#!/usr/bin/env node

const process = require('process');
const fs = require('fs');
const path = require('path');
const TelegramBot = require('node-telegram-bot-api');


const token = process.env.TELEGRAM_BOT_TOKEN || '5xxxxxxxxxxxxx';
// Create a bot that uses 'polling' to fetch new updates
const bot = new TelegramBot(token, {polling: true});  

bot.onText(/\/echo(.+)/, async (msg, match) => {
  // 'msg' is the received Message from Telegram
  // 'match' is the result of executing the regexp above on the text content
  // of the message
  //const chatId = msg.chat.id;
});

bot.on('message', async (msg) => {
...
});


bot.on('document', async (data) => {
  console.log(data);

  // here I'm getting the file url  
  const fileURL = await bot.getFileLink(data.document.file_id);
  // the library have a method that give the ability to get the file stream. How I save it to a file?
  const stream = await bot.getFileStream(data.document.file_id);
  console.log(fileURL, stream);
});

//

roj1512

你可以这样做:

const { createWriteStream } = require('fs');

stream.pipe(createWriteStream('your_file'));

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Node.js 电报机器人返回 ECONNREFUSED

电报机器人通过Node.js发送PDF文件

如何使用 Node.js node-telegram-bot-api 从电报机器人订阅和取消订阅用户?

哪个是 Azure 上 node.js 电报机器人的最佳托管解决方案?

如何通过node-red中的环境变量设置电报机器人的名称?

使用 Node.js 使用 Azure 在电报上托管机器人

Node JS Steam交易机器人

微软 QnA 机器人 Node.js

使用node.js的Discord机器人-如何将消息转换为小写?

如何在node.js中跟踪用户对特定聊天机器人消息的回复

“如何使用 anuglar 和 node.js 构建聊天机器人”?

如何使用Node.js检查ram的当前使用情况?(Discord.js机器人)

电报机器人-接收照片网址

如何使用 Node.js 将多个知识库添加到 Azure 门户中的单个机器人?

如何通过电报机器人发送PIL图像而不将其保存到文件中

使用Node.JS设计机器人,该机器人以随机超时和通用例程运行

电报机器人从其他机器人接收命令

电报机器人:如何使用botFather删除命令

如何更改电报机器人消息中的图片?

电报机器人,如何请求用户输入?

如何删除电报机器人说明?

如何用照片让电报机器人回复?

电报机器人:如何获得内联结果

如何安排电报机器人发送消息?

如何防止电报机器人键盘滚动

如何从电报机器人创建私人消息?

我如何部署电报机器人?

机器人消息的消息过滤器 undefined node.js

调用Rest API时,Node.js Discord机器人消息为空