如何在Viber-bot中使用bot.getUserDetails

时间:2019-05-31 13:20:23

标签: javascript node.js api viber

我是nodejs的新手,现在正在编写Viber-bot。 Viber-bot文档非常糟糕,我真的不明白如何使用某些功能。 例如:我想查看一些用户的数据,将该数据保存在移动设备上,等等。 如何使用功能:

bot.getUserDetails(userProfile)

如果可能的话,我想获取姓名,身份证,电话号码并将其保存到一些变量中。

我有此代码:

const ViberBot = require('viber-bot').Bot;
const BotEvents = require('viber-bot').Events;
const TextMessage = require('viber-bot').Message.Text;
const 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: "I'm your bot",
  avatar: ""
});

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

很抱歉,如果是愚蠢的问题。

非常感谢。

1 个答案:

答案 0 :(得分:0)

您可以从以下这些事件中触发的响应中获取用户个人资料数据。

“开始对话” “ message_received”

const ViberBot = require('viber-bot').Bot;
const BotEvents = require('viber-bot').Events;

const bot = new ViberBot(logger, {
    authToken: process.env.VB_API_KEY, 
    name: "Bot Name",
    avatar: "" 
});

bot.on(BotEvents.CONVERSATION_STARTED, (response) => {

      const roomname = response.userProfile.id;
      const username = response.userProfile.name;
      const profile_pic = response.userProfile.avatar;
      const country_origin = response.userProfile.country;
      const language_origin = response.userProfile.language;

      //Do something with user data
})

bot.on(BotEvents.MESSAGE_RECEIVED, (message, response) => {
    //Same as conversation started
});

如果要专门获取用户信息,可以使用viber NodeJS开发人员文档中此处描述的端点。 https://developers.viber.com/docs/all/#get_user_details

如果您想获取机器人信息,请尝试此端点。 https://developers.viber.com/docs/all/#get_account_info