node.js - 如何使用 Viber Bot 发送消息
问题描述
我无法使用 Viber Bot 发送消息。
我试过这个选项:
const TextMessage = require('viber-bot').Message.Text
viber_bot.sendMessage(req.body.viber_user_id, new TextMessage(req.body.message))
结果,错误:
UnhandledPromiseRejectionWarning: Error: Invalid arguments passed to sendMessage. 'optionalReceiver' and 'chatId' are Missing.
像这样尝试过:
viber_bot.sendMessage({
"receiver": "<my_id>",
"type": "text",
"text": "Hello world!"
}
用这种方法,没有错误,但也没有消息,它不来!
PS
请注意,机器人的其余部分工作正常。消息被接受,我可以发送回复。
解决方案
sendMessage()
需要UserProfile
而不仅仅是viber_user_id
以下应该工作:
const receiver = "to viber user";
const sampleUserProfile = { id: receiver };
const text = "sample message";
const sampleTrackingData = { value: "sent 1 message" };
const sampleKeyboard = { button: { bgColor: "#FFFFFF" } };
const sampleChatId = "sample chatId";
const sampleMinApiVersion = 2;
const sampleMessage = new TextMessage(text, sampleKeyboard, null, null, null, sampleMinApiVersion);
viber_bot.sendMessage(sampleUserProfile, sampleMessage, sampleTrackingData, sampleChatId);
另请注意,有两种实现可能会给您带来问题,具体取决于您实际使用的内容:
-
ViberClient.prototype.sendMessage = function (optionalReceiver, messageType, messageData, optionalTrackingData, optionalKeyboard, optionalChatId, optionalMinApiVersion)
-
ViberBot.prototype.sendMessage = function (optionalUserProfile, messages, optionalTrackingData, optionalChatId)
推荐阅读
- excel - SendKeys 未捕获所有数据
- python - Keras Lambda 层和 py_function 给出错误 Cannot iterate over a shape with unknown rank
- asp.net-core - 如何在 ASP.NET Core 3 SPA 应用程序的 Cookie 中存储 JWT 令牌
- vuetify.js - 如何摆脱此 DIV 中的第二个块
- android - 从 logback 库中获取文件路径
- c - 有没有办法在 C 转换程序中实现宏(#define)?
- c++ - 如何在应用程序子项目中从 gradle 子项目链接静态库
- python - pycharm运行单元测试停止工作
- c++ - 如何在给定特定节点的 BST 中找到直接较大的元素?
- python - 如何在 Python 中格式化 df 列