首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何保存发送给机器人的主动消息?

如何保存发送给机器人的主动消息?
EN

Stack Overflow用户
提问于 2019-04-29 15:25:32
回答 1查看 503关注 0票数 0

我正在使用Microsoft BotConnector向我的机器人发送消息,但它们不是作为普通消息记录的。为了将消息记录到DB,我编写了自定义记录器:

代码语言:javascript
复制
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的代码:

代码语言:javascript
复制
/**
 * 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);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-02 00:00:14

与其使用REST向用户发送积极的消息,我建议使用BotFramework适配器继续与用户的对话。当您从适配器发送主动消息时,活动会通过记录器中间件传递并保存到存储中。如果要从Azure函数中启动主动消息,可以在从该函数调用的索引文件中设置另一个消息端点。看看下面的代码片段。

index.js

代码语言:javascript
复制
// 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端点发送主动消息的一个很好的例子。

希望这能有所帮助!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55906407

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档