-
Notifications
You must be signed in to change notification settings - Fork 0
/
linebot.gs
75 lines (65 loc) · 2.21 KB
/
linebot.gs
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
function doPost(e)
{
var funArr = ['daily', 'weekly', 'monthly']; // 功能字串
var channelToken = 'Your Channel access token';
var msg = JSON.parse(e.postData.contents);
// 取出 replayToken 和發送的訊息文字
var replyToken = msg.events[0].replyToken;
var userMessage = msg.events[0].message.text;
// 若沒符合則結束
if (typeof replyToken === 'undefined') { return; }
var searchResult = linearSearch(funArr, userMessage); // 搜尋結果
var replyMessage = getReplyMessage(funArr, searchResult); // 取得回覆訊息
replyMsg(replyToken, replyMessage, channelToken); // 回復訊息
}
// 回覆訊息
function replyMsg(replyToken, replyMessage, channelToken)
{
var url = 'https://api.line.me/v2/bot/message/reply';
var opt = {
'headers': {
'Content-Type': 'application/json; charset=UTF-8',
'Authorization': 'Bearer ' + channelToken,
},
'method': 'post',
'payload': JSON.stringify({
'replyToken': replyToken,
'messages': [{
'type': 'text',
'text': replyMessage,
}],
}),
};
UrlFetchApp.fetch(url, opt);
}
// 取得回覆訊息
function getReplyMessage(funArr, searchResult)
{
var replyMessage = '';
if(searchResult.length == 0){
replyMessage = "請輸入: daily or weekly or monthly";
}else{
// 取得 trending 資料
var response = UrlFetchApp.fetch(Utilities.formatString('https://trendings.herokuapp.com/repo?since=%s', funArr[searchResult[0]]), {
'method': 'get'
});
var trending = JSON.parse(response.getContentText());
var replyMessage = "";
// 組合訊息
for(var idx=0,tlength=trending.items.length ; idx<tlength ; idx++){
replyMessage += trending.items[idx].repo + "(" + trending.items[idx].lang + ")" + "\n" + trending.items[idx].repo_link + "\n\n";
}
}
return replyMessage;
}
// Search array value using linearSearch
function linearSearch(array, searchText)
{
var result = [];
for (var i = 0; i < array.length; i++) {
if (array[i] == searchText) {
result.push(i);
}
}
return result;
}