line/line-bot-sdk-python

Line bot doesn't reply any message

Closed this issue · 2 comments

#!/usr/bin/env python

from flask import Flask, request, abort

from linebot.v3 import (
WebhookHandler
)
from linebot.v3.exceptions import (
InvalidSignatureError
)
from linebot.v3.messaging import (
Configuration,
ApiClient,
MessagingApi,
ReplyMessageRequest,
TextMessage
)
from linebot.v3.webhooks import (
MessageEvent,
TextMessageContent
)

app = Flask(name)

configuration = Configuration(access_token='Access token from Line developer console')
handler = WebhookHandler('Channel secret from Line developer console')

@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:
    app.logger.info("Invalid signature. Please check your channel access token/channel secret.")
    abort(400)

return 'OK'

@handler.add(MessageEvent, message=TextMessageContent)
def handle_message(event):
with ApiClient(configuration) as api_client:
line_bot_api = MessagingApi(api_client)
line_bot_api.reply_message_with_http_info(
ReplyMessageRequest(
reply_token=event.reply_token,
messages=[TextMessage(text=event.message.text)]
)
)

if name == "main":
app.run()

System Informations

  • Python version: 3.9.21
  • SDK version: 3.14.2
  • OS: Rokcy Linux 9

Expected Behavior

Line Bot should reply any message.

Current Behavior

Line Bot doesn't reply any message.

Steps to Reproduce

  1. Running sample code from line-bot-sdk-python Github as shown above.
  2. Sending "Hi" from iPhone Line APP to Line Bot
  3. Line Bot doesn't reply any message.

Logs

./line_bot_test.py

  • Serving Flask app 'line_bot_test'
  • Debug mode: off
    WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
  • Running on http://127.0.0.1:5000
    Press CTRL+C to quit

Thank you for reporting the issue. The problem you're experiencing is likely due to the server running on localhost (http://127.0.0.1:5000), which is not accessible from the external internet. To ensure your LINE Bot can receive messages, the Webhook URL must be publicly accessible.

You might consider using tools like ngrok to expose your local server to the internet temporarily or deploying server publicly.

Thanks for your great support! I have used ngrok to expose your local server to the internet temporarily and this issue has been solved.