Last active
August 21, 2019 01:35
-
-
Save Chi-Hsin/80d7bdacade1c8878476b52923d7eebe to your computer and use it in GitHub Desktop.
BOT機器人教學 說明檔案
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
function doPost(e) { | |
var msg= JSON.parse(e.postData.contents); | |
var shitText = msg.messaging[0].message.text; | |
var senderId = msg.messaging[0].sender_id; | |
if(shitText == "hello"){ | |
botReply("安安呀",senderId); | |
} | |
} | |
function botReply(replyMsg,senderId){ | |
sendObj = { | |
"recipient":{ | |
"id":senderId | |
}, | |
"message":{ | |
"type":"text", | |
"text":replyMsg | |
} | |
} | |
UrlFetchApp.fetch(shitUrl, { | |
'headers': { | |
'Content-Type': 'application/json; charset=UTF-8' | |
}, | |
'method': 'post', | |
'payload': JSON.stringify(sendObj), | |
}); | |
} |
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
ACCESS_TOKEN = 你後臺裡的Access Token; //改成你後臺裡的Access Token | |
shitUrl = "https://us-central1-hahamut-8888.cloudfunctions.net/messagePush?access_token=" + ACCESS_TOKEN; |
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
function xml_parsing(senderId) { | |
var fetch = UrlFetchApp.fetch("https://home.gamer.com.tw/homeindex.php?owner=" + senderId); | |
var doc = Xml.parse(fetch, true); | |
var bodyHtml = doc.html.body.toXmlString(); | |
var doc_parse = XmlService.parse(bodyHtml); | |
var root = doc_parse.getRootElement(); | |
return root; | |
} | |
function get_info(xml) | |
{ | |
var root = xml; | |
var arr = ["帳號","暱稱","稱號"]; | |
var obj = {}; | |
var MSG = getElementsByClassName(root,"MSG-mydata1")[0]; | |
var MSG_li = getElementsByTagName(MSG,"li"); | |
for(i=0;i<3;i++){ | |
if(MSG_li[i].getChild("span") != null){ | |
obj[arr[i]] = MSG_li[i].getChildText("span"); | |
continue; | |
} | |
obj[arr[i]] = MSG_li[i].getText(); | |
} | |
obj["稱號"] = obj["稱號"].slice(3); | |
return obj; | |
} |
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
function getElementsByClassName(element, classToFind) { | |
var data = []; | |
var descendants = element.getDescendants(); | |
descendants.push(element); | |
for(i in descendants) { | |
var elt = descendants[i].asElement(); | |
if(elt != null) { | |
var classes = elt.getAttribute('class'); | |
if(classes != null) { | |
classes = classes.getValue(); | |
if(classes == classToFind) data.push(elt); | |
else { | |
classes = classes.split(' '); | |
for(j in classes) { | |
if(classes[j] == classToFind) { | |
data.push(elt); | |
break; | |
} | |
} | |
} | |
} | |
} | |
} | |
return data; | |
} | |
function getElementsByTagName(element, tagName) { | |
var data = []; | |
var descendants = element.getDescendants(); | |
for(i in descendants) { | |
var elt = descendants[i].asElement(); | |
if( elt !=null && elt.getName()== tagName) data.push(elt); | |
} | |
return data; | |
} |
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
function doPost(e) { | |
var msg= JSON.parse(e.postData.contents); | |
var shitText = msg.messaging[0].message.text; | |
var senderId = msg.messaging[0].sender_id; | |
if(shitText == "hello"){ | |
var xml = xml_parsing(senderId); | |
var myinfo = get_info(xml); | |
botReply("阿!\n是" + myinfo["稱號"] + " " + myinfo["暱稱"] + ",你好呀!",senderId); | |
} | |
} |
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
{ | |
"oauthScopes": [ | |
"https://www.googleapis.com/auth/userinfo.email", | |
"https://www.googleapis.com/auth/firebase.database", | |
"https://www.googleapis.com/auth/script.external_request" | |
] | |
} |
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 RTDB_URL = 你的Firebase資料庫的網址; | |
var firebase_token = ScriptApp.getOAuthToken(); | |
function db_getData(url){ //取指定位置的資料 | |
var data = UrlFetchApp.fetch(url); | |
return data.getContentText(); | |
} | |
function db_putData(url,obj){ //寫入資料(更新覆蓋) | |
var options = { | |
'method': "put", | |
'payload': JSON.stringify(obj), | |
'muteHttpExceptions':true | |
}; | |
var response = UrlFetchApp.fetch(url,options); | |
return response; | |
} | |
function db_postData(url,obj){ //寫入資料(新增) | |
var options = { | |
'method': "post", | |
'payload': JSON.stringify(obj), | |
'muteHttpExceptions':true | |
}; | |
var response = UrlFetchApp.fetch(url,options); | |
return response; | |
} | |
function db_url(){ // 取得資料庫指定位置 | |
var args = Array.prototype.slice.call(arguments); | |
var path = args.join("/"); | |
var url = RTDB_URL + path + ".json?access_token=" + encodeURIComponent(firebase_token); | |
return url; | |
} |
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
if(shitText == "寫入測試"){ | |
var obj = {item:'apple'}; | |
var url = db_url(senderId,"info"); | |
var r = db_postData(url,obj); | |
if(r.getResponseCode() == 200){ | |
botReply("得到一個蘋果!",senderId); | |
} | |
else{ | |
botReply("測試發生錯誤",senderId); | |
} | |
} |
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
function doPost(e) { | |
var msg= JSON.parse(e.postData.contents); | |
var shitText = msg.messaging[0].message.text; | |
var senderId = msg.messaging[0].sender_id; | |
var url = db_url(senderId); //讀取的位置 | |
if(db_getData(url) == "null"){ //如果資料庫沒有就寫入 | |
var obj = { | |
'info':null | |
} | |
var info_data = get_info(xml_parsing(senderId)); | |
obj.info = info_data; | |
db_putData(url,obj); | |
} | |
var myData = JSON.parse(db_getData(url)); | |
if(shitText == "hello"){ | |
botReply("阿!\n是" + myData.info["稱號"] + myData.info["暱稱"] + ",你好呀!",senderId); | |
} | |
} |
謝謝你 :D
這樣看起來舒服多了
謝謝您的教學,小弟受益良多~
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
檔案名稱加上.js會有顏色喔