DAY20 - line message API 初体验

https://ithelp.ithome.com.tw/upload/images/20211005/20120107v7QWXOHJod.png

上一篇,申请好了 line message API 的频道,这一篇就来实际玩转 line message API 吧

line message API 官方文件

首先 line 官方是有文件的,不过没有中文,只有英文与日文,所以要选择一种自己熟悉的语言

line 官方文件

https://ithelp.ithome.com.tw/upload/images/20211005/20120107PM86UCCpWG.png

line message API 的种类

平常我们在传送 line 讯息的时候,多是以文字或贴图为主,但是其实line 的讯息有非常多种

https://ithelp.ithome.com.tw/upload/images/20211005/20120107GaoLzhyuAV.png

文字讯息

https://ithelp.ithome.com.tw/upload/images/20211005/20120107rpwVWsvBHH.png

贴图讯息

https://ithelp.ithome.com.tw/upload/images/20211005/20120107OsSVlz4v2b.png

图片讯息

https://ithelp.ithome.com.tw/upload/images/20211005/20120107vk3lhfvfJL.png

影片讯息

https://ithelp.ithome.com.tw/upload/images/20211005/20120107AspTE65cb3.png

声音讯息

https://ithelp.ithome.com.tw/upload/images/20211005/20120107WHlCGR8faS.png

位置讯息

https://ithelp.ithome.com.tw/upload/images/20211005/20120107G7t0YiNoeS.png

图片地图讯息

https://ithelp.ithome.com.tw/upload/images/20211005/20120107W1fW33wYJL.png

按钮讯息

https://ithelp.ithome.com.tw/upload/images/20211005/20120107Hqf88mJEcp.png

确认讯息

https://ithelp.ithome.com.tw/upload/images/20211005/20120107zxRrEyOKYb.png

橱窗讯息

https://ithelp.ithome.com.tw/upload/images/20211005/20120107h8r194qMis.png

开始使用 line message API

取得 api 资讯

首先,不管要做什麽,都有两件事情要做

  • 取得 line message API secret
  • 取得 line message API accessToken

先到昨天建立好的 line channel 页面,找到这两样资讯。一开始没有的话可以先按 issue 的按钮,发行一个新的

https://ithelp.ithome.com.tw/upload/images/20211005/201201075dFvxUH8fM.png

https://ithelp.ithome.com.tw/upload/images/20211005/20120107LDc8EN2wbk.png

在後端建立API

还记得一开始使用 Nx 建立的前後端专案吗?到目前为止,都只介绍到前端的部分,都没有使用到任何後端的项目。现在要开始在後端建立API了

安装 line bot sdk

npm install @line/bot-sdk --save 

安装line bot sdk 可以免除我们自己建立的後端与 line message API 之间沟通繁复的过程,只要使用line bot sdk,一切都会帮我们处理好

设定 line bot sdk

npm install @line/bot-sdk --save

推送讯息到 line

在这里示范三种讯息: 范本讯息文字讯息贴图讯息

使用 line bot sdk 有个好处,所有的型别它都帮你订好了,只要引用正确的类别,就可以知道要传什麽参数

import { Injectable } from '@nestjs/common';
import {
  ClientConfig,
  Client,
  TextMessage,
  MessageAPIResponseBase,
  TemplateMessage,
  StickerMessage,
} from '@line/bot-sdk';
import { from, Observable } from 'rxjs';

@Injectable()
export class AppService {
  clientConfig: ClientConfig = {
    channelAccessToken: '你的access token',
    channelSecret: '你的channel secret ',
  };
  client = new Client(this.clientConfig);
  groupId = '传送到群组的id';

  pushMessageToLineChannel(
    messageContent: any
  ): Observable<MessageAPIResponseBase> {
    const { imageUrl, name, message, docPath } = messageContent;
    const textMessage = `${name} 预约打卡罗` ;
    const templateMessage: TemplateMessage = {
      type: 'template',
      altText: textMessage,
      template: {
        type: 'buttons',
        thumbnailImageUrl: imageUrl,
        imageAspectRatio: 'rectangle',
        imageSize: 'cover',
        imageBackgroundColor: '#FFFFFF',
        title: textMessage,
        text: `${message}`,
        actions: [
          {
            type: 'uri',
            label: `看看${name}的打卡`,
            uri: `https://challenage90days.web.app/checkin/${docPath}`,
          },
        ],
      },
    };

    return from(this.client.pushMessage(this.groupId, templateMessage));
  }

  pushDayoffMessageToLineChannel({ name }): Observable<MessageAPIResponseBase> {
    const stickerMessage: StickerMessage = {
      type: 'sticker',
      packageId: '6362',
      stickerId: '11087923',
    };
    const textMessage: TextMessage = {
      type: 'text',
      text: `${name} 请假罗`,
    };

    return from(this.client.pushMessage(this.groupId, textMessage));
  }
}

范本讯息

结合之前前端送过来的打卡内容,将打卡内容做成范本讯息

const { imageUrl, name, message, docPath } = messageContent;
    const textMessage = `${name} 预约打卡罗` ;
    const templateMessage: TemplateMessage = {
      type: 'template',
      altText: textMessage,
      template: {
        type: 'buttons',
        thumbnailImageUrl: imageUrl,
        imageAspectRatio: 'rectangle',
        imageSize: 'cover',
        imageBackgroundColor: '#FFFFFF',
        title: textMessage,
        text: `${message}`,
        actions: [
          {
            type: 'uri',
            label: `看看${name}的打卡`,
            uri: `https://challenage90days.web.app/checkin/${docPath}`,
          },
        ],
      },
    };

贴图讯息

可以传送贴图,只要知道贴图系列的ID 和贴图的ID 就可以传送罗,如果不知道的话,官网上有贴图的对照表

 const stickerMessage: StickerMessage = {
      type: 'sticker',
      packageId: '6362',
      stickerId: '11087923',
    };

文字讯息

最一般的方式,像简讯一样,传送文字讯息

 const textMessage: TextMessage = {
      type: 'text',
      text: `${name} 请假罗`,
    };

推送讯息

最後只要使用 pushMessage 方法,就可以将讯息传送到 line 上面罗

this.client.pushMessage(this.groupId, 你的讯息类别)

传送出来的效果会这这样

https://ithelp.ithome.com.tw/upload/images/20211005/20120107ZRYHvEiqgY.png

这是对line message API 简单介绍,另外还有很多後端没有介绍的部分,像是nestjs的使用方法等等,就下一篇再介绍罗!


<<:  【Day22】导航元件 - Tabs

>>:  Day 20 ATT&CK for ICS - Evasion(2)

Day 25 埠映射与记忆体映射

输出与输入设备是在嵌入式系统里面,占有一个很重要的位置,所有的输入输出系统都必须透过设备控制暂存器,...

Day27 - 云端交易主机 - Ubuntu SSH登入 & 远端桌面

云端交易主机 - Ubuntu SSH登入 & 远端桌面 SSH登入 本机端建立SSH金钥 ...

[Day08] 什麽是贪婪法

#402 - Remove K Digits 连结: https://leetcode.com/p...

[Day 23] 资料分布与离群值处理

资料分布与离群值处理 今日学习目标 资料特徵观察与离群值分析 检视资料的分布状态 偏度 (Skewn...

虹语岚访仲夏夜-24(专业的小四篇)

我慢慢的告诉小路,我记得的事,包括那个R... 小路一直强调,只有我一个人参加,然後,不会有什麽NP...