有没有简单的方法可以添加指向Botman回复的链接?

时间:2019-04-17 23:36:02

标签: laravel chatbot

也许我没有为此使用正确的chatbot软件包(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();
    }

当选择“ visit”选项并且我无法通过xhr更改标头时,这将引发405错误。我也尝试过'return redirect(“ http://google.com”)'

除了纯文本外,有人知道如何通过简单的链接,重定向或其他方式在botman中答复吗?

修改 这是我的解决方案。在进入ifram的聊天窗口中,我添加了对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>

每次发送消息时,窗口似乎都会重新加载,因此代码必须每次都运行(例如,您不能更改ready check函数来查找我尝试过的“链接”类。 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.');
                }
            }
        });
    }

1 个答案:

答案 0 :(得分:0)

我在前端通过使用javascript将链接插入框架中解决了此问题。详情请参阅我的修改