Forums

Webhook callback SSL verification using LineBotAPI

Hello everyone, here is the scenario, I have a flask app running with the examplar file from the line-sdk-python, but whenever the verification of the webhook is called, it returns invalid reply token as the requested body, and on the developer manager, it displays the following error returned an invalid HTTP status code. (The expected status code is 200.) which I presume means nothing is return because the abort function of flask is called on the exception. I don't fully get what the issue is here.(On a side note, if I am running a flask app, where would the ouput stream be, like if I were to print hello world in a flask app, where would it be logged)I am pretty new to python, I am still learning the the more advanced concepts, so correct me if my comprehension is wrong, thanks.

code

import sys

from flask import Flask, request, abort

from linebot import (
    LineBotApi, WebhookHandler
)
from linebot.exceptions import (
    InvalidSignatureError
)
from linebot.models import (
    MessageEvent, TextMessage, TextSendMessage,
)

app = Flask(__name__)

line_bot_api = LineBotApi('CHANNEL_ACCESS_TOKEN')
handler = WebhookHandler('CHANNEL_SECRET')

print("hello world")

@app.route("/callback", methods=['GET', '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'


@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
    print("success")
    line_bot_api.reply_message(
        event.reply_token,
        TextSendMessage(text=event.message.text))


if __name__ == "__main__":
    app.run()

error log

2019-03-10 16:12:05,905: Request body: {
  "events": [
{
  "replyToken": "00000000000000000000000000000000",
  "type": "message",
  "timestamp": 1552234325207,
  "source": {
    "type": "user",
    "userId": "Udeadbeefdeadbeefdeadbeefdeadbeef"
  },
  "message": {
    "id": "100001",
    "type": "text",
    "text": "Hello, world"
  }
},
{
  "replyToken": "ffffffffffffffffffffffffffffffff",
  "type": "message",
  "timestamp": 1552234325207,
  "source": {
    "type": "user",
    "userId": "Udeadbeefdeadbeefdeadbeefdeadbeef"
  },
  "message": {
    "id": "100002",
    "type": "sticker",
    "packageId": "1",
    "stickerId": "1"
      }
    }
  ]
}
**NO MATCH**
2019-03-10 16:12:06,561: Exception on /callback [POST]
Traceback (most recent call last):
  File "/home/Pymauer/.virtualenvs/myproject/local/lib/python2.7/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
  File "/home/Pymauer/.virtualenvs/myproject/local/lib/python2.7/site-packages/flask/app.py", line 1815, in         full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/Pymauer/.virtualenvs/myproject/local/lib/python2.7/site-packages/flask/app.py", line 1718, in 
handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/Pymauer/.virtualenvs/myproject/local/lib/python2.7/site-packages/flask/app.py", line 1813, in             
full_dispatch_request
    rv = self.dispatch_request()
  File "/home/Pymauer/.virtualenvs/myproject/local/lib/python2.7/site-packages/flask/app.py", line 1799, in     
dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/Pymauer/mysite/flask_app.py", line 33, in callback
    handler.handle(body, signature)
  File "/home/Pymauer/.virtualenvs/myproject/local/lib/python2.7/site-packages/linebot/webhook.py", line 230, 
in handle
    func(event)
  File "/home/Pymauer/mysite/flask_app.py", line 45, in handle_message
    TextSendMessage(text=event.message.text))
  File "/home/Pymauer/.virtualenvs/myproject/local/lib/python2.7/site-packages/linebot/api.py", line 95, in     
reply_message
    '/v2/bot/message/reply', data=json.dumps(data), timeout=timeout
  File "/home/Pymauer/.virtualenvs/myproject/local/lib/python2.7/site-packages/linebot/api.py", line 561, in 
_post
    self.__check_error(response)
  File "/home/Pymauer/.virtualenvs/myproject/local/lib/python2.7/site-packages/linebot/api.py", line 584, in 
__check_error
    raise LineBotApiError(response.status_code, error)
LineBotApiError: LineBotApiError: status_code=400, error_response={"details": [], "message": "Invalid reply token"}

Regarding the error itself, you're probably best off asking the developers of the library. I can help on the side question, though -- it goes to the server log if it's a regular print like the one in your code (though you should add the keyword parameter flush=True), but if you print to the standard error stream, it will go to the error log.

Ok, thank you for answering my second question, I will definitely look into it