【问题标题】:Is there a simple way to add a link to Botman replies?有没有一种简单的方法可以添加指向 Botman 回复的链接?
【发布时间】:2019-04-17 23:36:02
【问题描述】:

也许我没有为此使用正确的聊天机器人包(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.');
                }
            }
        });
    }

【问题讨论】:

  • 如果你把它放在一个按钮的值中,然后使用被点击的按钮的值重定向呢?就像对话中的号召性用语按钮一样
  • 好主意!我已经尝试过这个并更新了我的问题,因为我觉得我们更接近了,但现在我收到了 405 错误。
  • 您目前使用的是什么驱动程序?如果您使用的是 web 小部件之类的东西,这意味着您的聊天框架是页面内的 iframe,我想这就是重定向的问题。
  • 我猜你将不得不使用 javascript 检查该特定按钮,如果是这种情况,则在单击它时重定向
  • 是的,我能够做到这一点。确实很hacky。我会更新我的问题

标签: laravel chatbot


【解决方案1】:

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

【讨论】:

  • 请在此处发布您的答案,而不是作为问题的一部分。
猜你喜欢
  • 2011-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-17
  • 2012-07-08
  • 1970-01-01
  • 2020-04-18
  • 2010-10-25
相关资源
最近更新 更多