@showin (匠 上地)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

ボタンを押すとLINEにメッセージ送信できるようにしたい

解決したいこと

ここに解決したい内容を記載してください。

例)
Rasberry pi4を使用してボタンを押すとLINEにメッセージ送信できるようなシステムを作っています。
実行しボタンを押した際にエラーが出てしました。
解決方法を教えてください。

発生している問題・エラー

Traceback (most recent call last):
  File "/home/pi/line.py", line 53, in <module>
    button.check_and_send()
  File "/home/pi/line.py", line 40, in check_and_send
    self._send()
  File "/home/pi/line.py", line 33, in _send
    headers = {not "NoneType" : "application/json; charset=UTF-8", "Authorization" : "Bearer " + self.token};
TypeError: can only concatenate str (not "NoneType") to str

該当するソースコード

import RPi.GPIO as GPIO
import time
import os
import json
import urllib.request

LINE_ACCESS_TOKEN=os.environ.get("LINE_ACCESS_TOKEN")
LINE_USER_ID=os.environ.get("LINE_USER_ID")

BUTTON_MESSAGE={}
BUTTON_MESSAGE[6]="こんにちは"
BUTTON_MESSAGE[13]="ありがとう"
BUTTON_MESSAGE[19]="さよなら"
BUTTON_MESSAGE[26]="また会いましょう"

class Button:
    gpio=None
    before=False
    now=False
    token=""
    to=""
    text=""

    def __init__(self,gpio,token,to,text):
        GPIO.setup(gpio, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
        self.gpio=gpio
        self.token=token
        self.to=to
        self.text=text

    def _send(self):
        data = {"to" : self.to, "messages" : [{"type" : "text", "text" : self.text}]};
        headers = {"Content-Type" : "application/json; charset=UTF-8", "Authorization" : "Bearer " + self.token};
        req = urllib.request.Request("https://api.line.me/v2/bot/message/push", json.dumps(data).encode(), headers)
        urllib.request.urlopen(req)

    def check_and_send(self):
        self.now=GPIO.input(self.gpio)
        if self.before==False and self.now ==True:
            self._send()
        self.before=self.now


if __name__ == '__main__':
    GPIO.setmode(GPIO.BCM)
    buttons=[]
    for key in BUTTON_MESSAGE:
        button=Button(key,LINE_ACCESS_TOKEN,LINE_USER_ID,BUTTON_MESSAGE[key])
        buttons.append(button)
    try:
        while True:
            for button in buttons:
                button.check_and_send()
            time.sleep(0.05)
    except KeyboardInterrupt:
        GPIO.cleanup()

自分で試したこと

https://yoshy-toshy.hateblo.jp/entry/2019/09/29/174732
を参考に制作しましたが、実行しても同じエラーがずっと出て治りませんでした。

0 likes

1Answer

str(None)でエラーになるかも?

def _send(self):
   data = {
       "to" : str(self.to),
       "messages" : [{
           "type" : "text",
           "text" : str(self.text)
       }]
   };
   headers = {
      "Content-Type" : "application/json; charset=UTF-8",
      "Authorization" : "Bearer " + str(self.token) 
   };
   url = "https://api.line.me/v2/bot/message/push"
  dat = json.dumps(data).encode()
   req = urllib.request.Request(url, dat, headers)
        
   urllib.request.urlopen(req)
   return

"Bearer " + self.token の箇所でしょうか? self.token は文字タイプ(None?)なんでしょうか?

LINE_ACCESS_TOKEN=os.environ.get("LINE_ACCESS_TOKEN")

暇人xinスーパー銭湯

1Like

Your answer might help someone💌