Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
var https = require("https");
exports.handler = function(event, context) {
console.log('Received event:', JSON.stringify(event, null, 2));
// テキストが送られた時
var msg = event.entry[0].messaging[0].message.text;
console.log(msg);
// 画像が送られた時
// console.log(event.entry[0].messaging[0].message.attachments[0].payload.url);
var token = "FaceBookページのACCESS TOKEN";
var options = {
hostname: "graph.facebook.com",
path: "/v2.6/me/messages?access_token=" + token,
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
}
var datas = {};
datas["徒然なるままに"] = function(){
return {
"recipient": {
"id": "送り先のUserID(Messengerからのリクエストのevent.entry[0].messaging[0].sender.idに送ってきたユーザーのIDが入ってます)"
},
"message": {
"text": "つれづれなるままに、日くらし硯にむかひて、心にうつりゆくよしなし事を、そこはかとなく書きつくれば、あやしうこそものぐるほしけれ。"
}
}
}
datas["画像"] = function(){
return {
"recipient": {
"id": "送り先のUserID"
},
"message": {
"attachment": {
"type": "image",
"payload": {
"url": "https://s3-us-west-1.amazonaws.com/blog-littlekbt/kenko.jpg"
}
}
}
}
}
datas["ストラクチャー"] = function(){
return {
"recipient": {
"id": "送り先のUserID"
},
"message": {
"attachment": {
"type": "template",
"payload": {
"template_type": "button",
"text": "何が知りたい?",
"buttons": [
{
"type": "web_url",
"url": "https://ja.wikipedia.org/wiki/%E5%90%89%E7%94%B0%E5%85%BC%E5%A5%BD",
"title": "兼好法師について"
},
{
"type": "web_url",
"url": "https://ja.wikipedia.org/wiki/%E5%BE%92%E7%84%B6%E8%8D%89",
"title": "徒然草について"
},
]
}
}
}
}
}
datas["カルーセル"] = function() {
return {
"recipient": {
"id": "送り先のUserID"
},
"message": {
"attachment": {
"type": "template",
"payload": {
"template_type": "generic",
"elements": [
{
"title": "新版 徒然草 現代語訳付き (角川ソフィア文庫) ",
"image_url": "http://ecx.images-amazon.com/images/I/51Q61wMUkoL._SX351_BO1,204,203,200_.jpg",
"subtitle": "無常観のなかに中世の現実を見据えた視点をもつ兼好の名随筆集。",
"buttons": [{
"type": "web_url",
"url": "http://www.amazon.co.jp/dp/4044001189/ref=pd_lpo_sbs_dp_ss_2?pf_rd_p=187205609&pf_rd_s=lpo-top-stripe&pf_rd_t=201&pf_rd_i=400301121X&pf_rd_m=AN1VRQENFRJN5&pf_rd_r=0RJ6WD4J0Z12BWZ2C8V5",
"title": "新版 徒然草 現代語訳付き (角川ソフィア文庫) "
}]
},
{
"title": "すらすら読める徒然草 (講談社文庫) ",
"image_url": "http://ecx.images-amazon.com/images/I/51NWWPwa7QL._SX350_BO1,204,203,200_.jpg",
"subtitle": "存命の喜び、日々に楽しまざらんや著者の「心の中で鳴り続け」生き方を定めた「今を生きる言葉」",
"buttons": [{
"type": "web_url",
"url": "http://www.amazon.co.jp/%E3%81%99%E3%82%89%E3%81%99%E3%82%89%E8%AA%AD%E3%82%81%E3%82%8B%E5%BE%92%E7%84%B6%E8%8D%89-%E8%AC%9B%E8%AB%87%E7%A4%BE%E6%96%87%E5%BA%AB-%E4%B8%AD%E9%87%8E-%E5%AD%9D%E6%AC%A1/dp/4062777053",
"title": "すらすら読める徒然草 (講談社文庫) "
}]
},
{
"title": "兼好法師家集 (岩波文庫)",
"image_url": "http://ecx.images-amazon.com/images/I/51a8Nmv1aYL._SX351_BO1,204,203,200_.jpg",
"subtitle": "歌人として頓阿,浄弁,慶雲とともに和歌四天王の一人といわれていた兼好の生涯にわたる作歌を集める.",
"buttons": [{
"type": "web_url",
"url": "http://www.amazon.co.jp/%E5%85%BC%E5%A5%BD%E6%B3%95%E5%B8%AB%E5%AE%B6%E9%9B%86-%E5%B2%A9%E6%B3%A2%E6%96%87%E5%BA%AB-%E5%90%89%E7%94%B0-%E5%85%BC%E5%A5%BD/dp/4003011228",
"title": "兼好法師家集 (岩波文庫)"
}]
},
]
}
}
}
}
}
var data = JSON.stringify(datas[msg]());
console.log(data);
var req = https.request(options, function(res) {
console.log(res.header);
console.log("finish");
}).on("error", function(e) {
console.log(e.message);
});
req.write(data);
req.end();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment