這不是侯捷大師的網站,只是一個姓名英譯相似的人企圖魚目混珠....

[PHP-LineBot]01:超簡單LineBot教學 : 關鍵字回應


功能 : 針對事先建檔的關鍵字對應表,去偵測關鍵字

事前準備 :
45f37302-91e1-4ef1-a50b-941ca66c1128



實際運作圖 :
07dab95b-3d10-410a-af87-47863b6e9db6

程式碼 :
GitHub: https://github.com/cdwalkman/a-little-tiny-line-bot-php.git
請直接將原始碼Clone下來,放置到 C:\xampp\htdocs\a-little-tiny-line-bot-php 即可
(磁碟機代號跟xampp路徑請自行置換)
理論上只有 LinebotConfig.php 裡面的兩個地方要動而已,也就是將 Token 要換成您申請的即可。
另外因為我們將 LineBot 程式放置於 a-little-tiny-line-bot-php 資料夾中,因此 Webhook 認證網址也要記得加上資料夾路徑。
之前剛嘗試的時候往往都掛在Verify這一步,我只能說...祈禱人品爆發吧 (真不科學XD)
506015531

最後掃條碼將 LINEBot 將入好友,就可以開始玩了。
b2965a12-b26a-435c-b464-5f41924f5244



完整程式碼 :

GitHub: https://github.com/cdwalkman/a-little-tiny-line-bot-php.git
第一次也將程式碼放在Blog上面,但之後都一律丟上GitHub,各階段陸續添加的功能也會以 Commit 作為分隔點,請諸位大德明鑑。

index.php
<?php
/**
 * Copyright 2016 LINE Corporation
 *
 * LINE Corporation licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
require_once('LINEBotTiny.php');
require_once('LinebotConfig.php');
// create LineBot
$client = new LINEBotTiny($channelAccessToken, $channelSecret);
foreach ($client->parseEvents() as $event) {
    $messageTYPE = $event['message']['type'];
    
    switch ($event['type']) {                      
        case 'message':
            $message = $event['message'];                            
                                                 
            //detect keywords and get reply message
            $json = file_get_contents('https://spreadsheets.google.com/feeds/list/1uzIBH3rdZerzvyl9ddoGspIIBr9oMjgJHrwJBPwbnx0/od6/public/values?alt=json');
            $data = json_decode($json, true);
            foreach ($data['feed']['entry'] as $item) {
                $keywords = explode(',', $item['gsx$keywords']['$t']);
                foreach ($keywords as $keyword) {
                    if (mb_strpos($message['text'], $keyword) !== false) {
                        $reply_msg  = $item['gsx$replymessage']['$t'];
                   }
               }
           }
            //reply message
            switch ($messageTYPE) {
                case 'text': 
                $client->replyMessage(array(
                    'replyToken' => $event['replyToken'],
                    'messages' =>array(                       
                            array(
                                'type' => 'text',
                                'text' => $reply_msg,
                             ),                    
                   ),
               ));
                    break;                         
                default:
                    // Apache(error.log)
                    error_log("Unsupporeted message type: " . $messageTYPE);
                    break;
           }
            break;
        default:
            // Apache(error.log)
            error_log("Unsupporeted event type: " . $event['type']);
            break;
   }
};




LinebotConfig.php
<?PHP
// Line Auth
$channelAccessToken = 'your channelAccessToken';
$channelSecret = 'your channelSecret';
?>

LinebotConfig.php 裡面請填上您申請的兩個 Token
之後我的 .gitignore 檔案裏面也將會排除掉 LinebotConfig.php 這個檔案
後面陸續添加的密碼或token類的參數會在Blog中明示,屆時請您手動添加進去即可。



後記 : 

說實在的也是因為當初公司有在評估用 LineBot 來蒐集轉發使用者丟在Line群組上面的一些線上問題,才利用下班時間找資料,東拼西湊硬幹弄起來的,所以沒啥PHP框架的概念,都是土砲出來的,大家笑笑就好XD。

也因為當初吃了不少苦頭,因此這次將所有東西都寫在同一頁中,期盼有緣人能夠以最快速的方式將LineBot搞上去,當然日後功能變多之後將會對原始碼做一些優化處理。
























SHARE

Milan Tomic

Hi. I’m Designer of Blog Magic. I’m CEO/Founder of ThemeXpose. I’m Creative Art Director, Web Designer, UI/UX Designer, Interaction Designer, Industrial Designer, Web Developer, Business Enthusiast, StartUp Enthusiast, Speaker, Writer and Photographer. Inspired to make things looks better.

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 意見:

張貼留言

[PHP-LineBot]01:超簡單LineBot教學 : 關鍵字回應

功能 : 針對事先建檔的關鍵字對應表,去偵測關鍵字 事前準備 : 開發環境準備 網頁伺服器+資料庫架設 : XAMPP 讓架設的網站可以被外部存取 : ngrok 任何您喜歡的文字編輯器或是IDE工具來撰寫PHP程式,例如 ATOM 或 Visual Stud...