简体   繁体   English

在来自Wit.ai聊天机器人的每个响应之前添加“ typing_on”发件人操作气泡

[英]Adding 'typing_on' sender action bubble before each response from Wit.ai chatbot

I've built a flow-base chat bot using FB messenger, Wit.ai and node.js. 我使用FB Messenger,Wit.ai和node.js构建了基于流的聊天机器人。 It's working well, but in order to make the interaction seem more natural I want my bot to pause for a short while and appear to be typing each of its responses. 它运行良好,但是为了使交互看起来更自然,我希望我的机器人暂停一会儿,并输入每个响应。

I want the 'typing' bubble to be displayed briefly before each response my bot sends, ideally being able to define the time the bubble is visible for before the response is sent. 我希望在机器人发送的每个响应之前短暂显示“输入”气泡,理想情况下,它可以定义在发送响应之前气泡可见的时间。 At the moment there are sections of my conversation where the bot sends consecutive messages and they are all sent too quickly at once. 目前,我对话的某些部分中,该漫游器发送连续的消息,而这些消息一次都发送得太快。

The FB Messenger Send API says that either the 'message' or 'sender_action' property must be set . FB Messenger Send API表示必须设置'message'或'sender_action'属性 I've tried setting both like so: 我试过像这样设置两者:

const fbMessage = (id, text) => {

if(fruits.apples.indexOf(text) >= 0 || fruits.oranges.indexOf(text) >= 0) {

    var body = JSON.stringify({
        recipient: { id },
        "sender_action":"typing_on",
        message: {
            attachment: {
                  "type": "image",
                  "payload": {
                      "url": text
                  }
              }
          },
    });


} else {
    var body = JSON.stringify({
        recipient: { id },
        "sender_action":"typing_on",
        message: { text },
    });
}

const qs = 'access_token=' + encodeURIComponent(FB_PAGE_TOKEN);
return fetch('https://graph.facebook.com/me/messages?' + qs, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body,
})
.then(rsp => rsp.json())
.then(json => {
    if (json.error && json.error.message) {
    throw new Error(json.error.message);
    }
    return json;
});
};

But I get the following error: 但是我收到以下错误:

在此处输入图片说明

I'm not sure what I need to do - I'm assuming I've got to set up some sort of 'sender_action' bot response that's triggered before each conversational response but I don't know how I'd do this. 我不确定我需要做什么-我假设我必须设置某种“ sender_action”机器人响应,该响应会在每次会话响应之前触发,但我不知道该怎么做。

To display the typing bubble you simply send a sender action of typing_on. 要显示键入气泡,您只需发送一个sender_type的发送者动作即可。 This displays the typing indicator for up to 20 seconds, during which time you will send the actual message you want to send. 这将显示键入指示符,最多20秒钟,在此期间,您将发送要发送的实际消息。

The JSON for this would be: 的JSON将是:

{
  "recipient":{
    "id":"USER_ID"
  },
  "sender_action":"typing_on"
}

The call is documented here 通话记录在这里

Got it working, can't work out how to control bubble timing but it's fine for now. 知道了,无法解决如何控制气泡计时的问题,但目前还可以。 The code below will make the typing bubble display briefly before each of my bot's responses without mucking up the flow of my conversation. 下面的代码将在我的每一个机器人响应之前短暂地显示键入气泡,而不会影响我的对话流程。

FB Messenger code: FB Messenger代码:

const typingBubble = (id, text) => {

  var body = JSON.stringify({
      recipient: { id },
      "sender_action":"typing_on"
  });

  const qs = 'access_token=' + encodeURIComponent(FB_PAGE_TOKEN);
  return fetch('https://graph.facebook.com/me/messages?' + qs, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body,
  })
  .then(rsp => rsp.json())
  .then(json => {
    if (json.error && json.error.message) {
      throw new Error(json.error.message);
    }
    return json;
  });
};

const fbMessage = (id, text) => {

  if(scenarioCombos.trends.indexOf(text) >= 0 || scenarioCombos.disruptions.indexOf(text) >= 0) {

    var body = JSON.stringify({
        recipient: { id },
        message: {
            attachment: {
                  "type": "image",
                  "payload": {
                      "url": text
                  }
              }
          },
    });


  } else {
    var body = JSON.stringify({
        recipient: { id },
        message: { text },
    });
  }

  const qs = 'access_token=' + encodeURIComponent(FB_PAGE_TOKEN);
  return fetch('https://graph.facebook.com/me/messages?' + qs, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body,
  })
  .then(rsp => rsp.json())
  .then(json => {
    if (json.error && json.error.message) {
      throw new Error(json.error.message);
    }
    return json;
  });
};

Wit.ai send action code (within 'actions'): Wit.ai发送操作代码(在“操作”内):

send({sessionId}, {text}) {
    const recipientId = sessions[sessionId].fbid;
    if (recipientId) {
      return typingBubble(recipientId, text), fbMessage(recipientId, text)
    .then(() => null)
    .catch((err) => {
        console.error(
        'Oops! An error occurred while forwarding the response to',
        recipientId,
        ':',
        err.stack || err
        );
    });
    } else {
    console.error('Oops! Couldn\'t find user for session:', sessionId);
    return Promise.resolve()
    }
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM