laravel - 有没有一种简单的方法可以添加指向 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.');
}
}
});
}
解决方案
我通过使用 javascript 在框架中插入链接解决了这个问题。有关详细信息,请参阅我的编辑
推荐阅读
- python - Django:使用默认过滤器扩展 QuerySet
- python-3.x - ModuleNotFoundError:尽管模型似乎已安装,但没有名为“matplotlib”的模块
- google-cloud-platform - Airflow 的 DataprocPySparkOperator 一小时后超时(使用 Google Cloud Composer)
- excel - 使用 MID 和 FIND 将字符串中的数据提取到 Excel 单元格中
- ssis - 在 Foreach 循环中将数据插入到 Dynamics crm
- php - 如何通过 Script\Event 获取作曲家自动加载类映射?
- ios - 缺少手表扩展的本地化
- sql - 如何在匹配的列数据上“汇总”行并在 SQL Server 中为不匹配的数据创建新列
- python - 使用每行和向量之间的余弦距离创建数据框列
- python - Python 用于字符串列表,如果字符串包含字符,则将其与下一个列表元素连接