Tutorials:
Line Messaging API (Line Bot)
Create an Line Business ID and an new channel
- Go to the URL: https://developers.line.biz/en/ , Login as your Line account.
- Create a new provider
- Select Messaging API and create a channel
Configure the BOT
- Go to the URL: https://developers.line.biz/en/ , Login as your Line account.
- Select Provider List > your-new-provider-name
- Select your-new-app-name
- Select Channel settings
- Channel access token (long-lived): Issue a new access token
NOTE: the 0 second is recommended for first time development
- Allow bot to join group chats: Enabled
- Use webhooks: Enabled
- Webhook URL: NOTE: Please check the following steps
Create a webhook site using Google Apps Script service
- Go to the site: https://script.google.com/ (NOTE: assuming you already have a GCP account)
- Create new project (new script) called LineBot and Copy-Paste the codes below
- Save and Publish it (佈署為網路應用程式)
- 專案版本: 新增 (NOTE: 不要輸入版號)
- 具有應用程式存取權的使用者: 任何人、匿名者
- Take a note of the public URL
- 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
- Add the Bot as a friend on your Line using QR code or searching the Line-ID of the Bot.
- Go to the site: https://script.google.com/ , 選擇 我的專案 , 選擇 LineBot , 在右邊的細項功能,選擇 Stackdriver 記錄 .
- 進入 記錄 頁面
NOTE: 一旦 webhook 程序被 Line 觸發, 會將訊息顯示在這裡 - Sending a test message to the Bot from your Line
- 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:
- Add the bot as a friend on the specified username
- The user sends a test message to the Bot
- 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 : 測試"
}
]
}'
## 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({});