-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.gs
90 lines (75 loc) · 2.41 KB
/
common.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
function sendLineMessageFromReplyToken(token, replyText) {
var url = "https://api.line.me/v2/bot/message/reply";
var headers = {
"Content-Type" : "application/json; charset=UTF-8",
"Authorization" : "Bearer " + channel_access_token
};
var postData = {
"replyToken" : token,
"messages" : [{
"type" : "text",
"text" : replyText
}]
};
var options = {
"method" : "POST",
"headers" : headers,
"payload" : JSON.stringify(postData)
}
return UrlFetchApp.fetch(url, options);
}
function setPosition(form){
SpreadSheet.setToSheet(form.latitude, 'B1', '位置情報');
SpreadSheet.setToSheet(form.longitude, 'B2', '位置情報');
return;
}
/**
* @param {string} keyword - 検索キーワード(現時点では固定)
* @return {string} 画像のURLを返却する
* 備考 : googleのCSEを使用しているので、一日に100件以上のリクエストを飛ばすと有料になる。
* cronで使用する予定なので、必要以上に使わないように注意すること。
*/
function getGoogleCustomSearchImage(keyword){
keyword = encodeURIComponent(keyword);
var uri = "https://www.googleapis.com/customsearch/v1?key=" + API_KEY + "&cx=" + CSE_ID + "&q=" + keyword + "&searchType=image"
var response = UrlFetchApp.fetch(uri);
var json = JSON.parse(response);
var random_params = Math.floor(Math.random() * json["items"].length);
var result = json["items"][random_params]["link"];
return result;
}
function sendLineImage(imageUrl) {
var url = "https://api.line.me/v2/bot/message/push";
var headers = {
"Content-Type" : "application/json; charset=UTF-8",
"Authorization" : "Bearer " + channel_access_token
};
var postData = {
"to" : user_id,
"messages" : [{
"type" : "image",
"originalContentUrl" : imageUrl,
"previewImageUrl" : imageUrl
}]
};
var options = {
"method" : "POST",
"headers" : headers,
"payload" : JSON.stringify(postData)
};
return UrlFetchApp.fetch(url, options);
}
/**
* @param {string} url - URL
* @return {string} 短縮URL
* 備考 : UrlShortenerが2019/03に終了となる。
*/
function urlShortener(url) {
var shortUrl = UrlShortener.Url.insert({longUrl:url}).id
return shortUrl;
}
// asyncとawaitで紐づかせる
// await sleep(1000);
// function sleep(milliseconds) {
// return new Promise(resolve => setTimeout(resolve, milliseconds));
// }