dueros / bot-sdk Goto Github PK
View Code? Open in Web Editor NEW度秘bot开发的PHP版SDK
License: Apache License 2.0
度秘bot开发的PHP版SDK
License: Apache License 2.0
什么时候会更新DBL额
发现使用此sdk接入智能家居的时候,请求的内容结构完全变了,https://dueros.baidu.com/didp/doc/dueros-bot-platform/dbp-smart-home/protocol/intro-protocol_markdown
sdk直接报错,看源代码是不支持智能家居的
自行安装PHP 7.1.* 和 Nginx
相关教程: https://github.com/mmjjb/Centos7-PHP7.1-Nginx
为了开发简单,可以关闭SElinux
按照这个教程安装完成之后,还需要安装一下后面会用到的依赖
yum --enablerepo=remi-safe -y install php71-php-zip
安装composer, 方便安装相关PHP 开发依赖包,具体链接参考此处: https://pkg.phpcomposer.com/#how-to-install-composer
composer 建议全局安装
php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');"
php composer-setup.php
php -r "unlink('composer-setup.php');"
sudo mv composer.phar /usr/local/bin/composer
修改composer服务 修改之后,可以有效加速相关依赖包的安装
composer config -g repo.packagist composer https://packagist.phpcomposer.com
安装Laravel 命令
composer global require "laravel/installer"
这个命令会安装一个laravel
命令,如果是最新的composer
,命令应该会在$HOME/.config/composer/vendor/bin
文件夹下面,所以最好是把$HOME/.config/composer/vendor/bin
加入到PATH中去
创建一个Laravel 项目
我们可以创建一个/www/sites
的目录来放网站源码【如果开启了SElinux,可以直接在/var/www 目录下面创建项目】
cd /www/sites/
laravel new dueros.bot
配置laravel 项目程序目录,很重要 不然会报错的;
给这两个文件配置读写权限:storage 目录和 bootstrap/cache 目录应该允许 Web 服务器写入,否则 Laravel 将无法运行。
cd /www/sites/dueros.bot
chmod -R 777 storage
chmod -R 777 bootstrap/cache
配置Nginx Server
server {
listen 80;
server_name www.domain.com;
#charset koi8-r;
#access_log /www/logs/www.domain.com.access.log main;
#error_log /www/logs/www.domain.com-error.log error;
root /www/sites/dueros.bot/public/;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-cgi alone:
fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SERVER_NAME $host;
}
}
重新启动一下nginx服务,这样laravel的基础项目我们就创建好了
sudo service nginx reload
重启之后,访问nginx server 里面配置域名,应该就可以访问到一个默认如下的网页了
请你可以先在Dueros的DBP平台创建好一个测试技能待用,
现在所有的工作都在基于上面的/www/sites/dueros.bot 目录下完成了。
我们先查看一下这个目录都包含那些文件
drwxrwxr-x 6 somebody somebody 4096 Oct 11 09:57 app
-rw-rw-r-- 1 somebody somebody 1686 Oct 11 09:57 artisan
drwxrwxr-x 3 somebody somebody 4096 Oct 11 09:57 bootstrap
-rw-rw-r-- 1 somebody somebody 1380 Oct 11 09:57 composer.json
-rw-rw-r-- 1 somebody somebody 138803 Oct 11 09:57 composer.lock
drwxrwxr-x 2 somebody somebody 4096 Oct 11 09:57 config
drwxrwxr-x 5 somebody somebody 4096 Oct 11 09:57 database
-rw-rw-r-- 1 somebody somebody 1129 Oct 11 09:57 package.json
-rw-rw-r-- 1 somebody somebody 1040 Oct 11 09:57 phpunit.xml
drwxrwxr-x 4 somebody somebody 4096 Oct 11 09:57 public
drwxrwxr-x 5 somebody somebody 4096 Oct 11 09:57 resources
drwxrwxr-x 2 somebody somebody 4096 Oct 11 09:57 routes
-rw-rw-r-- 1 somebody somebody 563 Oct 11 09:57 server.php
drwxrwxrwx 5 somebody somebody 4096 Oct 11 09:57 storage
drwxrwxr-x 4 somebody somebody 4096 Oct 11 09:57 tests
drwxrwxr-x 36 somebody somebody 4096 Oct 11 09:57 vendor
-rw-rw-r-- 1 somebody somebody 549 Oct 11 09:57 webpack.mix.js
-rw-rw-r-- 1 somebody somebody 219070 Oct 11 09:57 yarn.lock
执行以下命令安装dueros的bot-sdk
composer require dueros/bot-sdk
现在我们就来开发dueros的程序
我们先创建一个放Dueros 程序的目录, 执行下面的命令
cd /www/sites/dueros.bot/app/Http/Controllers
mkdir Dueros
我们把程序都放在Dueros这个目录中
进入Dueros 目录
cd Dueros
新建一个文件Bot.php
内容如下
<?php
namespace App\Http\Controllers\Dueros;
use Baidu\Duer\Botsdk\Bot as DuerosBot;
class Bot extends DuerosBot
{
public function __construct($postData = [])
{
parent::__construct($postData);
$this->addLaunchHandler(function () {
return [
'card' => null,
'outputSpeech' => 'Hi!',
'directives' => [],
'resource' => null,
'reprompt' => null,
];
});
$this->addSessionEndedHandler(function () {
return [
'card' => null,
'outputSpeech' => 'Bye!',
'directives' => [],
'resource' => null,
'reprompt' => null,
];
});
$this->addIntentHandler('say_hello', function () {
return [
'card' => null,
'outputSpeech' => 'Hello Word!',
'directives' => [],
'resource' => null,
'reprompt' => null,
];
});
}
}
继续在这个目录新建一个文件BotController.php, 这个文件将用来接收相关的网络请求,程序如下
<?php
namespace App\Http\Controllers\Dueros;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
class BotController extends Controller
{
public function post(Request $request)
{
$postData = file_get_contents('php://input');
Log::info($postData);
$bot = new Bot();
return $bot->run();
}
}
然后修改Laravel的路由配置,来接收post 请求
首先进入这个目录
cd /www/sites/dueros.bot/routes/
然后修改里面的api.php, 添加以下路由
Route::group([
'namespace' => 'Dueros',
], function () {
Route::post('/hi', 'BotController@post');
});
至此我们已经开发完成
现在我们就可以开始测试了,把路径填入到Dueros DBP开发平台的webhook 服务里面。路径地址型如:
http://www.domain.com/api/hi
你也可以使用Postman 直接发送POST 请求到此地址http://www.domain.com/api/hi
进行测试
exception: Undefined index: images
音乐播放指令
AudioPlayer.Play
use \Baidu\Duer\Botsdk\Directive\AudioPlayer\Play;
$directive = new Play('http://www.music', Play::REPLACE_ALL);
return [
'directives' => [$directive],
'outputSpeech' => '正在为你播放歌曲',
];
use \Baidu\Duer\Botsdk\Directive\AudioPlayer\Play;
$directive = new Play('http://www.music1', Play::ENQUEUE);
$directive2 = new Play('http://www.music2', Play::ENQUEUE);
return [
'directives' => [$directive, $directive2],
'outputSpeech' => '正在为你播放歌曲',
];
As in my code, Dueros just play the first one music.
This is the exception:
file_put_contents(/www/sites/*******/vendor/dueros/bot-sdk/src/f4354d10b358b0e176fbd65699535113):
failed to open stream: Permission denied {"exception":"[object] (ErrorException(code: 0): file_put_contents(/www/sites/*******/vendor/dueros/bot-sdk/src/f4354d10b358b0e176fbd65699535113):
failed to open stream: Permission denied at /www/sites/*******/vendor/dueros/bot-sdk/src/Certificate.php:62)
In my tech stack, the vendor directory is not writeable, I think there should a method to change the directory where to write the RSA key.
My dueros/bot-sdk version is: v2.1.8
Lines 651 to 662 in 98eb71f
这两个函数都调用了 $this->response->addExpectTextResponse($slot);
addExpectSlotResponse
貌似应该调用 $this->response->addExpectSlotResponse
才对吧?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.