forked from GoneToneStudio/line-example-bot-tiny-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_bot.php
87 lines (83 loc) · 3.13 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
<?php
/**
* Copyright 2017 GoneTone
*
* Line Bot
* 範例 Example Bot 執行主文件
*
* 此範例 GitHub 專案:https://github.com/GoneTone/line-example-bot-php
* 官方文檔:https://devdocs.line.me/en/
*/
error_reporting(0); // 不顯示錯誤 (Debug 時請註解掉)
date_default_timezone_set("Asia/Taipei"); // 設定時區為台北時區
require_once('LINEBotTiny.php');
if (file_exists(__DIR__ . '/config.php')) {
$config = include __DIR__ . '/config.php'; // 引入設定檔
if ($config['channelAccessToken'] == Null || $config['channelSecret'] == Null) {
error_log("config.php 設定檔內的驗證權杖和粉絲專頁存取權杖尚未設定完全!", 0); // 輸出錯誤
} else {
$channelAccessToken = $config['channelAccessToken'];
$channelSecret = $config['channelSecret'];
}
} else {
$configFile = fopen("config.php", "w") or die("Unable to open file!");
$configFileContent = "<?php
/**
* Copyright 2017 GoneTone
*
* Line Bot
* 範例 Example Bot 配置文件
*
* 此範例 GitHub 專案:https://github.com/GoneTone/line-example-bot-php
* 官方文檔:https://devdocs.line.me/en/
*/
return [
'channelAccessToken' => '',
'channelSecret' => ''
];
?>";
fwrite($configFile, $configFileContent); // 建立文件並寫入
fclose($configFile); // 關閉文件
error_log("config.php 設定檔建立成功,請編輯檔案輸入驗證權杖和粉絲專頁存取權杖!", 0); // 輸出錯誤
}
$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
break;
default:
//error_log("Unsupporeted message type: " . $message['type']);
break;
}
break;
case 'postback':
//require_once('postback.php'); // postback
break;
default:
//error_log("Unsupporeted event type: " . $event['type']);
$client->replyMessage(array(
'replyToken' => $event['replyToken'],
'messages' => array(
array(
'type' => 'text',
'text' => '大家好,這是一個範例 Bot OuO
範例程式開源至 GitHub (包含教學):
https://github.com/GoneTone/line-example-bot-php'
)
)
));
break;
}
};
?>