Skip to content

Instantly share code, notes, and snippets.

@redgosho
Last active August 2, 2018 14:06
Show Gist options
  • Select an option

  • Save redgosho/5d5d11c6c4c0d5e826e4c10d113f12e2 to your computer and use it in GitHub Desktop.

Select an option

Save redgosho/5d5d11c6c4c0d5e826e4c10d113f12e2 to your computer and use it in GitHub Desktop.
LINEbot createHTML
var PROPERTIES = PropertiesService.getScriptProperties();//ファイル > プロジェクトのプロパティから設定した環境変数的なもの
var Line_access_token = PROPERTIES.getProperty('Line_access_token');
var Amazon_accessKey = PROPERTIES.getProperty('Amazon_accessKey'); //Your Access Key
var Amazon_secretKey = PROPERTIES.getProperty('Amazon_secretKey'); //Your Secret Key
var Amazon_associateID = PROPERTIES.getProperty('Amazon_associateID'); //Your Associate ID
// ボットにメッセージ送信/フォロー/アンフォローした時の処理
function doPost(e) {
var events = JSON.parse(e.postData.contents).events;
events.forEach(function(event) {
if(event.type == "message") {
reply(event);
} else if(event.type == "follow") {
follow(event);
} else if(event.type == "unfollow") {
unFollow(event);
}
});
}
// 入力されたメッセージをおうむ返し
function reply(e) {
var date = createDate();
var input_message = e.message.text;
//var input_message = "https://www.amazon.co.jp/BOOX-Note-%E9%9B%BB%E5%AD%90%E6%9B%B8%E7%B1%8D%E3%83%AA%E3%83%BC%E3%83%80%E3%83%BC-10-3%E3%82%A4%E3%83%B3%E3%83%81%E5%A4%A7%E7%94%BB%E9%9D%A2-Android6-0-%E3%83%87%E3%83%A5%E3%82%A2%E3%83%AB%E3%83%BB%E3%82%BF%E3%83%83%E3%83%81/dp/B07BNGJXGS/ref=lp_5906856051_1_1?srs=5906856051&ie=UTF8&qid=1533205572&sr=8-1&smid=AWV3CL9HMEXQA";
//var input_message = "B00UJIWZ88";
var URL_check = "https://www.amazon.co.jp/";
if (input_message.match(/https:\/\/www.amazon.co.jp\//) && input_message.match(/dp/)){
var check_word = "/dp/";
var index = input_message.indexOf(check_word);
Logger.log(check_word);
var ASIN = input_message.slice(index + 4,index + 14);
Logger.log(ASIN);
var xml = getXML_(ASIN);
var html = getData_(xml);
Logger.log(html);
var message = {
"replyToken" : e.replyToken,
"messages" : [
{
"type" : "text",
"text" : html,
}
]
};
}else if(input_message.match(/B........./)){
var ASIN = input_message;
var xml = getXML_(ASIN);
var html = getData_(xml);
Logger.log(html);
var message = {
"replyToken" : e.replyToken,
"messages" : [
{
"type" : "text",
"text" : html,
}
]
};
}else{
var message = {
"replyToken" : e.replyToken,
"messages" : [
{
"type" : "text",
"text" : "Amazonの製品ページのURLを入力してください。",
}
]
};
}
var replyData = {
"method" : "post",
"headers" : {
"Content-Type" : "application/json",
"Authorization" : "Bearer " + Line_access_token
},
"payload" : JSON.stringify(message)
};
UrlFetchApp.fetch("https://api.line.me/v2/bot/message/reply", replyData);
}
/* フォローされた時の処理 */
function follow(e) {
}
/* アンフォローされた時の処理 */
function unFollow(e){
}
//// amazon area
// Private getXML_
function getXML_(asin)
{
var timestamp = getTimeStamp_();
// プロパティ取得
var url = "webservices.amazon.co.jp"; // Search In Japan
//var url = "webservices.amazon.com"; // Search In USA
var parameters = {
"Service":"AWSECommerceService",
"AWSAccessKeyId":Amazon_accessKey,
"Operation":"ItemLookup",
"ItemId":asin,
"AssociateTag":Amazon_associateID, // 2012/03/12Add
"ResponseGroup":"ItemAttributes,OfferSummary,Images",
"Timestamp":timestamp
};
var array = [];
for(var parameter in parameters){
array.push(parameter + "=" + encodeURIComponent(parameters[parameter])); //Encode
}
array.sort();
var urlParameter = array.join('&');
var request = "GET" + "\n" + url + "\n" + "/onca/xml" + "\n" + urlParameter;
var signature = Utilities.base64Encode(Utilities.computeHmacSha256Signature(request, Amazon_secretKey)); //HMAC-SHA256 -> base64
var urlSignature = "&Signature=" + encodeURIComponent(signature);
var response = UrlFetchApp.fetch("http://" + url + "/onca/xml?" + urlParameter + urlSignature);
return XmlService.parse(response.getContentText()); // Get XMLData
}
// Private getTimeStamp
function getTimeStamp_(){
var dates = new Date();
var year = dates.getUTCFullYear();
var month = paddingZero_(dates.getUTCMonth() + 1); //Month:0to11
var day = paddingZero_(dates.getUTCDate());
var hour = paddingZero_(dates.getUTCHours());
var minute = paddingZero_(dates.getUTCMinutes());
var second = paddingZero_(dates.getUTCSeconds());
return year + "-" + month + "-" + day + "T" + hour + ":" + minute + ":" + second + "Z";
}
// Private paddingZero_
function paddingZero_(value){
//Get Last 2 Characters
return ("0" + value).slice(-2);
}
function getData_(xmlData){
var xmlns = 'http://webservices.amazon.com/AWSECommerceService/2011-08-01';
var ecs = XmlService.getNamespace(xmlns);
var item = xmlData.getRootElement()
.getChild('Items', ecs)
.getChild('Item', ecs);
// price
var price = item
.getChild('OfferSummary', ecs)
.getChild('LowestNewPrice', ecs)
.getChild('FormattedPrice', ecs)
.getText();
// image
var image160 = item
.getChild('MediumImage', ecs)
.getChild('URL', ecs)
.getText();
// url
var URL = item
.getChild('DetailPageURL', ecs)
.getText();
// title
var title = item
.getChild('ItemAttributes', ecs)
.getChild('Title', ecs)
.getText();
var tax = "税抜";
var shop = "Amazon";
var date = createDate();
const html ="<div class=\"mv-amazon_stage\"><div class=\"mv-amazon_image\"><a href=\""+ URL +"\"target=\"blank\"><img src=\""+ image160 +"\"></a></div><div class=\"mv-amazon_info\"><ul><li><div class=\"mv-amazon_shop_name\"><a href=\""+ URL +"\"target=\"blank\">"+ shop +"</a></div></li><li><div class=\"mv-amazon_item_name\"><a href=\""+ URL +"\"target=\"blank\">"+ title +"</a></div></li><li><div class=\"mv-amazon_price\">"+ price +" ("+ tax +")</div></li><li><div class=\"mv-amazon_price_date\">※"+ date +"時点</div></li><li><a href=\""+ URL +"\"target=\"blank\"class=\"mv-amazon_btn\">購入はこちら<i class=\"fa fa-external-link\"aria-hidden=\"true\"></i></a></li></ul></div></div>";
return html;
}
function createDate () {
const now = new Date();
const year = now.getFullYear();
const month = now.getMonth() + 1;
const day = now.getDate();
return year + '/' + month + '/' + day;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment