我无法使用 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!"
}
用这种方法,没有错误,但也没有消息,它不来!
附言
请注意,机器人的其余部分工作正常。
消息被接受,我可以发送回复。
答案 0 :(得分:0)
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)