Last active
April 30, 2017 10:59
-
-
Save isnullnull/ff1e98e3ce1eac4cad130bc44520c822 to your computer and use it in GitHub Desktop.
夢野久作の ”瓶詰地獄” がサーバレスアーキテクチャの Azure Functions で送出されて3つの瓶が流れ着く LINEBOT ref: http://qiita.com/n_ueh/items/3ddccd0a08b3d7fec430
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function (context, req) { | |
context.log('JavaScript HTTP trigger function processed a request. RequestUri=%s', req.originalUrl); | |
context.log(req); | |
context.bindings.outputQueueItem = req.body; | |
res = { body : "" }; | |
context.done(); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var https = require("https"); | |
var url = require("url"); | |
// 送出 | |
function send_line(postData, context, mtd){ | |
var parse_url = url.parse('https://api.line.me/v2/bot/message/'+ mtd); | |
var options = { | |
host: parse_url.host, | |
path: parse_url.path, | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json', | |
'Authorization': 'Bearer {(キー)}' | |
} | |
}; | |
var req = https.request(options, function(res) { | |
context.log('-- send_line : STATUS: ' + res.statusCode); | |
context.log('-- send_line : HEADERS: ' + JSON.stringify(res.headers)); | |
res.setEncoding('utf8'); | |
res.on('data', function (chunk) { | |
context.log('-- send_line : BODY: ' + chunk); | |
}); | |
}); | |
req.on('error', function(e) { | |
context.log('-- send_line : problem with request: ' + e.message); | |
}); | |
req.write(postData); | |
req.end(); | |
} | |
// 手紙シャッフル | |
function shuffle(array) { | |
var n = array.length, t, i; | |
while (n) { | |
i = Math.floor(Math.random() * n--); | |
t = array[n]; | |
array[n] = array[i]; | |
array[i] = t; | |
} | |
return array; | |
} | |
// 瓶詰 | |
function push_bottles(context, event) { | |
shuffled_lettersNum = shuffle(["1", "2", "3"]); | |
var postData = JSON.stringify( | |
{'to' : event.source.userId, | |
'messages' : [ | |
{ | |
"type": "template", | |
"altText": "『瓶画像と本文へのリンク』です。", | |
"template": { | |
"type": "carousel", | |
"columns": [ | |
{ | |
"thumbnailImageUrl": "https://(アカウント).blob.core.windows.net/bins/btl_typeB.png", | |
"text": "よほど以前に漂着致したる封瓶", | |
"actions": [ | |
{ | |
"type": "uri", | |
"label": "瓶を開ける", | |
"uri": "https://(アカウント).blob.core.windows.net/bins/" + shuffled_lettersNum[0] + ".jpg" | |
} | |
] | |
}, | |
{ | |
"thumbnailImageUrl": "https://(アカウント).blob.core.windows.net/bins/btl_typeA.png", | |
"text": "よほど以前に漂着致したる封瓶", | |
"actions": [ | |
{ | |
"type": "uri", | |
"label": "瓶を開ける", | |
"uri": "https://(アカウント).blob.core.windows.net/bins/" + shuffled_lettersNum[1] + ".jpg" | |
} | |
] | |
}, | |
{ | |
"thumbnailImageUrl": "https://(アカウント).blob.core.windows.net/bins/btl_typeB.png", | |
"text": "よほど以前に漂着致したる封瓶", | |
"actions": [ | |
{ | |
"type": "uri", | |
"label": "瓶を開ける", | |
"uri": "https://(アカウント).blob.core.windows.net/bins/" + shuffled_lettersNum[2] + ".jpg" | |
} | |
] | |
} | |
] | |
} | |
} | |
]} | |
); | |
send_line(postData, context, "push"); | |
}; | |
// 浜辺 | |
function reply_beach(context, event) { | |
var postData = JSON.stringify({ | |
'replyToken' : event.replyToken, | |
'messages' : [ | |
{ | |
"type": "image", | |
"originalContentUrl": "https://(アカウント).blob.core.windows.net/bins/beach.jpg", | |
"previewImageUrl": "https://(アカウント).blob.core.windows.net/bins/beach.jpg" | |
} | |
] | |
}); | |
send_line(postData, context, "reply"); | |
}; | |
function post_message(context, event) { | |
if (event.type == 'message'){ | |
var messageType = event.message.type; | |
if (messageType == 'text') { | |
reply_beach(context, event); | |
push_bottles(context, event); | |
} else | |
reply_beach(context, event); | |
}; | |
} | |
module.exports = function (context, myQueueItem) { | |
context.log('------------ JavaScript queue trigger function', myQueueItem); | |
myQueueItem.events.forEach(event => post_message(context, event)); | |
context.done(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment