【问题标题】:Sending messages with delay延迟发送消息
【发布时间】:2018-11-09 18:02:47
【问题描述】:

我正在实现一个 Facebook Messenger 聊天机器人,在其中一个对话流中,该机器人应该一个接一个地发送 6 条消息。

我希望将这些消息延迟 1 秒,并在它们之间显示发件人操作,以使对话感觉自然(而不是一次转储 6 条消息,这会迫使用户向上滚动阅读全部)。

我尝试了 2 种不同的 webhook 实现,但它们都不起作用。一个是在 Python/Flask 中:在每条消息之间,我输入了time.sleep(delay),但它不起作用。另一个在 Javascript/NodeJS 中:在每条消息之间,我已经输入了setTimeout(function() {sendMessage(recipient_id);}, delay),但它也不起作用。两个版本都能完美运行,没有延迟。

谁能帮忙?

【问题讨论】:

    标签: chatbot facebook-messenger facebook-messenger-bot facebook-chatbot


    【解决方案1】:

    您可以使用下面的代码,它会等待 1 秒,然后使用 async/await 回复。

    const messages = ["first", "second", "third", "forth", "fifth", "sixth"];
    
    const sleep = delay => {
      return new Promise(function(resolve) {
        setTimeout(resolve, delay);
      });
    };
    
    const displayMessage = async messages => {
      for (let message of messages) {
        await sleep(1 * 1000);
        console.log(message);
      }
    };
    
    displayMessage(messages);
    

    【讨论】:

      【解决方案2】:

      您可以在此类情况下使用 settimeout。但是为了显示 sender_action 假设你想在 messenger 中显示像 typing... 这样的文本,那么 facebook 在其 messenger API 中提供了一些功能,以包含具有不同标签的发件人操作。这是我的做法。

      sender_action: 'typing...',
      messaging_type: 'MESSAGE_TAG',
      tag: 'NON_PROMOTIONAL_SUBSCRIPTION',
      

      请查看以下链接以获取更多信息。 https://developers.facebook.com/docs/messenger-platform/send-messages/sender-actions

      【讨论】:

        【解决方案3】:

        如果您在提问时提供更多代码会更好。我怀疑你实际上是这样做的:

        setTimeout(function() {sendMessage(recipient_id);}, delay)
        setTimeout(function() {sendMessage(recipient_id);}, delay)
        setTimeout(function() {sendMessage(recipient_id);}, delay)
        setTimeout(function() {sendMessage(recipient_id);}, delay)
        setTimeout(function() {sendMessage(recipient_id);}, delay)
        setTimeout(function() {sendMessage(recipient_id);}, delay)
        

        setTimeout 是异步的,这意味着您的代码将等待 1 秒,然后连续发送 6 条消息。你可能正在寻找这样的东西:

        await setTimeout(function() {sendMessage(recipient_id);}, delay)
        await setTimeout(function() {sendMessage(recipient_id);}, delay)
        await setTimeout(function() {sendMessage(recipient_id);}, delay)
        await setTimeout(function() {sendMessage(recipient_id);}, delay)
        await setTimeout(function() {sendMessage(recipient_id);}, delay)
        await setTimeout(function() {sendMessage(recipient_id);}, delay)
        

        【讨论】:

          【解决方案4】:

          您可以查看wingbothere on the github。它有助于构建一个简单的机器人。像这样:

          const { Router } = require('wingbot');
          
          const bot = new Router();
          
          bot.use('start', (req, res) => {
             res.typingOn()
                .wait(1000)
                .text('Hello');
          });

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-02-15
            • 1970-01-01
            • 1970-01-01
            • 2014-05-14
            • 1970-01-01
            • 1970-01-01
            • 2016-09-28
            • 1970-01-01
            相关资源
            最近更新 更多