如何设置树莓派来接收webhooks [英] How to setup a Raspberry Pi to receive webhooks

查看:291
本文介绍了如何设置树莓派来接收webhooks的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在开发一个小项目,每当在Shopify上创建一个新订单时,我的树莓派就会点亮一个独角兽。我从来没有使用webhooks和web服务器,更不用说Flask或Zappa了,我很好奇如何设置这个,而不会将pi暴露在家庭网络上的开放式互联网上。



我已经读过,使用Amazon的Lambda与Flask和Zappa结合使用会很简单,但是我很遗憾。这是我到目前为止:

 从时间进口睡觉$ b $从瓶子进口烧瓶,请求
导入unicornhat作为独角兽
import light.py

app = Flask(__ name__)
@ app.route('/',methods = ['POST'])

def index():
data = request.get_json()
if data ['orders / create'] == null:
light.light()//指示灯uHat创建新订单
返回成功

任何指针都会被赞赏,我已经用了几个星期的时间(闲暇时间)和我在webdev节目中缺乏经验。我甚至不确定是否正确地阅读了Shopify的API信息,甚至正在监听正确的webhook。



再次感谢!


Shopify有一个Python模组,一个>,允许你注册你的webhook。

  import shopify 

shop_url =https://%s:%s @%s。 (shop_url)
shopify.Session.setup(api_key = API_KEY,secret = SHARED_SECRET)

new_webhook = shopify.Webhook()
new_webhook.address ='http://your.pi.address'
new_webhook.topic ='orders / create'
new_webhook.save()

一旦完成,任何创建的命令都会调用webhook发送订购数据到你的地址。
对于您可以用作触发器的其他事件,请参阅 API文档



您的Flask应用程序可以接受这样的帖子:

 导入烧瓶,请求
导入灯

app =烧瓶(__ name__)

@ app.route('/',methods = ['POST'])
def index():
data = request.json#可选
light.blink()
返回成功

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

不需要对订单数据做任何事情,
,但是检查和/或登录可能会很好。


I am currently working on a little project that will have my raspberry pi light up a unicornhat whenever a new order is created on Shopify. I have never worked with webhooks nor web servers, much less Flask or Zappa before, and I was curious as to how I would set this up without exposing the pi to the open internet on my home network.

I had read that this would be simple to do using Amazon's Lambda in conjunction with Flask and something called Zappa, however I am rather lost. This is what I have so far:

from time import sleep
from flask import Flask, request
import unicornhat as unicorn
import light.py

app = Flask(__name__)
@app.route('/', methods = ['POST'])

def index():
    data = request.get_json()
    if data['orders/create'] == null:
        light.light() //lights uHat on new order creation
    return "Success"

Any pointers would be much appreciated, I've been banging my head over this for several weeks (in my spare time) and my inexperience in webdev shows. I'm not even sure if I read Shopify's API information correctly for it to even be listening for the correct webhook.

Thanks again!

解决方案

Shopify has a Python module, shopifyapi that allows you to register your webhook.

import shopify

shop_url = "https://%s:%s@%s.myshopify.com/admin" % (API_KEY, PASSWORD, SHOP_NAME)
shopify.ShopifyResource.set_site(shop_url)
shopify.Session.setup(api_key=API_KEY, secret=SHARED_SECRET)

new_webhook = shopify.Webhook()
new_webhook.address = 'http://your.pi.address'
new_webhook.topic = 'orders/create'
new_webhook.save()

Once that's done any orders created will call the webhook to send order data to your pi's address. For additional events you can use as a trigger see the API docs.

Your Flask app could accept posts like this:

from flask import Flask, request
import light

app = Flask(__name__)

@app.route('/', methods = ['POST'])
def index():
    data = request.json  # optional
    light.blink()
    return "Success"

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

For what you're trying to accomplish you don't need to do anything with the order data, but it might be nice to inspect and/or log.

这篇关于如何设置树莓派来接收webhooks的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