

PythonとHerokuを用いて対話型のLINEBOTを作っているのですが、いくらデプロイしても返答がうまく返信されません。  
また、herokuのログを確認しても以下のようなエラーが表示されます。どうすれば改善できますか？よろしくお願いします。

補足  
Windowsで作業を行っています。  
また、 <https://junpage.com/line-bot-development/> のサイトの作り方をもとに作業を行っております。  
また、デプロイ方法ですが、このサイトの示してある通りにデプロイを行いました。

コードは以下になります。

[code]

    from flask import Flask, request, abort
    import requests, os
    from linebot import LineBotApi, WebhookHandler
    from linebot.exceptions import InvalidSignatureError
    from linebot.models import MessageEvent, TextMessage, TextSendMessage, ImageMessage, ImageSendMessage, FollowEvent, UnfollowEvent
    from PIL import Image
    from io import BytesIO
    import psycopg2
    
    
    LINE_CHANNEL_ACCESS_TOKEN = "~~~~~あなたのチャネルアクセストークン~~~~~"
    LINE_CHANNEL_SECRET = "~~~~~あなたのチャネルシークレット~~~~~"
    DATABASE_URL = "~~~~~あなたのデータベースURL~~~~~" # 後ほどHerokuでPostgreSQLデータベースURLを取得
    HEROKU_APP_NAME = "~~~~~あなたのHerokuアプリ名~~~~~" # 後ほど作成するHerokuアプリ名
    
    app = Flask(__name__)
    Heroku = "https://{}.herokuapp.com/".format(HEROKU_APP_NAME)
    
    line_bot_api = LineBotApi(LINE_CHANNEL_ACCESS_TOKEN)
    handler = WebhookHandler(LINE_CHANNEL_SECRET)
    
    header = {
        "Content_Type": "application/json",
        "Authorization": "Bearer " + LINE_CHANNEL_ACCESS_TOKEN
    }
    
    @app.route("/")
    def hello_world():
        return "hello world!"
    
    
    # アプリにPOSTがあったときの処理
    @app.route("/callback", methods=["POST"])
    def callback():
        # get X-Line-Signature header value
        signature = request.headers["X-Line-Signature"]
        # get request body as text
        body = request.get_data(as_text=True)
        app.logger.info("Request body: " + body)
        # handle webhook body
        try:
            handler.handle(body, signature)
        except InvalidSignatureError:
            abort(400)
        return "OK"
    
    
    # botにメッセージを送ったときの処理
    @handler.add(MessageEvent, message=TextMessage)
    def handle_message(event):
        line_bot_api.reply_message(
            event.reply_token,
            TextSendMessage(text=event.message.text))
        print("返信完了!!\ntext:", event.message.text)
    
    
    # botに画像を送ったときの処理
    @handler.add(MessageEvent, message=ImageMessage)
    def handle_image(event):
        print("画像を受信")
        message_id = event.message.id
        image_path = getImageLine(message_id)
        line_bot_api.reply_message(
            event.reply_token,
            ImageSendMessage(
                original_content_url = Heroku + image_path["main"],
                preview_image_url = Heroku + image_path["preview"]
            )
        )
        print("画像の送信完了!!")
    
    
    # 受信メッセージに添付された画像ファイルを取得
    def getImageLine(id):
        line_url = f"https://api-data.line.me/v2/bot/message/{id}/content"
        result = requests.get(line_url, headers=header)
        print(result)
    
        img = Image.open(BytesIO(result.content))
        w, h = img.size
        if w >= h:
            ratio_main, ratio_preview = w / 1024, w / 240
        else:
            ratio_main, ratio_preview = h / 1024, h / 240
    
        width_main, width_preview = int(w // ratio_main), int(w // ratio_preview)
        height_main, height_preview = int(h // ratio_main), int(h // ratio_preview)
    
        img_main = img.resize((width_main, height_main))
        img_preview = img.resize((width_preview, height_preview))
        image_path = {
            "main": f"static/images/image_{id}_main.jpg",
            "preview": f"static/images/image_{id}_preview.jpg"
        }
        img_main.save(image_path["main"])
        img_preview.save(image_path["preview"])
        return image_path
    
    
    # データベース接続
    def get_connection():
        return psycopg2.connect(DATABASE_URL, sslmode="require")
    
    
    # botがフォローされたときの処理
    @handler.add(FollowEvent)
    def handle_follow(event):
        profile = line_bot_api.get_profile(event.source.user_id)
        with get_connection() as conn:
            with conn.cursor() as cur:
                conn.autocommit = True
                cur.execute('CREATE TABLE IF NOT EXISTS users(user_id TEXT)')
                cur.execute('INSERT INTO users (user_id) VALUES (%s)', [profile.user_id])
                print('userIdの挿入OK!!')
                cur.execute('SELECT * FROM users')
                db = cur.fetchall()
        print("< データベース一覧 >")
        for db_check in db:
            print(db_check)
    
    
    # botがアンフォロー(ブロック)されたときの処理
    @handler.add(UnfollowEvent)
    def handle_unfollow(event):
        with get_connection() as conn:
            with conn.cursor() as cur:
                conn.autocommit = True
                cur.execute('DELETE FROM users WHERE user_id = %s', [event.source.user_id])
        print("userIdの削除OK!!")
    
    
    # データベースに登録されたLINEアカウントからランダムでひとりにプッシュ通知
    def push():
        with get_connection() as conn:
            with conn.cursor() as cur:
                cur.execute('SELECT * FROM users ORDER BY random() LIMIT 1')
                (to_user,) = cur.fetchone()
        line_bot_api.multicast([to_user], TextSendMessage(text="今日もお疲れさん!!"))
    
    
    # アプリの起動
    if __name__ == "__main__":
        # 初回のみデータベースのテーブル作成
        with get_connection() as conn:
            with conn.cursor() as cur:
                conn.autocommit = True
                cur.execute('CREATE TABLE IF NOT EXISTS users(user_id TEXT)')
        
        # LINE botをフォローしているアカウントのうちランダムで一人にプッシュ通知
        push()
        port = int(os.getenv("PORT", 5000))
        app.run(host="0.0.0.0", port=port)
    ### End
    
[/code]

プロックファイルの中身は以下のようにしてあります。

[code]

    web: python main.py
    
[/code]

また、`heroku logs` でログを確認するとこのようにエラーが表示されます。

[code]

    Warning: heroku update available from 7.53.0 to 8.5.0.
    2023-11-13T19:38:41.227541+00:00 app[web.1]: handler = WebhookHandler(LINE_CHANNEL_SECRET)
    2023-11-13T19:38:41.260711+00:00 app[web.1]: Traceback (most recent call last):
    2023-11-13T19:38:41.260713+00:00 app[web.1]: File "/app/main.py", line 150, in <module>
    2023-11-13T19:38:41.260845+00:00 app[web.1]: push()
    2023-11-13T19:38:41.260877+00:00 app[web.1]: File "/app/main.py", line 137, in push
    2023-11-13T19:38:41.260925+00:00 app[web.1]: (to_user,) = cur.fetchone()
    2023-11-13T19:38:41.260956+00:00 app[web.1]: ^^^^^^^^^^
    2023-11-13T19:38:41.260964+00:00 app[web.1]: TypeError: cannot unpack non-iterable NoneType object
    2023-11-13T19:38:41.385596+00:00 heroku[web.1]: Process exited with status 1
    2023-11-13T19:38:41.413061+00:00 heroku[web.1]: State changed from starting to crashed
    2023-11-14T01:06:58.771692+00:00 heroku[web.1]: State changed from crashed to starting
    2023-11-14T01:07:02.498837+00:00 heroku[web.1]: Starting process with command `python main.py`
    2023-11-14T01:07:03.554890+00:00 app[web.1]: /app/main.py:19: LineBotSdkDeprecatedIn30: Call to deprecated class LineBotApi. (Use v3 class; linebot.v3.<feature>. See https://github.com/line/line-bot-sdk-python/blob/master/README.rst for more details.) -- Deprecated since version 3.0.0.
    2023-11-14T01:07:03.554914+00:00 app[web.1]: line_bot_api = LineBotApi(LINE_CHANNEL_ACCESS_TOKEN)
    2023-11-14T01:07:03.554931+00:00 app[web.1]: /app/main.py:20: LineBotSdkDeprecatedIn30: Call to deprecated class WebhookHandler. (Use 'from linebot.v3.webhook import WebhookHandler' instead. See https://github.com/line/line-bot-sdk-python/blob/master/README.rst for more details.) -- Deprecated since version 3.0.0.
    2023-11-14T01:07:03.554932+00:00 app[web.1]: handler = WebhookHandler(LINE_CHANNEL_SECRET)
    2023-11-14T01:07:03.579665+00:00 app[web.1]: Traceback (most recent call last):
    2023-11-14T01:07:03.579671+00:00 app[web.1]: File "/app/main.py", line 150, in <module>
    2023-11-14T01:07:03.579802+00:00 app[web.1]: push()
    2023-11-14T01:07:03.579846+00:00 app[web.1]: File "/app/main.py", line 137, in push
    2023-11-14T01:07:03.579919+00:00 app[web.1]: (to_user,) = cur.fetchone()
    2023-11-14T01:07:03.579958+00:00 app[web.1]: ^^^^^^^^^^
    2023-11-14T01:07:03.579968+00:00 app[web.1]: TypeError: cannot unpack non-iterable NoneType object
    2023-11-14T01:07:03.706332+00:00 heroku[web.1]: Process exited with status 1
    2023-11-14T01:07:03.731035+00:00 heroku[web.1]: State changed from starting to crashed
    2023-11-14T06:51:21.555943+00:00 heroku[web.1]: State changed from crashed to starting
    2023-11-14T06:51:24.991038+00:00 heroku[web.1]: Starting process with command `python main.py`
    2023-11-14T06:51:26.025249+00:00 app[web.1]: /app/main.py:19: LineBotSdkDeprecatedIn30: Call to deprecated class LineBotApi. (Use v3 class; linebot.v3.<feature>. See https://github.com/line/line-bot-sdk-python/blob/master/README.rst for more details.) -- Deprecated since version 3.0.0.
    2023-11-14T06:51:26.025266+00:00 app[web.1]: line_bot_api = LineBotApi(LINE_CHANNEL_ACCESS_TOKEN)
    2023-11-14T06:51:26.025293+00:00 app[web.1]: /app/main.py:20: LineBotSdkDeprecatedIn30: Call to deprecated class WebhookHandler. (Use 'from linebot.v3.webhook import WebhookHandler' instead. See https://github.com/line/line-bot-sdk-python/blob/master/README.rst for more details.) -- Deprecated since version 3.0.0.
    2023-11-14T06:51:26.025293+00:00 app[web.1]: handler = WebhookHandler(LINE_CHANNEL_SECRET)
    2023-11-14T06:51:26.050262+00:00 app[web.1]: Traceback (most recent call last):
    2023-11-14T06:51:26.050263+00:00 app[web.1]: File "/app/main.py", line 150, in <module>
    2023-11-14T06:51:26.050342+00:00 app[web.1]: push()
    2023-11-14T06:51:26.050368+00:00 app[web.1]: File "/app/main.py", line 137, in push
    2023-11-14T06:51:26.050416+00:00 app[web.1]: (to_user,) = cur.fetchone()
    
[/code]


CC BY-SA 4.0
2023-11-10T04:30:07.887
96731
2023-11-15T07:31:42.763
2023-11-15T07:31:42.763
3060
60372
question
python heroku line デプロイ
PythonとHerokuを用いて対話型のLINEBOTの作成で困っています