如何设置 Raspberry Pi 以接收网络钩子 [英] How to setup a Raspberry Pi to receive webhooks

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

问题描述

我目前正在做一个小项目,只要在 Shopify 上创建新订单,我的树莓派就会点亮一顶独角兽帽.我以前从未使用过 webhooks 或 web 服务器,更不用说 Flask 或 Zappa,我很好奇如何在不将 pi 暴露给家庭网络上的开放互联网的情况下进行设置.

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.

我已经读到将 Amazon 的 Lambda 与 Flask 和称为 Zappa 的东西结合使用会很简单,但是我很迷茫.这是我目前所拥有的:

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"

任何指点都将不胜感激,我已经为此纠结了几个星期(在我的业余时间)并且我在 webdev 节目中缺乏经验.我什至不确定我是否正确阅读了 Shopify 的 API 信息,以便它甚至可以侦听正确的 webhook.

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.

再次感谢!

推荐答案

Shopify 有一个 Python 模块,shopifyapi 允许您注册您的网络钩子.

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()

一旦完成,任何创建的订单都会调用 webhook 将订单数据发送到你的pi地址.有关您可以用作触发器的其他事件,请参阅 API 文档.

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.

你的 Flask 应用可以接受这样的帖子:

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.

这篇关于如何设置 Raspberry Pi 以接收网络钩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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