ð°(æ°IDE察å¿ç)ãªãŠã è¿ããããLINEBotãGASã®Webãšãã£ã¿ã§æå°éã«äœã
æ³å®èªè
- åè¬çã®æ¹
- ãã€ããLINEBot ãªãŠã è¿ããã§æ€çŽ¢ããç§
- Getãããã¯PostããããŒã¿ããããã°ããæ¹æ³ãæ¢ããŠããæ¹
ããããš
ãããªãéçšã¬ãã«ã®è©±ããããšããããããã®ã§ã
- LINEããã¡ãã»ãŒãžãéãããã«MessagingAPIã䜿ããBOTã¢ã«ãŠã³ããäœã
- MessagingAPIã䜿ãèšå®ã«ããŠããã£ãã«ã¢ã¯ã»ã¹ããŒã¯ã³ããäœæãã
- GASã®ãããžã§ã¯ããäœã
- GASã®ãœãŒã¹ã³ãŒãã«äžèšMessagingAPIã§äœæãããã£ãã«ã¢ã¯ã»ã¹ããŒã¯ã³ãæå®ãã
- GASã®ãããžã§ã¯ãããããã€ãã
- GASã®Webã¢ããªURL(https://script.google.com/macros/s/ããã«é©åœãªID/exec)ãLINEBotã®WebhookURLã«èšå®ãã
ã«çµã£ãŠãããŸãã
åŸã§æ¢ãçŽããã®ãŸãšã
LINE Developerã®çšèª
çšèª | æèš³ããŸãã¯ã€ã¡ãŒãž |
---|---|
ãããã€ã㌠| ãã£ã¬ã¯ããªã®ãããªãã®ãBotãããŸãäœããªããªããªãã¹ããŸãšããŠããŸã£ãæ¹ãèŠéããè¯ãïŒå人差ããïŒ |
ãã£ãã« | ãããã€ããŒããã£ã¬ã¯ããªãªããã¡ã€ã«ã®ãããªãã®ãå®æ ã¯LINEã¢ã«ãŠã³ãã®ãããªãã®ã |
URL
æŠèŠ | ã¢ãã¬ã¹ | äœãæ¹ |
---|---|---|
LINEBotã¢ã«ãŠã³ããäœã | https://developers.line.biz/console | LINE Developersãæ°ãããã£ãã«ã¯ããã§äœãããŸãããã°ã€ã³ã¯LINEã¢ã«ãŠã³ãã䜿ã |
LINEã®MessagingAPIèšå® | https://developers.line.biz/console/channel/ãã£ãã«ID/messaging-api | ãã£ãã«ã¢ã¯ã»ã¹ããŒã¯ã³ãçºè¡ããããWebhookURLãèšå®ãã |
LINEBotã®ã°ã«ãŒãèšå® | https://manager.line.biz/account/(BOTã®ã¢ã«ãŠã³ãID)/setting | ã°ã«ãŒãã«äœ¿ããããã«ããèšå®ãããå Žæ |
GASãããžã§ã¯ãã®æ°èŠäœæ | https://script.google.com/home | Googleã¢ã«ãŠã³ãã¯äœã£ãŠããæ³å®ããã°ã€ã³ã§ããªãå Žåã¯ãã¹ã¯ãŒããåçºè¡ããããã¢ã«ãŠã³ããäœãçŽããããã |
(GAS)LINEBot WebhookURL | https://script.google.com/macros/s/ããã«é©åœãªID/exec | GASãããžã§ã¯ãããããã€ãã |
Githubãªããžã㪠| https://github.com/shimajima-eiji/--GAS_v5_LINEdebug | ä»å䜿ããªããžããªã®ã«ãŒã |
泚æããã¹ãã¯äžèšã®éãã§ãã
现ããã®ã¯ããããã®ç« ã§æžããŠããŸãã
LINEåŽã®èšå®
- ãŸãã¯LINE Developersã«ãã°ã€ã³ããŸãããã
- åããŠã®æ¹ã¯ãããã€ããŒãèšå®ããŸãããã
- https://developers.line.biz/console/ ã®ãããã€ããŒã®è¿ãã«äœæãã¿ã³ããããŸããè²ã èšããããŸãŸã«èšå®ããŸãããã
- ãããã€ããŒã«æ°ãããã£ãã«ãäœæããŸããæ¢åã®ãã®ã«åœ±é¿ãããã®ã§å šå¡ãäœã£ãæ¹ãããã§ãã
- äœã£ããã£ãã«ã®MessagingAPIèšå® (https://developers.line.biz/console/channel/ãã£ãã«ID/messaging-api) ã§ãã£ãã«ã¢ã¯ã»ã¹ããŒã¯ã³ãçºè¡ãã
- WebhookURLã¯åŸã§èšå®ããã®ã§ãããŒãžã¯éããªãããš
åºæ¬çã«ããã§ãããŸããããããã€ããŒãšãã£ãã«ã®äœææã«è²ã
èãããã®ã§ãããªãã®ç¶æ³ã«äžçªè¿ããã®ãéžæããŠãããŸãããã
å·çæç¹ã§ã¯ããã®äœæ¥ã§ãµãã¹ã¯ãªãã·ã§ã³ãçºçããããšã¯ãããŸããã
GASã§ããããš
ãŸãã¯ãããžã§ã¯ããäœæãã
ã§ãæ°ãããããžã§ã¯ããäœæããŸãããã
ååã¯ãªãã§ãããã®ã§ãåããããã管çã§ããããã«ããŠããã®ãããããã§ãã
Google Driveã§èŠãæã«ã«ãŒããã£ã¬ã¯ããªä»¥äžã«äœæãããŠããã®ã§ããã¡ããã¡ãããŠèŠã¥ãããšæããŸãã
åŸã§æåã§é
眮æããããŠãããŸãããã
ãœãŒã¹ã³ãŒããäœæ
ã³ããã§OKã§ãã
äžéšç·šéããå¿
èŠãããã®ã§ãåŸè¿°ããŠãããŸãã
ãã¡ã€ã«åã¯ã³ãŒã.gs
ã§ãããã§ãããé¢æ°åã¯myfunction()
â doPost(e)
ãšããŠãã ããã
ãã®ååãããªããšPOSTãããããŒã¿ãåãåããŸããã[1]
const TOKEN = "(ããã¯èªåã§å
¥ãã)"
const ENDPOINT_TARGET = "push" // ããŒã¯ã¯reply, ã°ã«ãŒãã¯push
// GASã§ãããã°ããå Žåã¯ã°ã«ãŒãã®ã¿ïŒããŒã¯ã®ãªãã©ã€ããŒã¯ã³ã䜿ããªãããïŒ
ãã®TOKENãšã¯ãLINEBOTã®ãã£ãã«ã¢ã¯ã»ã¹ããŒã¯ã³ã®ããšã§ãã
ENDPOINT_TARGETã«ã¯ãããŒã¯ãšã°ã«ãŒãã§éä¿¡å
ãç°ãªããããæå察å¿ãå¿
èŠã§ãã
GASã§ãããã°ããããã®ã¯ã°ã«ãŒããªã®ã§åæå€ã¯push
ã«ããŠãŸãããããŒã¯ã䜿ãããå Žåã¯reply
ã«ããŠãããã€ããŠãã ããã
GASã®ãããžã§ã¯ãããããã€
ãããã€ã«çœ ããããŸãã
æ°ããWebIDEã®è©±ãããŸãããæ°ãããããã€ãäœããã³ã«æ°ããWebhookURLãçºè¡ãããŸãã
ãªã®ã§ãããæ¹ãééãããšãããã€ããããã³ã«åŸè¿°ã®LINEBotã®WebhookURLãæžãæããå¿
èŠãåºãŠããŸãã
泚æããç¹ã ãããã¯ã¢ããããŸãã
- æ°ãããããã€ãäœæ
- 次ã®ãŠãŒã¶ãŒãšããŠå®è¡ïŒãèªåã
- ã¢ã¯ã»ã¹ã§ãããŠãŒã¶ãŒïŒãå šå¡ã
- ãããã€ãäœãçŽãå Žå
- ãããã€ã®ç®¡çãéã
- ç·šéïŒãã³ã®ã¢ã€ã³ã³ïŒãæŒäž
- ããŒãžã§ã³ããæ°ããŒãžã§ã³ãã«å€æŽããããããå¿ãããšäœãæŽæ°ãããŠããªãäºã«ãªãã
ããããããªãå Žåã¯ããããã€ãããã³ã«åŸè¿°ã®LINEBotã®WebhookURLãæžãæãããšããã«ãŒã«ã培åºããã°OKã§ãã
LINEBotãšGASãé£æºãã
LINE Developers(https://developers.line.biz/console/channel/ãã£ãã«ID/messaging-api)ã«æ»ã£ãŠãGASã®Webã¢ããªURL(https://script.google.com/macros/s/ããã«é©åœãªID/exec)ãLINEBotã®WebhookURLã«èšå®ããŸãã
ãæŽæ°ããã¿ã³ãæŒããåŸã«ãæ€èšŒããã¿ã³ãæŒããšãæ¥ç¶å
ãæ£ããã確èªã§ããŸãã
ãããŸã§ã§ããããåéè¿œå ã®QRã³ãŒããLINEã§èªã¿åã£ãŠã¡ãã»ãŒãžãæããããã°ã«ãŒããäœã£ãããããšè²ã
ã§ããŸãã
GASã§èšå®ããENDPOINTãreplyãpushãã¯ç¢ºèªããŠãããŸããããããå¿ããã®ã§ãå¿é
ãªãäž¡æ¹ã«ã¡ãã»ãŒãžãæãããšè¯ãã§ãã
ãŒãã
ãã®å
容ããããªãNode-RED(enebler)ã䜿ãã°ããŒã³ãŒãã§åãããšãã§ããããããªãããšæã£ãŠãŸãã
ä»ã®ããšã§äœ¿ãããã®ã§ããã£ããGASã§äœãæ¹æ³ããŸãšããŠãããŸããã
ãšããããšã§ããããŸã§åºæ¥ããåè¬çã®æ¹ã¯æ¬¡ã®çºå±èª²é¡ãžé²ãã§ã¿ãŸãããã
å
ãªããžããªã§ã¯ã©ããã£ãŠéçšããŠããã®ããå®æããã³ãŒããšæ¯èŒããŠã¿ãã®ãè¯ãã§ãã
çºå±ïŒãããã¬ã®æ¹åæ¡
ãããããã®èšäºã®æ¹æ³ã¯ãéä¿¡ãããŸããã£ãŠããå Žåãã«æå¹ãªã®ã§ãããŸãè¡ã£ãŠããªãå Žåã¯æããããããã°ãã«ããã§ãã
éçºãããããªãããã«ïŒãšããèš³ã§ã¯ãªããã§ãããããŒã«ã«ã§ãéçºã§ããïŒclaspããããã§ãããç°å¢æ§ç¯ã®æéãããã®ã§äœ¿ããããªãäºãããäºãæ³å®ããŠããŸãã
ããŠããããªããšGASã®ãããã¬ã§ã¯ãªããGET/POST(curlã³ãã³ããšã)ã®printãããã°ã®æ¹æ³ãèããå¿
èŠããããŸãã
ã€ãŸããdoPostã¹ãã¬ããã·ãŒãã«æžã蟌ãã§ããæ¹æ³ã§ãã
以äžãåœè©²ã³ãŒãã
function output_sheet(value = {}){
const SSID = property("SSID").value;
const SSNAME = property("SSNAME").value;
const SHEET = SpreadsheetApp.openById(SSID).getSheetByName(SSNAME);
message = "[Skip] Not found [Spread-Sheet ID] or [Spread-Sheet Name] or [value(json)].";
if (SHEET != null) {
SHEET.appendRow([new Date(), JSON.stringify(value)]);
message = "[COMPLETE]add data";
}
Logger.log("output.gs/output_sheet: " + message);
return message;
}
output_sheet(JSON圢åŒ)
ã§å®è¡ãããšãã¹ãã¬ããã·ãŒãã«æžã蟌ãã§ãããã®ã§ãã
ããã§æž¡ãã¹ãåŒæ°ããdoPost(e)
ã®e
ã§ãã
propertyã¯ç¬èªé¢æ°ãªãã§æçž®ã§ãããã¹ãã¬ããã·ãŒãã®IDãšã·ãŒãåãæ ŒçŽããŠãããã®ãšçœ®ãæããŠãã ããã
SHEET.appendRow([ããŒã¿])
ã§å€ãæ ŒçŽããŠããŸããJSON.stringfy
ã§æååã«ããªããšç»é²ãããããŒã¿ã[Object, Object]
ã«ãªã£ãŠããŸããŸãã
泚æãã¹ããã€ã³ãã¯ãeã®äžã«JSONããŒã¿ã«ãªãæååãããããããJSON.parse
ã§ããŒã¿ãšããŠäœ¿ããããã«ããŠããå¿
èŠãããç¹ã§ãã
ãããã£ãããŒã¿ã®å 容ãæŽãåºãããã«ãããããã¬ã¯éèŠã§ãã
åè
æ£çŽãGAS(WebIDE)ã¯ãããã°èœåãã»ãŒãŒããªã®ã§ãã¡ãããšãããã°ããããªãããŒã«ã«ã§claspå
¥ããŠäœã£ãæ¹ãããã§ãã
nodeãžã®ç解ãšããŠããŠãå¿
èŠã§ãããç解ãããšå€§äœã®nodeéçºã«æŽ»ãããã®ã§åŠã¶äŸ¡å€ã¯é«ããšæããŸãã
ä»åã¯GASã®WebIDEã«åºå·ããŸããã
ããŒã«ãèªèº«ã§ããŸã䜿ããã°ãããªãã®ç°å¢ã¯æŽåã§ããŸãã
ããæ
£ããªããã¡ã¯ç¡çãã䜿ããæ©èœã掻çšããŠããäºãèããŸãããã
ããªãã«ãšã£ãŠããã®èšäºãã圹ã«ç«ãŠããªããðããããããã ãããšç§ã¯ãšãŠãå¬ããã§ãã
-
ãGETãšPOSTãããã§ã¯è©³ãã解説ããŸããããWebã§ããŒã¿ãããåãããæ¹æ³ã§ããLINEããããŒã¿ãåãåãããPOSTã䜿ããŸãã â©ïž
Discussion