LINEBOT-GoogleAppScript กำหนดสิทธิ์ follower ในการใช้งาน
QR bot ตัวอย่าง
Repository : https://github.com/itchampclub/linebot-GAS-con
การทำ Linebot โดยใช้ google app script เป็น webhook และใช้ google sheet บันทึกข้อมูล
- สร้าง sheet
2.webhook โดยใช้ code จากไฟล์ webhook.gs ใน repository
config
- var channelToken = “XXX -Token— XXX”;
- var ss = SpreadsheetApp.openByUrl(“https://docs.google.com/spreadsheets/d/1XpXXXXXXXaM/edit#gid=0");
- var sheet = ss.getSheetByName(“ชื่อ sheet”);
- var admId = “Uf879 — userID ผู้ดูแล — -64b8376”;
อธิบายการทำงานคร่าวๆ
เมื่อ user follow bot >> case ‘follow’:
เมื่อ user ส่งข้อความหรือ event ใดๆ ขณะที่ยังไม่ได้รับการยืนยันจากผู้ดูแล
เมื่อผู้ดูแล confirm ให้ user สามารถใช้งานฟังก์ชั่นต่างๆได้
if(messageText.indexOf(“conf “)>-1){ << ตรวจสอบ text ที่มี conf เพื่อเข้าฟังก์ชั่นยืนยันให้ user ใช้งาน
var confId = messageText.split(‘ ‘,2)[1]; << เลือกตัดเฉพาะ userid ด้วยการ split จาก วรรค conf userid [จะได้ userid เพื่อนำไปค้นหาและยืนยันให้ใช้งาน]
if(confstatus == true){
//ส่วนที่ทำงานเฉพาะ user ที่ได้รับการยืนยันแล้ว สามารถปรับแก้ไขเพิ่มเติมได้ในส่วนนี้
if(messageText == “ทดสอบ”){
var mess = [{‘type’: ‘text’, ‘text’: ‘ข้อความทดสอบ ยืนยันแล้ว’}];
replyMsg(replyToken, mess, channelToken);
}
else{
var mess = [{‘type’: ‘text’, ‘text’: messageText}];
replyMsg(replyToken, mess, channelToken);
}
}
3.เมื่อแก้ไขโค๊ดเรียบร้อยแล้ว
Deploying a script as a web app
url ที่ได้คือ webhook
https://developers.line.biz/console/ << ตั้งค่า
หรือเข้าไปตั้งค่าที่ https://manager.line.biz/