LINE push message

    版本為 07:55, 30 Nov 2024

    到這個版本。

    返回到 版本存檔.

    查閱目前版本

    Tutorials:

    Line Messaging API (Line Bot)

    Create an Line Business ID and an new channel
    1. Go to the URL: https://developers.line.biz/en/ , Login as your Line account.
    2. Create a new provider
    3. Select Messaging API and create a channel
    Configure the BOT
    1. Go to the URL: https://developers.line.biz/en/ , Login as your Line account.
    2. Select Provider List > your-new-provider-name
    3. Select your-new-app-name
    4. 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
    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": "USERID_OF_RECIPIENT",
        "messages":[
            {
                "type":"text",
                "text":"From Bot : 測試"
            }
        ]
    }'
    

    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({});
    
    Powered by MindTouch Core