我正在使用Microsoft BotConnector向我的机器人发送消息,但它们不是作为普通消息记录的。为了将消息记录到DB,我编写了自定义记录器:
class CustomLogger {
/**
* Log an activity to the transcript file.
* @param activity Activity being logged.
*/
constructor() {
this.conversations = {};
}
logActivity(activity) {
if (activity) {
console.log("Log information")
}
if (!activity) {
throw new Error("Activity is required.");
}
if (activity.conversation) {
var id = activity.conversation.id;
if (id.indexOf("|" !== -1)) {
id = activity.conversation.id.replace(/\|.*/, "");
}
}
if (activity.type === "message") {
Conv.create({
text: activity.text,
conv_id: activity.conversation.id,
from_type: activity.from.role,
message_id: activity.id || activity.replyToId
}).then(() => {
console.log("logged");
});
delete this.conversations[id];
}
}
}它可以很好地处理普通消息,但是不能处理发送给
POST /v3/会话/{conversationId}/activities
通过微软机器人连接器。
当我使用bot连接器发送消息时,它不会通过活动记录请求。
用于发送主动msg的代码:
/**
* Send message to the user.
*/
function sendMessage(token, conversation, name) {
var config = {
headers: { "Authorization": "Bearer " + token }
};
var bodyParameters = {
"type": "message",
"text": name
}
axios.post(
'https://smba.trafficmanager.net/apis/v3/conversations/29:XXXXXXXXXXXXXXX/activities',
bodyParameters,
config
).then((response) => {
console.log(response)
}).catch((error) => {
console.log(error)
});
}
let name = "Hey, How was your week?";
let conversation = "29:XXXXXXXXXXXXXXX";
run(conversation, name);发布于 2019-05-02 00:00:14
与其使用REST向用户发送积极的消息,我建议使用BotFramework适配器继续与用户的对话。当您从适配器发送主动消息时,活动会通过记录器中间件传递并保存到存储中。如果要从Azure函数中启动主动消息,可以在从该函数调用的索引文件中设置另一个消息端点。看看下面的代码片段。
index.js
// Listen for incoming notifications and send proactive messages to user.
server.get('/api/notify/:conversationID', async (req, res) => {
const { conversationID } = req.params;
const conversationReference = conversationReferences[conversationID];
await adapter.continueConversation(conversationReference, async turnContext => {
await turnContext.sendActivity('proactive hello');
});
res.setHeader('Content-Type', 'text/html');
res.writeHead(200);
res.write('<html><body><h1>Proactive messages have been sent.</h1></body></html>');
res.end();
});关于更多细节,我将看一看这个主动消息示例。它位于samples-work-in-progress分支中,可能略有变化,但它是如何配置项目以从Restify端点发送主动消息的一个很好的例子。
希望这能有所帮助!
https://stackoverflow.com/questions/55906407
复制相似问题