Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Viber Bot从缓冲区发送文件_Node.js_Viber Api - Fatal编程技术网

Node.js Viber Bot从缓冲区发送文件

Node.js Viber Bot从缓冲区发送文件,node.js,viber-api,Node.js,Viber Api,我正在尝试使用viber bot将pdf文件发送到客户端,但我在本地或作为url没有这些文件。我从其他请求中获取所需文件,并且在viber bot文档中未找到任何有关缓冲区的信息。 这是我的密码: const ViberBot = require('viber-bot').Bot, BotEvents = require('viber-bot').Events, TextMessage = require('viber-bot').Message.Text, FileMessage

我正在尝试使用viber bot将pdf文件发送到客户端,但我在本地或作为url没有这些文件。我从其他请求中获取所需文件,并且在viber bot文档中未找到任何有关缓冲区的信息。 这是我的密码:

const ViberBot = require('viber-bot').Bot,
  BotEvents = require('viber-bot').Events,
  TextMessage = require('viber-bot').Message.Text,
  FileMessage = require('viber-bot').Message.File,
  express = require('express');
const app = express();

if (!process.env.BOT_ACCOUNT_TOKEN) {
  console.log('Could not find bot account token key.');
  return;
}
if (!process.env.EXPOSE_URL) {
  console.log('Could not find exposing url');
  return;
}

const bot = new ViberBot({
  authToken: process.env.BOT_ACCOUNT_TOKEN,
  name: process.env.BOT_NAME,
  avatar: ''
});
bot.on(BotEvents.SUBSCRIBED, response => {
  response.send(new TextMessage(`Hi there ${response.userProfile.name}. I am ${bot.name}! Feel free to ask me anything.`));
});
bot.onSubscribe(response => console.log(`Subscribed: ${response.userProfile.name}`));
bot.on(BotEvents.MESSAGE_RECEIVED, async (message, response) => {
  try {
//use buffer instead of urls
    await response.send(
new FileMessage('https://upload.wikimedia.org/wikipedia/commons/3/3d/Katze_weiss.png', 2, 'test.png')).catch(err => console.log(err))
  } catch(err) {
    console.log(err);
  }

  response.send(new TextMessage(`Message received.`));
});
const port = process.env.PORT || 3000;
app.use("/viber/webhook", bot.middleware());
app.listen(port, () => {
  console.log(`Application running on port: ${port}`);
  bot.setWebhook(`${process.env.EXPOSE_URL}/viber/webhook`).catch(error => {
    console.log('Can not set webhook on following server. Is it running?');
    console.error(error);
    process.exit(1);
  });
});

这对我有什么好处:

  • Viber Bot在Node.js下的本地服务器上运行
  • Ngrok生成并公开一个特定链接,该链接将ViberBot直播在互联网上
  • 生成您的.pdf并将其保存在本地路径(e.x.ngrokpath/myfiles/mypdf.pdf)
然后在您的机器人内利用:

var filename = "MyClientFile.pdf";
var urlstring = expose_url+"/myfiles/"+filename;    
var pdffilesize = 150; //kb                        
response.send(new FileMessage(urlstring, pdffilesize, filename));
expose_url是来自Ngrok会话的url。 根据viber api,PdfileSize仅供参考。Viber机器人需要知道文件大小

如果您不想存储本地文件并直接从缓冲区检索,请使用pdfmake插件

生成您的pdf(base64字符串),然后作为媒体馈送到viber消息

希望这对你也有用


我的诀窍是什么:

  • Viber Bot在Node.js下的本地服务器上运行
  • Ngrok生成并公开一个特定链接,该链接将ViberBot直播在互联网上
  • 生成您的.pdf并将其保存在本地路径(e.x.ngrokpath/myfiles/mypdf.pdf)
然后在您的机器人内利用:

var filename = "MyClientFile.pdf";
var urlstring = expose_url+"/myfiles/"+filename;    
var pdffilesize = 150; //kb                        
response.send(new FileMessage(urlstring, pdffilesize, filename));
expose_url是来自Ngrok会话的url。 根据viber api,PdfileSize仅供参考。Viber机器人需要知道文件大小

如果您不想存储本地文件并直接从缓冲区检索,请使用pdfmake插件

生成您的pdf(base64字符串),然后作为媒体馈送到viber消息

希望这对你也有用


你找到解决方案了吗?@edrichans似乎维伯做不了这样的事你找到了解决方案吗?@edrichans似乎维伯做不了这样的事