我尝试使用 Twilio 和 Laravel 复制以下博客文章中构建的 Whatsapp 聊天机器人: https://www.twilio.com/blog/build-whatsapp-currency-conversion-bot-twilio-laravel
我已经尝试复制这篇文章两次。但是,当我通过发送消息来测试机器人时,我在 Twilio 上收到一条错误消息:11200 - Http 检索失败。以下是 twilio 错误消息的更多详细信息:
调试事件SID
NOa58ef5b2f604abf9ea4718ea95d5ede4
服务SID
SM28801242e0831dd259f663885446f69f
资源SID
SM28801242e0831dd259f663885446f69f
时间戳
世界标准时间 2023 年 4 月 26 日上午 11:28
留言
URI null 的 URI 方案必须等于(忽略大小写)'http'、'https'、'ws' 或 'wss'
我尝试使用 ngrok 和 sail share 来使应用程序可用,但是,两者都不起作用。我收到这两个错误“500 内部服务器错误”。谁能告诉我可能的解决方案是什么?
如果需要任何其他信息,请告知。
机器人控制器:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\CurrencyConverterClient;
use Twilio\TwiML\MessagingResponse;
use App\Currency;
use Exception;
class BotController extends Controller
{
protected $client;
public function __construct(CurrencyConverterClient $client)
{
$this->client = $client;
}
public function sendReplies(Request $request)
{
$response = new MessagingResponse();
$body = $request->input('Body');
$content = $this->determineMessageContent($body);
$response->message($content);
return $response;
}
private function determineMessageContent(string $content)
{
$formatContent = strtolower($content);
if (strpos($formatContent, 'hello') !== false) {
$message = "Welcome to the WhatsApp Bot for Currency Conversion \n";
$message .= "Use the following format to chat with the bot \n";
$message .= "Convert 5 USD to NGN \n";
return $message;
}
if (strpos($formatContent, 'convert') !== false) {
return $this->formatContentForConversion($formatContent);
}
return $this->formatContentForInvalidMessageFormat();
}
private function formatContentForConversion($formatContent)
{
$contentInArray = explode(" ", $formatContent);
$itemsInArray = count($contentInArray);
if ($itemsInArray < 5 || $itemsInArray > 5) {
return $this->formatContentForInvalidMessageFormat();
}
return $this->performConversion($contentInArray);
}
private function formatContentForInvalidMessageFormat()
{
$message = "The Conversion Format is Invalid \n";
$message .= "Please use the format \n";
$message .= "Convert 5 USD to NGN";
return $message;
}
private function performConversion(array $contentInArray)
{
$amount = $contentInArray[1];
$baseCurrency = strtoupper($contentInArray[2]);
$toCurrency = strtoupper($contentInArray[4]);
if (!is_numeric($amount)) {
return "Please provide a valid amount";
}
$items = $this->getCurrencyCode($baseCurrency, $toCurrency);
if ($items->count() < 2) {
return "Please enter a valid Currency Code";
}
try {
$convertedAmount = $this->client->convertCurrency($amount, $baseCurrency, $toCurrency);
return "{$amount} {$baseCurrency} is {$convertedAmount} {$toCurrency}";
} catch (Exception $e) {
return "We could not perform this conversion now, please bear with us";
}
}
private function getCurrencyCode(string $baseCurrency, string $currency)
{
$items = [$baseCurrency, $currency];
$currencyCode = Currency::findByCurrencyCode($items);
return $currencyCode;
}
}
CurrencySeeder.php:
<?php
namespace Database\Seeders;
use App\CurrencyConverterClient;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class CurrencySeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run(CurrencyConverterClient $client)
{
$currencies = $client->getSupportedCurrencies();
foreach ($currencies as $currency => $key) {
foreach ($key as $k) {
DB::table('currencies')->insert([
'name' => $k['currencyName'],
'code' => $k['id']
]);
}
}
}
}
web.php
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::post('/currency', 'BotController@sendReplies');
我解决了这个问题。问题出在我的路由上。这是因为 Laravel 8.x 中的更改。查看这篇文章:使用 Laravel 8 时出现错误“目标类控制器不存在”