首页 > 解决方案 > 有没有一种简单的方法可以添加指向 Botman 回复的链接?

问题描述

也许我没有为此使用正确的聊天机器人包(botman)。如果可能的话,我想避免使用 facebook 驱动程序。我的理想方案是听一个问题,然后通过链接快速回复。

$bot->reply('click <a href="http://google.com">here</a> for answers.');

但这仅呈现文本。

// controller code, activated after a hears() match
public function askReason()
    {
        $question = Question::create("If you have questions about $this->town, please visit their community page here:")
            ->fallback('Unable to ask question')
            ->callbackId('ask_reason')
            ->addButtons([
                Button::create('Visit')->value('visit'),
                Button::create('Ask Something Else')->value('continue')
            ]);

        return $this->ask($question, function (Answer $answer) {
            if ($answer->isInteractiveMessageReply()) {
                if ($answer->getValue() === 'visit') {
                    header("Location: http://google.com");
                    exit();
                } else {
                    $this->say('Alright, lets talk about something else.');
                }
            }
        });
    }

    /**
     * Start the conversation
     */
    public function run()
    {
        $this->askReason();
    }

This throws a 405 error when the 'visit' option is chosen and I cannot change the header via xhr. 我也试过'return redirect(" http://google.com ")'

有谁知道如何用简单的链接、重定向、纯文本以外的任何内容在 botman 中回复?

编辑 这是我的解决方案。在进入 iframe 的聊天窗口中,我添加了 DOMNode 插入检查并手动添加了链接。

<script>
var ready = true;

// set interval
setInterval(setready, 1000);
function setready() {
  ready = true;
}

$(document).on('DOMNodeInserted', "#messageArea", function() {
  if(ready == true)
  {
  setTimeout(replacelink, 200);
  ready = false;
  }
});

function replacelink() {
  $('#messageArea .btn').each(function(){
      text = $(this).html();
      link = text.match(/(Link:)\s(\/[^<]*)/g);
      if(link)
      {
        $(this).html(' ');
        url = link.toString().substring(5);
        text = text.match(/(.*)(Link:)/g).toString().substring(0,5);
        $(this).empty();
        $(this).html('<a href="' + url + '">' + text + '</a>');
        $(this).addClass('linked');
      }
      else
      {
        $(this).addClass('linked');
      }
  });
}

</script>

每次发送消息时窗口似乎都会重新加载,因此代码必须每次都运行(例如,您不能更改就绪检查功能以查找我尝试过的“链接”类。我保留它以用于 css 转换,隐藏按钮,直到它们被链接起来。)在对话中,我制作了这样的链接:

public function askTown()
    {
        $slug = str_slug($this->town, '-');
        $question = Question::create("If you have questions about $this->town, please visit their community page here:")
            ->fallback('Unable to ask question')
            ->callbackId('ask_reason')
            ->addButtons([
                Button::create('Visit Link: /city/'.$slug)->value('visit'),
                Button::create('Ask Something Else')->value('continue')
            ]);

        return $this->ask($question, function (Answer $answer) {
            if ($answer->isInteractiveMessageReply()) {
                if ($answer->getValue() === 'visit') {
                    $this->say('Glad I could help!');
                } else {
                    $this->say('Alright, let's talk about something else.');
                }
            }
        });
    }

标签: laravelchatbot

解决方案


我通过使用 javascript 在框架中插入链接解决了这个问题。有关详细信息,请参阅我的编辑


推荐阅读