-
-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathexample_bot.php
112 lines (103 loc) · 3.97 KB
/
example_bot.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
/**
* Copyright 2021 GoneTone
*
* Line Bot
* 範例 Example Bot 執行主文件
*
* 此範例 GitHub 專案:https://github.com/GoneToneStudio/line-example-bot-tiny-php
* 此範例教學文章:https://blog.reh.tw/archives/988
*
* 官方文檔:https://developers.line.biz/en/reference/messaging-api/
*/
date_default_timezone_set("Asia/Taipei"); //設定時區為台北時區
require_once('LINEBotTiny.php');
$channelAccessToken = '';
$channelSecret = '';
if (file_exists(__DIR__ . '/config.ini')) {
$config = parse_ini_file("config.ini", true); //解析配置檔
if ($config['Channel']['Token'] == null || $config['Channel']['Secret'] == null) {
error_log("config.ini 配置檔未設定完全!", 0); //輸出錯誤
} else {
$channelAccessToken = $config['Channel']['Token'];
$channelSecret = $config['Channel']['Secret'];
}
} else {
$configFile = fopen("config.ini", "w") or die("Unable to open file!");
$configFileContent = '; Copyright 2020 GoneTone
;
; Line Bot
; 範例 Example Bot 配置文件
;
; 此範例 GitHub 專案:https://github.com/GoneToneStudio/line-example-bot-tiny-php
; 此範例教學文章:https://blog.reh.tw/archives/988
;
; 官方文檔:https://developers.line.biz/en/reference/messaging-api/
[Channel]
; 請在雙引號內輸入您的 Line Bot "Channel access token"
Token = ""
; 請在雙引號內輸入您的 Line Bot "Channel secret"
Secret = ""
';
fwrite($configFile, $configFileContent); //建立文件並寫入
fclose($configFile); //關閉文件
error_log("config.ini 配置檔建立成功,請編輯檔案填入資料!", 0); //輸出錯誤
}
$message = null;
$event = null;
$client = new LINEBotTiny($channelAccessToken, $channelSecret);
foreach ($client->parseEvents() as $event) {
switch ($event['type']) {
case 'message': //訊息觸發
$message = $event['message'];
switch ($message['type']) {
case 'text': //訊息為文字
require_once('includes/text.php'); //Type: Text
require_once('includes/image.php'); //Type: Image
require_once('includes/video.php'); //Type: Video
require_once('includes/audio.php'); //Type: Audio
require_once('includes/location.php'); //Type: Location
require_once('includes/sticker.php'); //Type: Sticker
require_once('includes/imagemap.php'); //Type: Imagemap
require_once('includes/template.php'); //Type: Template
require_once('includes/flex.php'); //Type: Flex
break;
default:
//error_log("Unsupporeted message type: " . $message['type']);
break;
}
break;
case 'postback': //postback 觸發
//require_once('postback.php'); //postback
break;
case 'follow': //加為好友觸發
$client->replyMessage(array(
'replyToken' => $event['replyToken'],
'messages' => array(
array(
'type' => 'text',
'text' => '您好,這是一個範例 Bot OuO
範例程式開源至 GitHub (包含教學):
https://github.com/GoneTone/line-example-bot-php'
)
)
));
break;
case 'join': //加入群組觸發
$client->replyMessage(array(
'replyToken' => $event['replyToken'],
'messages' => array(
array(
'type' => 'text',
'text' => '大家好,這是一個範例 Bot OuO
範例程式開源至 GitHub (包含教學):
https://github.com/GoneTone/line-example-bot-php'
)
)
));
break;
default:
//error_log("Unsupporeted event type: " . $event['type']);
break;
}
}