6
4

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チャットボット 作成 備忘録

Last updated at Posted at 2022-02-01

参考

LINE Developer 登録

・Developer name
・メールアドレス

Create a new channel

項目 説明 備考
Channel type MessageingAPI
Provider CreateNewPwovider
Channel icon 画像を指定
Channel name LINEの名前になる
日本語OK
Channel description 日本語OK
  • I have read and agree to the LINE Official Account Terms of Use
  • I have read and agree to the LINE Official Account API Terms of Use
  • Create
  • Messaging API settings タブのQRコードで友達登録

pipでline-bot-sdkをインストールする

pip install line-bot-sdk

うまくいかない時

項目 原因 備考
NameError: name 'pip' is not defined. Did you mean: 'zip'? python.exeを実行している pythonコマンドを打たず、直接pipコマンドを打つ
name 'pip' is not defined python.exeを実行している pythonコマンドを打たず、直接pipコマンドを打つ

pipが使えない linux

sudo apt update
sudo apt upgrade
sudo apt install python3-pip
pip install line-bot-sdk

チャンネルアクセストークンとユーザーID

  • pythonプロジェクトを作成する

  • プロジェクトと同じディレクトリにテキストファイルを作成し、「info.json」とする

  • LINE Developersのログイン画面 > Messaging API settingsタブの下の方 >
    Channel access token > [issue]・・・A

  • LINE Developersのログイン画面 > Basic settingsタブの下の方 > Your user ID ・・・B

A:チャンネルアクセストークン
B:ユーザーID

info.json
{
  "CHANNEL_ACCESS_TOKEN": "ajkdldjafdhkdjhrioutwrjfglksdfauiejfgsnflkbsjfbsdkljalfjeiouj114584toijrkilFU=",
  "USER_ID": "dakleiNiadkfiIoikdgnadldfjk857"
}

line.py
#!/usr/bin/env python3
#test
import json
file = open('info.json','r')
info = json.load(file)
print(info['CHANNEL_ACCESS_TOKEN'])
print(info['USER_ID'])
from linebot import LineBotApi
from linebot.models import TextSendMessage
CHANNEL_ACCESS_TOKEN = info['CHANNEL_ACCESS_TOKEN']
line_bot_api = LineBotApi(CHANNEL_ACCESS_TOKEN)

def main():
    USER_ID = info['USER_ID']
    messages= TextSendMessage(text="test")
    #※送信方法
    #line_bot_api.push_message(USER_ID,messages=messages)
    line_bot_api.broadcast(messages=messages)

if __name__ == "__main__":
    main()

参考

送信方法

送信方法 説明 備考
プッシュメッセージ ユーザー、グループ、トークルームを指定してメッセージを送信します。たとえば、ショッピングサイトで商品を購入したユーザーに対して、商品の発送を通知する場合に使用します。
ブロードキャストメッセージ LINE公式アカウントと友だちになっているすべてのユーザーに、同じメッセージを送信します。

javaから呼び出す方法

hoge.java
ProcessBuilder p = new ProcessBuilder("python","C:\\Users\\hoge\\Desktop\\sample\\test.py");
p.start();

引数を渡す場合

hoge.java
String msgStr ="メッセージの内容";
ProcessBuilder p = new ProcessBuilder("python","C:\\Users\\hoge\\Desktop\\sample\\test.py",msgStr);
p.start();
line.py
import sys
mesStr = sys.argv[1]
## 中略
messages= TextSendMessage(text =mesStr)

うまくいかない時(windows環境では動いたのに、Linux環境で動かなくなった)

症状 原因 備考
パスが存在しない パスの指定方法はあっているか。 【Windows】例: C:\Users\hoge\Desktop\sample\test.py
【linux】例: /home/hogeuser/sample/test.py
エラーの原因がわからない --- try~except句でエラーを特定する
No module named 'linebot' pip installしていない pip3 show line-bot-sdk で確認
No module named 'linebot' 権限を与えていない インストールされているディレクトリまでの全てのディレクトリの権限を確認する。
例:'/home/hogeuser/.local/lib/python3.8/site-packages のsite-packagesが権限700になっている等
権限詳細↓↓

try.py
try:
 #処理1
 #処理2
 #処理3
except Exception as e:
  #exctype:exceptonのタイプ、fname:例外ファイル名、exc_tb:例外発生行
  exc_type, exc_obj, exc_tb = sys.exc_info()
  fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
  print(str(exc_type) + str(e) +":"+ str(fname) +":"+ str(exc_tb.tb_lineno)  )
except:
  print('予期せぬエラー')

6
4
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
6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?