====== 測試建構 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://at-blog.line.me/tw/create_developer_trial - https://developers.line.biz/en/ - 使用自己的 LINE 帳號登入 - Create new provider - Create new channel -> Messaging API -> Plan : Developer Trial ===== 建立開發環境 ===== * 安裝 php 與 httpd 相關套件 yum install epel-release rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm 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 vi /etc/php-fpm.d/www.conf : ;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: ; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific IPv4 address on ; a specific port; ; '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on ; a specific port; ; 'port' - to listen on a TCP socket to all addresses ; (IPv6 and IPv4-mapped) on a specific port; ; '/path/to/unix/socket' - to listen on a unix socket. ; Note: This value is mandatory. ;listen = 127.0.0.1:9000 listen = /var/run/php-fpm/php7.2-fpm.sock ; Set listen(2) backlog. ; Default Value: 511 (-1 on FreeBSD and OpenBSD) ;listen.backlog = 511 ; 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 = nobody ;listen.group = nobody ;listen.mode = 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 /etc/nginx/nginx.conf : server { listen 80 default_server; listen [::]:80 default_server; server_name _; root /usr/share/nginx/html; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; charset utf-8; gzip on; gzip_types text/css application/javascript text/javascript application/x-javascript image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon; location / { try_files $uri $uri/ /index.php?$query_string; } location /api/v0 { try_files $uri $uri/ /api_v0.php?$query_string; } location ~ \.php { include fastcgi.conf; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php-fpm/php7.2-fpm.sock; } location ~ /\.ht { 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://www.ichiayi.com/linebot/Webhook.php -> 按下 Verify 後要能夠看到 Success * 如果想要讓 Bot 可以被邀請加入某群組中 Allow bot to join group chats -> Enable ===== 安裝 line-bot-sdk-php ===== * 透過 composer 安裝 cd /usr/share/nginx/ composer require linecorp/line-bot-sdk cd vendor/linecorp/line-bot-sdk/ composer install ===== 寫一個 LINE BOT 程式 ===== * 至 https://developers.line.biz/console/ 選上面建立的 Provider -> channel Exp. iChiayi-Bot (Messaging API) * Channel settings : * QR Code : 可以讓手機 LINE 直接掃描加 BOT 當好友,也可以將 BOT 邀請到 LINE 群組內 * Channel secret : 提供後續開發時重要的 KEY, 大概長的像這樣629345c1d259db2e59c308e05b371f5x * Channel access token(long-lived) : 提供後續開發時重要的 KEY, 大概長的像這樣 U2nmqwuvXIA0h0JyDzeeSP/51YwnkbcCCw89BzCOCav9+cc+kTTWugtWdbw729e3VBfXt6B4NrCu9F3+yHWWC4Wx235D123+4J42IWSDrzj4PTiB68DkHN7W4dq4XQJi9kdpNpbw90EsdfrRqc3NtwdB04t89/1O/w1cDnyilFU= * 開始寫一個簡單的 BOT 程式 Webhook.php vi /usr/share/nginx/html/Webhook.php '); $bot = new \LINE\LINEBot($httpClient, ['channelSecret' => '']); ?> ===== 參考網址 ===== * https://developers.line.biz/en/docs/messaging-api/building-bot/ * https://developers.line.me/messaging-api/getting-started * https://github.com/line/line-bot-sdk-php * https://packagist.org/packages/linecorp/line-bot-sdk * https://webtatic.com/packages/php56/ * http://www.agix.com.au/install-composer-on-centosredhat/ * https://laracasts.com/discuss/channels/servers/problems-with-missing-php-extensions