我无法使用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!"
}
有了这种方法,没有错误,但也没有消息,它不会来!
p.S.
请注意,机器人程序的其余部分运行良好。消息已被接受,我可以发送回复。
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)