3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

自分のLINEにメッセージを送るLINE Bot in LINE Messaging API

Last updated at Posted at 2023-11-03

概要

LINE Messaging APIで個人lineにメッセージを送信する方法の基本形を紹介する.
BashとGASとPythonのそれぞれの場合について紹介する.

必要な準備

  • LINE Messaging APIでチャンネルをあらかじめ作成
  • Channel Access Tokenを発行して控える(チャンネル画面で発行可能)
  • 自分のLINEのUser IDを控える(チャンネル画面で確認可能)
  • 作成したチャンネルを自分の個人lineに友達登録する

やり方

Bashの場合

以下のコマンドを打ち込む(参考文献)

curl -v -X POST https://api.line.me/v2/bot/message/push \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer チャネルアクセストークン' \
-d '{
    "to": "送り先のユーザーID",
    "messages":[
        {
            "type":"text",
            "text":"送りたいメッセージ1"
        },
        {
            "type":"text",
            "text":"送りたいメッセージ2"
        }
    ]
}'

GASの場合

  • あらかじめGoogle Apps ScriptでWebアプリを作成
  • .gsファイルを作成し、以下のコードを入力
  • 関数sendMessageを右上のメニューから実行する
const sendMessage = ()=>{
  const ACCESS_TOKEN = "チャンネルアクセストークン";
  const USER_ID = "自分のユーザーID";

  const URL = "https://api.line.me/v2/bot/message/push";
  const RES = UrlFetchApp.fetch(URL, {
    "headers": {
      "Content-Type": "application/json; charset=UTF-8",
      "Authorization": "Bearer " + ACCESS_TOKEN,
    },
    "method": "post",
    "payload": JSON.stringify({
      "to": USER_ID,
      "messages":[{
              "type":"text",
              "text":"送りたいメッセージ1"
          },
          {
              "type":"text",
              "text":"送りたいメッセージ2"
          }]
    }),
  });

  return RES;
}

Pythonの場合

工事中

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?