Woocomerce webhook not being recieved - python

I am playing around with the webhooks of WooCommerce and i have setup a hook that triggers when an item is being added to a cart.
I have a little Flask app that listens to requests and prints them out.
from flask import Flask, request, Response
app = Flask(__name__)
#app.route('/webhook', methods=['POST'])
def respond():
print(request.json);
return Response(status=200)
This script is being run on the same machine as the Wordpress server.
But when i add an item to my cart no calls are being made.
Does anyone know what i am doing wrong?

I still am unable to trigger any webhook to an url that points to an service on the same machine. I tried running my endpoint in a docker container and addressing the ip address of the container. I also tried making custom dns entries in my host file. Still didnt work. Eventually fixed my problem by using ngrok.

Related

Flask-Socketio emitting from an external process

I've been working on a project using flask, flask-socketio and redis
I have a server, and some modules I would like to be able to emit from outside of the server file.
server.py
from flask import Flask, Response, request, json
from flask_socketio import SocketIO, join_room, leave_room, emit
app = Flask(__name__)
socketio = SocketIO()
socketio.init_app(
app,
cors_allowed_origins="*",
message_que="redis://127.0.0.1:6379/0"
)
#socketio.on('ready')
def ready(data):
socketio.emit('rollCall', { 'message': 'Ive got work for you' }, room='Ready')
...
jobque.py
from modules.server.server import socketio
...
socketio.emit('rollCall', { 'message': 'Ive got work for you' }, room='Ready')
As it's currently configured, emits from the server file all work, the clients respond and they can talk back and forth. But when jobque goes to emit the same message, nothing happens. There's no error, and the client doesn't hear it.
I'm also using redis for things other than the websockets, I can get and set from it with no problem, in both files.
What do I need to do to get this external process to emit? I've looked through the flask-socketio documentation and this is exactly how they have it setup.
I've also tries creating a new SocketIO object inside jobque.py instead of importing the one form the server, but the results are the same
socket = SocketIO(message_queue="redis://127.0.0.1:6379/0")
socketio.emit('rollCall', { 'message': 'Ive got work for you' }, room='Ready')
I also went and checked if I could see the websocket traffic in redis with the message que setup using redis-cli > MONITOR, but I don't see any. I only see the operations I'm using redis for directly with the redis module. This makes me think the message que isn't actually being used, but I can't know for sure.
Unfortunately spelled message_queue as message_que was the issue.
Creating a new SocketIO instance without the app works now.

Github webhooks not working on Raspberry Pi 3 but works on Windows 10 computer

I am new to using raspberry pi's and don't really have any knowledge on flask.
So, I'm trying to implement Github webhooks with a python flask + ngrok to automatically git pull when a new push has been made to the repository. This is so I can just work on my Windows computer, push via Github Desktop and have it automatically pull the code from the repository using webhooks (the url from ngrok) to my raspberry's local repository. I use flask and ngrok to receive the webhook, which when received, starts pulling from the repo.
The issue is that the code for receiving the webhook doesn't work on my raspberry pi. The code below works perfectly fine on my Windows pc and works as expected, but when use the exact same code on my raspberry pi it just doesn't receive the webhook nor print anything. I don't really know what's going on, should work as expected.
from flask import Flask, request, json
import subprocess
app = Flask(__name__)
#app.route('/')
def root():
return 'Working'
# This receives the webhooks and does code when received
#app.route('/', methods=['POST'])
def webhook():
print("Receiving data...")
data = json.loads(request.data)
print ("\n\nNew commit by: {}".format(data['commits'][0]['author']['name'])) # Inform that someone has committed
print("\n\nPulling repository...")
subprocess.run(["git", "pull"]) # Pull new code from repo
print("Finished pulling.\nLocal repository up-to-date!\n\n")
return "OK"
if __name__ == '__main__':
app.run(debug=True)

Spotipy redirect uri opening on server instead of users browser?

