差異處
這裏顯示兩個版本的差異處。
| 兩邊的前次修訂版 前次修改 | |||
| tech:linebot [2019/04/10 09:03] – [安裝 line-bot-sdk-php] jonathan_tsai | tech:linebot [2019/04/10 09:23] (目前版本) – [建立一個 LINE BOT] jonathan_tsai | ||
|---|---|---|---|
| 行 1: | 行 1: | ||
| + | ====== 測試建構 LINE BOT (Developer Trial 方案) ====== | ||
| + | |||
| + | * 環境 : CentOS Linux release 7.6.1810 (Core) | ||
| + | * 語言 : PHP 7.2 | ||
| + | * Web : Nginx 1.12.2 | ||
| + | * DB : MariaDB 5.5.60 | ||
| + | |||
| + | ===== 申請Developer Trial帳號 ===== | ||
| + | * 參考 - http:// | ||
| + | - https:// | ||
| + | - 使用自己的 LINE 帳號登入 | ||
| + | - Create new provider | ||
| + | - Create new channel -> Messaging API -> Plan : Developer Trial | ||
| + | |||
| + | ===== 建立開發環境 ===== | ||
| + | * 安裝 php 與 httpd 相關套件< | ||
| + | yum install epel-release | ||
| + | rpm -Uvh https:// | ||
| + | yum install composer mariadb mariadb-server nginx unzip php72w php72w-cli php72w-common php72w-curl php72w-fpm php72w-gd php72w-mbstring php72w-mysqlnd php72w-process php72w-snmp php72w-xml php72w-zip php72w-opcache | ||
| + | </ | ||
| + | * 設定 Web server - nginx <code sh> | ||
| + | vi / | ||
| + | </ | ||
| + | : | ||
| + | ;user = apache | ||
| + | user = nginx | ||
| + | ; RPM: Keep a group allowed to write in log dir. | ||
| + | group = apache | ||
| + | |||
| + | ; The address on which to accept FastCGI requests. | ||
| + | ; Valid syntaxes are: | ||
| + | ; ' | ||
| + | ; a specific port; | ||
| + | ; ' | ||
| + | ; a specific port; | ||
| + | ; ' | ||
| + | ; (IPv6 and IPv4-mapped) on a specific port; | ||
| + | ; '/ | ||
| + | ; Note: This value is mandatory. | ||
| + | ;listen = 127.0.0.1: | ||
| + | listen = / | ||
| + | |||
| + | ; Set listen(2) backlog. | ||
| + | ; Default Value: 511 (-1 on FreeBSD and OpenBSD) | ||
| + | ; | ||
| + | |||
| + | ; Set permissions for unix socket, if one is used. In Linux, read/write | ||
| + | ; permissions must be set in order to allow connections from a web server. Many | ||
| + | ; BSD-derived systems allow connections regardless of permissions. | ||
| + | ; Default Values: user and group are set as the running user | ||
| + | ; mode is set to 0660 | ||
| + | ; | ||
| + | ; | ||
| + | ; | ||
| + | listen.owner = nginx | ||
| + | listen.group = nginx | ||
| + | listen.mode = 0660 | ||
| + | |||
| + | ; When POSIX Access Control Lists are supported you can set them using | ||
| + | ; these options, value is a comma separated list of user/group names. | ||
| + | : | ||
| + | </ | ||
| + | systemctl enable php-fpm | ||
| + | systemctl restart php-fpm | ||
| + | |||
| + | vi / | ||
| + | </ | ||
| + | : | ||
| + | server { | ||
| + | listen | ||
| + | listen | ||
| + | server_name | ||
| + | root / | ||
| + | |||
| + | # Load configuration files for the default server block. | ||
| + | include / | ||
| + | |||
| + | | ||
| + | gzip on; | ||
| + | | ||
| + | | ||
| + | try_files $uri $uri/ / | ||
| + | } | ||
| + | | ||
| + | try_files $uri $uri/ / | ||
| + | } | ||
| + | | ||
| + | include fastcgi.conf; | ||
| + | fastcgi_split_path_info ^(.+\.php)(/ | ||
| + | fastcgi_pass unix:/ | ||
| + | } | ||
| + | | ||
| + | deny all; | ||
| + | } | ||
| + | |||
| + | |||
| + | error_page 404 /404.html; | ||
| + | location = /40x.html { | ||
| + | : | ||
| + | </ | ||
| + | systemctl enable nginx | ||
| + | systemctl restart nginx | ||
| + | </ | ||
| + | |||
| + | ===== 驗證 Webhook 是否可以正確連上 ===== | ||
| + | |||
| + | * 假設 Channel 為 iChiaYi-Bot | ||
| + | * Use webhooks -> Enable | ||
| + | * 假設 Webhook URL 為 https:// | ||
| + | * 如果想要讓 Bot 可以被邀請加入某群組中 Allow bot to join group chats -> Enable | ||
| + | |||
| + | ===== 安裝 line-bot-sdk-php ===== | ||
| + | * 透過 composer 安裝 <code sh> | ||
| + | cd / | ||
| + | composer require linecorp/ | ||
| + | cd vendor/ | ||
| + | composer install | ||
| + | </ | ||
| + | |||
| + | ===== 寫一個 LINE BOT 程式 ===== | ||
| + | * 至 https:// | ||
| + | * Channel settings : | ||
| + | * QR Code : 可以讓手機 LINE 直接掃描加 BOT 當好友,也可以將 BOT 邀請到 LINE 群組內 | ||
| + | * Channel secret : 提供後續開發時重要的 KEY, 大概長的像這樣< | ||
| + | * Channel access token(long-lived) : 提供後續開發時重要的 KEY, 大概長的像這樣< | ||
| + | U2nmqwuvXIA0h0JyDzeeSP/ | ||
| + | </ | ||
| + | * 開始寫一個簡單的 BOT 程式 Webhook.php< | ||
| + | vi / | ||
| + | </ | ||
| + | <?php | ||
| + | require '/ | ||
| + | $httpClient = new \LINE\LINEBot\HTTPClient\CurlHTTPClient('< | ||
| + | $bot = new \LINE\LINEBot($httpClient, | ||
| + | ?> | ||
| + | </ | ||
| + | |||
| + | ===== 參考網址 ===== | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * http:// | ||
| + | * https:// | ||