Forums

New to web api?

Hi guys. i've worked with Python on localhost for a while sometimes using XAMPP. i'm currently trying to host https://github.com/MasDenk/TradingViewTelegram.

Doing things localHost is alot easier, I can see things running/not running,

I've set the web app to the correct directory/file using flask, installed all dependancies, removed the line for flask in the main file since apparently it causes issues? I add a print statement to say the script ran and it shows when it runs.

but how do I know the script is functioning correctly? It relies on a webhook to send to Telegram and when I attempt to access via browser it gives a 404/207 error, an ap being hidden from a browser makes perfect sense to me, but giving me a 404 error also throws me off?

Did you check your web app's error log? (Logs are linked on the Web page)

Yes, I had my first webhook post and access log returned

  • [14/Feb/2024:00:00:07 +0000] "POST /main.py HTTP/1.1" 404 207 "-" "Go-http-client/1.1" response-time=0.002

which is weird because I have the WSGI configuration directing to the correct folder and the webhook has /main.py

So having this set as project_home = '/home/Jonmathers123/TradingViewTelegram' using /main.py doesn't seem to work

but if i set always on task using python /home/Jonmathers123/TradingViewTelegram/main.py it is able to locate and run the file

error logs returned nothing, they have errors from hours earlier while installing modules but nothing else. Server logs returned what I assume to be web api logs

2024-02-13 21:11:08 WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x561eda365e80 pid: 1 (default app) 2024-02-13 21:11:08 *** uWSGI is running in multiple interpreter mode *** 2024-02-13 21:11:08 gracefully (RE)spawned uWSGI master process (pid: 1) 2024-02-13 21:11:08 spawned uWSGI worker 1 (pid: 2, cores: 1) 2024-02-13 21:11:08 spawned uWSGI worker 2 (pid: 3, cores: 1) 2024-02-13 21:11:08 metrics collector thread started 2024-02-13 21:11:08 spawned 2 offload threads for uWSGI worker 2 2024-02-13 21:11:08 spawned 2 offload threads for uWSGI worker 1 2024-02-13 21:11:08 announcing my loyalty to the Emperor... 2024-02-13 21:42:57 announcing my loyalty to the Emperor...

Having main.py in the URL sounds unusual; what views have you defined (using the app.route decorator) in your code?

What should it be named? it's a cloned git but like I mentioned i've only really worked on desktop using CMD and visual studio so i've never needed to really get involved with flask and this is also alot different than typing cd and python main.py.

The decorator in main.py is:

@app.route("/webhook", methods=["POST"])
def webhook():
    try:
        if request.method == "POST":
            data = request.get_json()
            key = data["key"]
            if key == config.sec_key:
                print(get_timestamp(), "Alert Received & Sent!")
                send_alert(data)
                return "Sent alert", 200

            else:
                print("[X]", get_timestamp(), "Alert Received & Refused! (Wrong Key)")
                return "Refused alert", 400

    except Exception as e:
        print("[X]", get_timestamp(), "Error:\n>", e)
        return "Error", 400


if __name__ == "__main__":
    from waitress import serve

But I also see the wsgi only points to the directory, I assumed it would be similar to HTML where you could just name the file you wanted to access?

Looks like the endpoint is /webhook, did you try that? Also, do you see any errors in your web app's error log?

Ah I see!

No I was attempting to run the script via a webhook, not use the endpoint. I've tried /webhook and now get a 405 error so it looks like it's now right but obviously needs to be a post instruction not accessed from browser. I'm guessing I should keep this in always on tasks

No errors at all, I did put a simple print so it printed everytime it ran because the script ran quiet (which makes sense if the script is running right but I was accessing it wrong)

So am I right in thinking, I don't need the task to always be on, the webhook gets sent to /webhook, the server then loads up the script which then allows the POST instruction to be carried out?

Yes, that's right. If you have set up a website on the "Web" page with that code, then /webhook is the URL to use. And you don't need an always-on task to be running for the website to work. When a request comes in for your website, our system will start up your website code (if it's not already running) and then will direct the request to the code with the appropriate route decorator.

Ah OK awesome, yes I set it as that. I think i'm missing something else though? i'm getting a 400 error from the server, not the actual script. The script caters for 400 error in main.py

2024-02-16 02:00:31 400 Bad Request: The browser (or proxy) sent a request that this server could not understand. 2024-02-16 06:02:57 [X] 2024-02-16 06:02:57 Error:#012> 2024-02-16 06:02:57 400 Bad Request: The browser (or proxy) sent a request that this server could not understand. 2024-02-16 08:00:15 [X] 2024-02-16 08:00:15 Error:#012> 2024-02-16 08:00:15 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.

Could this be the mapping to main.py? When I set it up as a new web api I did point it at that file, but when I look at WSGI it only references the directory. The request is received as a post request and the script is coded to accept it as a post request.

I'm really not as bad as my posts make out. I just have a genuine hatred towards flask and my hard to grasp understanding on it. On PC whenever I needed a server I just loaded up XAMPP....ah, times were much simpler back then!

It looks like the log from the Exception handling from the view above. You could add more printing to see what is the exact error, how the request looks like etc.

Yes you're exactly right, it wasn't from the server it was the script. I didn't have the post data in JSON format. It works exactly as intended now. Thanks for your help guys, you're all legends. Enjoy my subscription fee! :D

Glad to hear that!