我正在使用Microsoft BotConnector向我的机器人发送消息,但它们没有作为普通消息记录下来。为了将消息记录到数据库,我编写了自定义记录器:
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 / conversations / {conversationId} /活动
通过Microsoft机器人连接器。
当我使用漫游器连接器发送消息时,它不会通过活动记录请求。
我用来发送主动式味精的代码:
/**
* 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);
答案 0 :(得分:0)
我建议不要使用REST API向用户发送主动消息,而建议使用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();
});
有关更多详细信息,我将看一下这个Proactive Messages Sample。它位于samples-work-in-progress
分支中,可能会稍有变化,但这是一个很好的示例,说明了如何配置您的项目以从Restify端点发送主动消息。
希望这会有所帮助!