Tutorials:
NOTE: the 0 second is recommended for first time development
1. Go to the site: https://script.google.com/ *(NOTE: assuming you already have a GCP account)*
2. Create new project (new script) called LineBot and Copy-Paste the codes below
3. Save and Publish it (佈署為網路應用程式)
+ 專案版本: 新增 *(NOTE: 不要輸入版號)*
+ 具有應用程式存取權的使用者: 任何人、匿名者
4. Take a note of the public URL
5. Back to the site: https://developers.line.biz/en/ and input the URL above into the field 'Webhook URL'.
LineBot webhook script:
```
function doPost(e) {
var CHANNEL_ACCESS_TOKEN = 'Here is hash key for Channel-Access-Token';
var msg = JSON.parse(e.postData.contents);
console.log(msg);
return JSON.stringify({});
}
```
### Testing the webhook script
1. Add the Bot as a friend on your Line using QR code or searching the Line-ID of the Bot.
2. Go to the site: https://script.google.com/ , 選擇 **我的專案** , 選擇 **LineBot** , 在右邊的細項功能,選擇 **Stackdriver 記錄** .
3. 進入 **記錄** 頁面
*NOTE: 一旦 webhook 程序被 Line 觸發, 會將訊息顯示在這裡*
4. Sending a test message to the Bot from your Line
5. If webhook script works fine you will see something on the Stackdriver 記錄 web.
*NOTE: the logs will contain the info of the userid/groupid that the message is sent from*
## Sending a test message using API
To send a message to specified user or group using API the userid or groupid of the recipients are required for the API.
>NOTE: the userid/groupid is different from Line ID/Group name.
How to get the userid of the specified username:
1. Add the bot as a friend on the specified username
2. The user sends a test message to the Bot
3. The webhook script that we created will got some info that contains the userid or groupid.
Sending message using shell:
```
curl -v -X POST https://api.line.me/v2/bot/message/push \
-H 'Content-Type:application/json' \
-H 'Authorization: Bearer CHANNEL_ACCESS_TOKEN' \
-d '{
"to": "Ua683d0cd676d2fa5c03fc45a3c",
"messages":[
{
"type":"text",
"text":"From Bot : 測試"
}
]
}'
```
> my uid=Ua683d0cd676d5b43462fa5c03fc45a3c
> my gid=Cd3ac82cde8fd7f7467cd7ea6e3cd21b3
## Manager the Bot
Go to the URL: https://admin-official.line.me/
+ Getting the Line ID of the Bot or QR code so that adding the bot as a friend.
## Webhook Scripts
JAVA: sending an email with username info
```
var CHANNEL_ACCESS_TOKEN = '<CHANNEL_ACCESS_TOKEN>';
function getUsername(userId) {
var url = 'https://api.line.me/v2/bot/profile/' + userId;
var response = UrlFetchApp.fetch(url, {
'headers': {
'Authorization': 'Bearer ' + CHANNEL_ACCESS_TOKEN
}
});
return JSON.parse(response.getContentText()).displayName;
}
function doPost(e) {
var messageText = JSON.parse(e.postData.contents).events[0].message.text;
var userId = JSON.parse(e.postData.contents).events[0].source.userId;
var username = getUsername(userId);
MailApp.sendEmail('sample@mail.com', 'Forwarded LINE Messages', 'From: ' + username + String.fromCharCode(10) + messageText);
return JSON.stringify({});
```
## Reference
* [LINE Notify – LINE 提供的免費通知服務](https://mr117119.wordpress.com/2017/...C%8D%E5%8B%99/)
* [Line Message API 初戰!](https://kantai235.github.io/2017/03/...ineMessageAPI/)
* [【功能介紹】Messaging API](http://at-blog.line.me/tw/messaging_api_intro)
* [實作 LINE 聊天機器人 ( Google Apps Script )](https://www.oxxostudio.tw/articles/2...ps-script.html)
* [Chatbot 開發指南:使用 LINE Bot PHP SDK 打造問答型聊天機器人](https://www.appcoda.com.tw/line-chatbot-sdk/)
* [用 PHP 實現 Line Message API 接收系統訊息 ](https://blog.toright.com/posts/5727/...%E6%81%AF.html)
* [[Line Bot] Messaging Api 回話機器人 (PHP)](https://andynote.com/?p=90)
* [用 Docker 發送 Line 訊息](https://blog.wu-boy.com/2016/11/send...ten-in-golang/)
* [Line Chatbot 開發攻略(一)](https://medium.com/hondtour/line-cha...0-969fd592c08e)
* [LINE Chatbot 開發攻略(二)](https://medium.com/hondtour/line-cha...C-3ce2d54b21ae)
* [LINE Bot聊天機器人程式開發教學(七):從ESP8266/Arduino控制板觸發Line發布訊息](https://swf.com.tw/?p=1118)
* [使用C#開發LINE Bot系列文索引](http://studyhost.blogspot.com/search/label/LineBot)
* Line emojis
+ https://qiita.com/hisaharu/items/613baad81a4161c3c6c2
+ https://github.com/line/line-bot-sdk-php/issues/54