I built a web app using Django and the wrapper for the Spotify api, Spotipy, and deployed it to Heroku. The problem I am facing is that the redirect uri opens on the machine running the code, which in this case is the linux server used by heroku. Because of this, the user never actually sees the page prompting them to authenticate the app and login to their Spotify accounts, resulting in a request timeout from Heroku. This happens when my REDIRECT_URI is set to http://localhost/. I have tried to set the REDIRECT_URI to https://nameofmyapp.herokuapp.com which then resulted in an EOFError from the Spotipy module. I have not been able to find a solution for this. For context, my authentication flow is set up as follows:
def index(request):
cache_handler = spotipy.DjangoSessionCacheHandler(request=request)
auth_manager = spotipy.oauth2.SpotifyOAuth(
env("CLIENT_ID"), env("CLIENT_SECRET"), env("REDIRECT_URI"), scope=env("SCOPE"), cache_handler=cache_handler)
session = spotipy.Spotify(
oauth_manager=auth_manager, requests_session=True)

Trading view alerts to trigger market order through python and Oanda's API

I'm trying to trigger a python module (market order for Oanda) using web hooks(from trading view).
Similar to this
1) https://www.youtube.com/watch?v=88kRDKvAWMY&feature=youtu.be
and this
2)https://github.com/Robswc/tradingview-webhooks-bot
But my broker is Oanda so I'm using python to place the trade. This link has more information.
https://github.com/hootnot/oanda-api-v20
The method is web hook->ngrok->python. When a web hook is sent, the ngrok (while script is also running) shows a 500 internal service error and that the server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
This is what my script says when its running (see picture);
First says some stuff related the market order then;
running script picture
One thing I noticed is that after Debug it doesn't say Running on... (so maybe my flask is not active?
Here is the python script;
from flask import Flask
import market_orders
# Create Flask object called app.
app = Flask(__name__)
# Create root to easily let us know its on/working.
#app.route('/')
def root():
return 'online'
#app.route('/webhook', methods=['POST'])
def webhook():
if request.method == 'POST':
# Parse the string data from tradingview into a python dict
print(market_orders.myfucn())
else:
print('do nothing')
if __name__ == '__main__':
app.run()
Let me know if there is any other information that would be helpful.
Thanks for your help.
I fixed it!!!! Google FTW
The first thing I learned was how to make my module a FLASK server. I followed these websites to figure this out;
This link helped me set up the flask file in a virtual environment. I also moved my Oanda modules to this new folder. And opened the ngrok app while in this folder via the command window. I also ran the module from within the command window using flask run.
https://topherpedersen.blog/2019/12/28/how-to-setup-a-new-flask-app-on-a-mac/
This link showed me how to set the FLASK_APP and the FLASK_ENV
Flask not displaying http address when I run it
Then I fixed the internal service error by adding return 'okay' after print(do nothing) in my script. This I learned from;
Flask Value error view function did not return a response

Unable to request the status code of localhost

I am using python and flask to create a web API. However I am getting trouble in requesting the HTTP status code of my localhost.
My code:
import requests
import flask
app = flask.Flask(__name__)
app.config["DEBUG"] = True
#app.route('/home', methods=['GET'])
def home():
r = requests.get(url="http://localhost:5000/home")
print(r.status_code)
return "Welcome!"
app.run()
Before adding the line for requesting status code, it works fine in my browser (Chrome) and the command prompt show something like this:
127.0.0.1 - - [19/Sep/2019 01:03:54] "GET /home HTTP/1.1" 200 -
After adding the line for requesting, it keeps loading (forever) in my browser and no response in the command prompt.
I have no idea about this problem because it did not show any error and I have read some of the solutions mentioned in other similar problems (like disabling proxy) but it seems not working for me.
Thanks!
Look at it from the point of view of the app. It gets a request for /home and routes it to home(). While servicing that request, the app makes a request for /home, which routes to home(). During the servicing of that request... and so on until some resource is exhausted.
If you intent is to prove that you can make a request to the app from within the app, target a different endpoint.

Categories

Resources