How to use Linebot with Flask

世霖 趙 0 Reputation points
2024-04-14T12:36:30.6866667+00:00

I deployed a Flask web app on my Azure account, and I'd like to use it as the webhook URL for my linebot. I've tested the code by ngrok, and it works. However, if I use the website " https://practice-linebot-2024.azurewebsites.net/ ", the linebot won't reply to anything.

I tested this webhook URL on Line Developers, and it replies, "

The webhook returned an HTTP status code other than 200. (405 Method Not Allowed)

Confirm that your bot server returns status code 200 in response to the HTTP POST request sent from the LINE Platform. For more information, see Response in the Messaging API Reference. "

What should I do now?

(By the way, I barely know anything about websites or https. I'd appreciate you use some easy words to reply.)

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,939 questions
{count} votes

1 answer

Sort by: Most helpful
  1. ajkuma 28,036 Reputation points Microsoft Employee Moderator
    2024-04-17T08:12:39.4466667+00:00

    @世霖 趙 , From your webapp URL

    https://practice-linebot-2024.azurewebsites.net/ , I see that it's displaying default app (screenshot below).

    The default app appears because you either haven't deployed your app code to App Service, or App Service failed to find your app code and ran the default app instead.

    If the App Service doesn't find a custom command, ( your Flask app), then it runs a default read-only app, located in the opt/defaultsite folder

    Typically, the error message you received indicates that the LINE platform is not able to communicate with your Flask web app running on Azure. The 405 Method Not Allowed error suggests that the LINE platform is sending an HTTP request to your web app using an HTTP method that is not supported by your app.
    To isolate, you can first check the functionality of your webapp locally if it works fine as expected.

    To isolate the issue with default app being displayed, kindly try the following steps:

    • Use SSH to connect directly to the App Service container and verify that your files exist under site/wwwroot. If your files don't exist, use the following steps:
      1. Create an app setting named SCM_DO_BUILD_DURING_DEPLOYMENT with the value of 1, redeploy your code, wait a few minutes, then try to access the app again. For more information on creating app settings, see Configure an App Service app in the Azure portal.
      2. Review your deployment process, check the deployment logs, correct any errors, and redeploy the app.
    • If your files exist, then App Service wasn't able to identify your specific startup file. Check that your app is structured as App Service expects for Flask, or use a custom startup command.
    1. Check that your Flask app is configured to listen on the correct port. You can check the port number by looking at the PORT environment variable in your Azure app settings.

    User's image

    Checkout this doc for more info: Configure a Linux Python app for Azure App Service


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.