首页 > 解决方案 > BotMan 小部件未回复,使用 PHP 版本 7.4.19 + Win 10 + XXAMP

问题描述

我正在测试机器人聊天。遵循https://botman.io/2.0/installation上的文档

脚本打开右下角的聊天小部件,但是当我输入你好时,它没有回复。尝试了多个论坛,但没有得到任何解决方案。

下面是我正在使用的脚本

安装了botman

composer require botman/botman

网络导出器

composer require botman/driver-web

索引.php

<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>COINS ChatBot</title>
        <!-- <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/botman-web-widget@0/build/assets/css/chat.min.css"> -->
    </head>
    <body>
<script>
// Feel free to change the settings on your need
    var botmanWidget = {
        frameEndpoint: '/chat.html',
        chatServer: 'chat.php',
        title: 'ChatBot',
        introMessage: 'Hi and welcome to our chatbot how can i help you ?',
        placeholderText: 'Ask Me Something',
        mainColor: '#F28240',
        bubbleBackground: '#F28240',
        aboutText: '',
        aboutLink: '',
        bubbleAvatarUrl: 'https://www.applozic.com/assets/resources/images/Chat-Bot-Icon@512px.svg'
    };
        </script>
        <script src='https://cdn.jsdelivr.net/npm/botman-web-widget@0/build/js/widget.js'></script>
    </body>
</html>

聊天.html

<html>
<head>
    <title>BotMan Widget</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/botman-web-widget@0/build/assets/css/chat.min.css">
</head>
<body>
<script id="botmanWidget" src='https://cdn.jsdelivr.net/npm/botman-web-widget@0/build/js/chat.js'></script>
</body>
</html>

聊天.php

<?php
   require_once 'vendor/autoload.php';

   use BotMan\BotMan\BotMan;
   use BotMan\BotMan\BotManFactory;
   use BotMan\BotMan\Drivers\DriverManager;

   
   $config = [
    // Your driver-specific configuration
    // "telegram" => [
    //    "token" => "TOKEN"
    // ]
];

   DriverManager::loadDriver(\BotMan\Drivers\Web\WebDriver::class);

   $botman = BotManFactory::create($config);

  // Give the bot something to listen for.
$botman->hears('hello', function (BotMan $bot) {
    $bot->reply('Hello yourself.');
});

$botman->hears('Good Morning', function (BotMan $bot) {
    $bot->reply('Hey John');
});

$botman->hears('what is the time in {city} located in {continent}' , function (BotMan $bot,$city,$continent) {
     date_default_timezone_set("$continent/$city");
      $reply = "The time in ".$city." ".$continent." is ".date("h:i:sa");
    $bot->reply($reply);
});

$botman->fallback(function($bot) {
    $bot->reply('Sorry, I did not understand these commands. Here is a list of commands I understand: ...');
});

// Start listening
$botman->listen();
?>

检查后我发现缓存可能有问题,我已经安装了 APCU PHP Cache 但仍然没有运气。

标签: phpbotman

解决方案


推荐阅读