자바스크립트를 활성화 해주세요

p082 Line SDK써보기, 30일만 사용할 수 있는 short-lived access token 으로 메시지하기

 ·  ☕ 3 min read

Line의 SDK를 사용하는 방법의 핵심은 channel access token 을 얻는 것입니다. 관련문서를 읽어보면 channel access token에는 3종류가 있습니다.

channel access token

일단 access token을 취득한 후에는, 메시지를 보내는 것은 동일한 형식입니다.

channel id 와 channel secret

Token을 구하려면 channel idchannel secret이 필요합니다.
Line Developer Consone이라고 하는 Dashboard에 개발자 등록신청을 하면서 Message Channel을 만들면, channel idchannel secret을 구할 수 있습니다.

https://developers.line.biz/console/channel/채널ID

p082_line_console_channel_id.png

이미지에 보이는 저 QR-Code를 접속해보면, bot을 친구등록하는 화면으로 넘어갑니다.

access token 취득 테스트

친절하게도 web에서 테스트해볼 수 있는 화면을 제공해 주고 있습니다. 우리는 여기에서 30일용 토근을 구할 수 있습니다.

https://developers.line.biz/en/reference/messaging-api/#issue-shortlived-channel-access-token

p082_line_short_token_online_test.png

메시지 받을사람에게 bot을 친구로 등록

친구로 등록하지 않으면 메시지 보낼 수 없죠? 우선은 친구등록을 한 자신의 UserID에게 보낼 수 있습니다.

친구등록은 Developer Console의 QR코드로 등록할 수 있습니다.

p082_add_linebot_as_friend.png

또, 친구로 등록한 유저들에게 안내 메시지를 보낼 수 있는 공식 계정 관리자라고 하는 별도의 대쉬보드도 제공하고 있습니다.

p082_line_official_account_manager.png

사실, 일본에서는 많은 관공소, 학교, 상점에서는 이를 이용해서 서비스 이용자, 고객에게 메시지를 보내고 있는데, 이 기능을 이용하는 것입니다.

message push 테스트

Message Push API 설명은 이쪽에 있습니다.

https://developers.line.biz/en/reference/messaging-api/#send-push-message

To는 userId, groupId, 또는 roomId가 될 수 있다고 합니다.

p082_line_push_message_test.png

자기 자신의 UserId는 Line Developer Console의 ChannelID가 있는 부분에서 구할 수 있습니다.

자 그럼 보내봅니다. 다음의 출력문중 개인정보는 적당히 변경했습니다.

[tkim@dev-int-api-v0001 ~(keystone_admin)]$ curl -v -X POST https://api.line.me/v2/bot/message/push \
> -H 'Content-Type: application/json' \
> -H 'Authorization: Bearer HelloWorldscXtiaiUZXJ1WgBzokaIJ68Jqz5rym4r78F/vjpkXZDKcF01cdLJykoNz+2fcJMGkZCugRpy+m/A0cPMR2WTpgOr40Ga1kqvI8txD9ixEm1xPu+lGgqDC6NQeFLdUaYek+E49PbdgDzCFqoOLOYbqAITQ=' \
> -d '{
>     "to": "Uf57408fac1566f8fc6c36f3HelloWorld",
>     "messages":[
>         {
>             "type":"text",
>             "text":"Hello, world1"
>         },
>         {
>             "type":"text",
>             "text":"Hello, world2"
>         }
>     ]
> }'
Note: Unnecessary use of -X or --request, POST is already inferred.
*   Trying 203.104.153.64:443...
* Connected to api.line.me (203.104.153.64) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
*   CAfile: /home/tkim/miniconda/ssl/cacert.pem
  CApath: none
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
* TLSv1.3 (IN), TLS handshake, Certificate (11):
* TLSv1.3 (IN), TLS handshake, CERT verify (15):
* TLSv1.3 (IN), TLS handshake, Finished (20):
* TLSv1.3 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.3 (OUT), TLS handshake, Finished (20):
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
* ALPN, server accepted to use h2
* Server certificate:
*  subject: C=JP; ST=Tokyo-to; L=Shinjuku-ku; O=LINE Corporation; CN=*.line.me
*  start date: Jun 17 06:01:58 2020 GMT
*  expire date: Sep  5 12:00:00 2022 GMT
*  subjectAltName: host "api.line.me" matched cert's "*.line.me"
*  issuer: C=BE; O=GlobalSign nv-sa; CN=GlobalSign RSA OV SSL CA 2018
*  SSL certificate verify ok.
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* Using Stream ID: 1 (easy handle 0x563de0b776a0)
> POST /v2/bot/message/push HTTP/2
> Host: api.line.me
> user-agent: curl/7.71.1
> accept: */*
> content-type: application/json
> authorization: Bearer HelloWorldO7A4wj2scXtiaiUZXJ1WgBzokaIJ68Jqz5rym4r78F/vjpkXZDKcF01cdLJykoNz+2fcJMGkZCugRpy+m/A0cPMR2WTpgOr40Ga1kqvI8txD9ixEm1xPu+lGgqDC6NQeFLdUaYek+E49PbdgDzCFqoOLOYbqAITQ=
> content-length: 238
>
* We are completely uploaded and fine
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* old SSL session ID is stale, removing
* Connection state changed (MAX_CONCURRENT_STREAMS == 128)!

< HTTP/2 200
< server: openresty
< date: Tue, 02 Mar 2021 07:01:45 GMT
< content-type: application/json
< x-line-request-id: f169947a-c1a3-4f6f-b5f3-49f64a498626
< x-content-type-options: nosniff
< x-xss-protection: 1; mode=block
< cache-control: no-cache, no-store, max-age=0, must-revalidate
< pragma: no-cache
< expires: 0
< x-frame-options: DENY
<
* Connection #0 to host api.line.me left intact
[tkim@dev-int-api-v0001 ~(keystone_admin)]$

결과 화면은 다음과 같습니다.

p082_line_sdk_result.png

Ref

공유하기

tkim
글쓴이
tkim
Software Engineer