当前位置: 首页 > 编程日记 > 正文

Webhooks上的一个简单方法:恐吓现在停止

Webhook.

Webhook。

It sounds like what happens when you cross a spider and a pirate. In the world of the internet though, webhooks are something completely different. Webhooks help connect services together.

听起来就像当您越过蜘蛛和海盗时会发生什么。 但是,在互联网世界中,网络钩子是完全不同的东西。 Webhooks帮助将服务连接在一起。

Let me explain.

让我解释。

Say we have two hypothetical services. One is a service that generates data, and the other that gathers and organizes that data.

说我们有两个假设的服务。 一个是生成数据的服务,另一个是收集和组织数据的服务。

The developers of the first service thought, “Man, our platform is only so useful. Let’s give the users the ability to forward real-time data to other services.”

第一项服务的开发人员认为:“伙计,我们的平台是如此有用。 让我们为用户提供将实时数据转发到其他服务的能力。”

The developers of the second service thought. “Gee willikers, it would be great if our users could import data easier.” So, they added webhooks to receive data in real time from a service like the first service.

第二种服务的开发者认为。 “ Gee willikers,如果我们的用户可以更轻松地导入数据,那就太好了。” 因此,他们添加了Webhook,以从诸如第一个服务之类的服务实时接收数据。

Now as a user, you happen to use both services. You now have the power in your hands to connect them together.

现在,作为用户,您碰巧同时使用了这两种服务。 现在,您可以将它们连接在一起。

The best way to explain it is with a real-world example.

解释它的最好方法是使用一个实际示例。

真实的例子 (Real World Example)

On a recent project, I connected an IoT sensor to a Google Docs Sheet. It took me only about 10 minutes. I’m going to show you the same right now.

在最近的项目中 ,我将IoT传感器连接到Google文档表。 我只花了大约10分钟。 我现在将向您展示相同的内容。

首先让我们开始设置Google表格。 (Let’s first start by setting up the Google Sheet.)

  • Create a new sheet

    建立新工作表
  • Once you’re there, go to Tools and click Script editor

    到那里后,转到“ 工具” ,然后单击“ 脚本编辑器”

  • It should open up a new window which looks something like this:

    它应该打开一个新窗口,看起来像这样:
  • Copy and paste this code. I’ll explain it after we do that.

    复制并粘贴此代码。 这样做之后,我会解释。
//this is a function that fires when the webapp receives a POST requestfunction doPost(e) {    //Return if null  if( e == undefined ) {    console.log("no data");    return HtmlService.createHtmlOutput("need data");   }    //Parse the JSON data  var event = JSON.parse(e.postData.contents);  var data = event.data;
//Get the last row without data  var sheet = SpreadsheetApp.getActiveSheet();  var lastRow = Math.max(sheet.getLastRow(),1);  sheet.insertRowAfter(lastRow);    //Get current timestamp  var timestamp = new Date();    //Insert the data into the sheet  sheet.getRange(lastRow + 1, 1).setValue(event.published_at);  sheet.getRange(lastRow + 1, 2).setValue(data.temperature);  sheet.getRange(lastRow + 1, 3).setValue(data.humidity);  sheet.getRange(lastRow + 1, 4).setValue(data.pm10);  sheet.getRange(lastRow + 1, 5).setValue(data.pm25);  sheet.getRange(lastRow + 1, 6).setValue(data.tvoc);  sheet.getRange(lastRow + 1, 7).setValue(data.c02);    SpreadsheetApp.flush();  return HtmlService.createHtmlOutput("post request received");}

Now, let’s understand everything.

现在,让我们了解所有内容。

function doPost(e) {

Is the function that gets called on a POST event. Consider this script as a web server. We’re sending it data at a specific address (that we’ll have in a hot minute)

是在POST事件上调用的函数。 将此脚本视为Web服务器。 我们正在将数据发送到一个特定的地址(我们将在一分钟之内收到)

e is the object of the HTTP call. It will have the data that we’re sending it. So it’s a good idea to check if it’s NULL. If it is, then there’s no need to run the script.

e是HTTP调用的对象。 它将包含我们正在发送的数据。 因此,最好检查它是否为NULL。 如果是这样,则无需运行脚本。

If we do have valid data, let’s change it from a string into useable JSON. You can use everyone’s favorite functionJSON.parse to do so.

如果我们确实有有效的数据,让我们将其从字符串更改为可用的JSON。 您可以使用每个人喜欢的功能JSON.parse来执行此操作。

var event = JSON.parse(e.postData.contents);

Remember, the structure of the data will determine how you process it! You may have to run JSON.parse several times depending on how nested your data is and what format it’s in.

请记住,数据的结构将决定您如何处理它! 您可能必须多次运行JSON.parse ,这取决于您的数据嵌套的方式和格式。

After you have your data, it’s time to put it in the right place!

拥有数据之后,就该将其放置在正确的位置了!

//Get the last row without datavar sheet = SpreadsheetApp.getActiveSheet();var lastRow = Math.max(sheet.getLastRow(),1);sheet.insertRowAfter(lastRow);

These three calls will get you to the first available row starting at row 1 (leaving row 0 for the column labels).

这三个调用将带您到第1行开始的第一个可用行(列标签保留第0行)。

Then, finally, we put the data in the row it belongs:

然后,最后,我们将数据放在它所属的行中:

sheet.getRange(lastRow + 1, 1).setValue(event.published_at);

Where the first parameter of sheet.getRange is the row and the second is the column. You can use the setValue function to set what you want in that particular cell.

其中sheet.getRange的第一个参数是行,第二个参数是列。 您可以使用setValue函数来设置所需的特定单元格。

By the way, the inspiration for this code came from this post.

顺便说一句,这段代码的灵感来自于这篇文章 。

Cool. So we have a script. How do we call it?

凉。 所以我们有一个脚本。 我们怎么称呼它?

  • Hit that Publish button

    点击那个发布按钮

  • Click Deploy as a web app

    点击Deploy as a web app

  • Change the settings to match the screenshot below. Then click Deploy

    更改设置以匹配下面的屏幕截图。 然后点击Deploy

  • You may get a screen asking you to update your permissions. Click Review Permissions

    您可能会看到一个屏幕,要求您更新权限。 单击Review Permissions

  • Click the Advanced and then click Go to <Your File Name> in the bottom left.

    单击Advanced ,然后单击左下方的Go to <Your File Na >。

  • Finally, click Allow

    最后,点击Allow

  • In the last screen, copy your Webhook URL!

    在最后一个屏幕中,复制您的Webhook URL!

测试一下 (Test it)

Now we can test if everything works by using Postman. If you haven’t played with Postman yet, it’s a great graphical user interface for curl.

现在,我们可以使用Postman测试一切是否正常。 如果您还没有玩过Postman,那将是curl的绝佳图形用户界面。

  • Install Postman. You may need an account to go further.

    安装邮递员。 您可能需要一个帐户才能继续使用。

  • Once inside, create a new request. Name it so you know it belongs to this Google Docs webhook

    进入内部后,创建一个新请求。 为其命名,以确保它属于此Google Docs Webhook
  • Click body and enter the following test code:

    单击body然后输入以下测试代码:

{    "event": "gdxg",    "data": {        "temperature": 21    },    "coreid": "zczxvz",    "published_at": "zcvzxcvx"}
  • Finally, click that blue Send button.

    最后,单击该蓝色的“ Send按钮。

  • Go back to your excel sheet and see the magic!

    返回您的Excel工作表并查看魔术!

Now we’re cooking with gas!

现在我们正在用煤气做饭!

结论 (Conclusion)

I hope you’ve gotten the above example to work. Luckily for you, there’s a lot less to worry about once you get this part up and running!

希望您能使用上面的示例。 幸运的是,一旦您完成了这部分的准备工作,就不必担心了!

To recap, we’ve talked about webhooks and why they’re used. You should be feeling confident at this point to go and set up some of your own. If you’re still feeling intimidated, I recommend using services like Zapier or IFTTT. (They’re shiny front ends for APIS and Webhooks already available.)

回顾一下,我们讨论了Webhook及其使用原因。 在这一点上,您应该有信心自己建立一些。 如果您仍然感到害怕,建议您使用Zapier或IFTTT之类的服务。 (它们是已经可用的APIS和Webhooks的闪亮前端。)

Last but not least check out the full post where I integrate hardware and web in one awesome project.

最后但并非最不重要的一点是, 查看完整的文章 ,其中我将硬件和Web集成到一个了不起的项目中。

Happy webhooking!

祝您网络愉快!

翻译自: https://www.freecodecamp.org/news/a-simple-how-to-on-webhooks-the-intimidation-stops-now-9671e8c94c76/

相关文章:

12.MySql关于获取当前时间的三个函数

这三个函数都是获取当前时间的&#xff0c;获取的详细格式如下图所示&#xff0c;可以根据需要来选用。 转载于:https://www.cnblogs.com/Nick-Hu/p/7566805.html

微信小程序云开发,使用阿里云短信服务,搜索员工生日定期发送短信。

相关API文档地址&#xff1a; 阿里云短信服务API文档地址 小程序云开发云函数正则匹配API文档地址 小程序云开发云函数定时触发器 1.登录阿里云&#xff0c;购买短信服务并添加签名和模板 2.&#xff0c; 登录阿里云&#xff0c;鼠标放在右上角的头像图标就会显示 AccessKey…

信息安全系统设计基础家庭作业

《深入理解计算机系统》家庭作业 * 8.9 答案&#xff1a; 进程对 是否并发 AB 否 AC 是 AD 是 BC 是 BD 是 CD 是 * 8.10 答案&#xff1a; A. 调用一次&#xff0c;返回两次&#xff1a; fork B. 调用一次&#xff0c;从不返回&#xff1a; execve, longjmp C. 调…

css游戏代码_介绍CSSBattle-第一个CSS代码搜寻游戏

css游戏代码by kushagra gour由kushagra gour 介绍CSSBattle-第一个CSS代码搜寻游戏 (Introducing CSSBattle — the first CSS code-golfing game) If you are learning Web development or are already a professional Web developer, there is a very high chance you have…

IOS手机全屏长按识别二维码HTML代码

代码段作用讲解&#xff1a; 1. 二维码的全屏样式, opacity: 0; 透明样式&#xff0c; touch-callout: none; -webkit-touch-callout: none; -ms-touch-callout: none; -moz-touch-callout: none; 禁止IOS默认长按事件 .codePage {position: absolute;touch-callout: none;…

[亲测]在Mac下配置php开发环境:Apache+php+MySql

公司给我们配上了高大上的Apple Mac Pro本本&#xff0c;这两天自己正在习惯中。通过虚拟机PD&#xff0c;确实解决了一些因为工作习惯无法在iOS上很好完成的事情&#xff0c;但是我想&#xff0c;既然用起了iOS就尽量将一些事务在iOS环境下处理&#xff0c;免得好似关羽耍着大…

RabbitMQ 异常与任务分发

RabbitMQ 异常与任务分发 异常情况处理 上篇最后提到了这个问题&#xff0c; consumer异常退出、queue出错、甚至rabbitMQ崩溃。因为它们都是软件 &#xff0c;软件都会有bug&#xff0c;这是无法避免的。所以RabbitMQ在设计的时候也想到了这一点 在之前&#xff0c;消息分发给…

reddit_如何使用Python创建自定义Reddit通知系统

redditby Kelsey Wang王凯西 如何使用Python创建自定义Reddit通知系统 (How to make a custom Reddit notification system with Python) Don’t you just love automated emails? I know I do. I mean, who doesn’t enjoy waking up to 236 new messages from Nike, Ticket…

1016. Phone Bills (25)

时间限制400 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者CHEN, Yue去掉非法数据计算账单A long-distance telephone company charges its customers by the following rules:Making a long-distance call costs a certain amount per minute, depending on the…

样式集(五)微信朋友圈样式模拟

效果图&#xff1a; 小图标&#xff1a; 源码&#xff1a; <!--pages/findList/findList.wxml--> <image class"xxiangji" catchtap"xxiangji" src"/images/xxiangji.png"></image> <image class"top_img" src&…

为什么要选择useState而不是useReducer

by Austin Malerba奥斯汀马勒巴(Austin Malerba) 为什么要选择useState而不是useReducer (Why you should choose useState instead of useReducer) 通过useState进行本地和全局状态管理的指南 (A guide to local and global state management via useState) Since the introd…

php 类中的变量的定义

php 如果在类中定义变量&#xff0c;在类的方法中调用时应该加上$this-> . class ClassName {private $a 333;function __construct(){$this->a 2222;}public function bbb($value){echo $this->a;} } $b new className(); echo $b->bbb();转载于:https://www.c…

微信小程序云数据库触底分页加载,下拉无限加载,第一次请求数据随机,随机获取数据库的数据

效果图 小程序云开发分页加载代码 <!--pages/chatList/chatList.wxml--> <view class"pageTitle">家博慧</view> <view class" search_arr"><icon class"searchcion" size16 typesearch></icon><input …

Linux(Centos)之安装Java JDK及注意事项

1.准备工作 a.因为Java JDK区分32位和64位系统&#xff0c;所以在安装之前必须先要判断以下我们的Centos系统为多少位系统&#xff0c;命令如下&#xff1a; uname -a解释&#xff1a;如果有x86_64就是64位的&#xff0c;没有就是32位的。后面是X686或X86_64则内核是64位的&…

2019web前端趋势_2019年最值得关注的Web开发趋势

2019web前端趋势by Mrudul Shah通过Mrudul Shah 2019年最值得关注的Web开发趋势 (Top Web Development trends to look out for in 2019) Do you know that nearly 200 websites are pushed out every minute? Sounds astonishing right? But it is a fact and that’s why …

WPF入门教程系列九——布局之DockPanel与ViewBox(四)

七. DockPanel DockPanel定义一个区域&#xff0c;在此区域中&#xff0c;您可以使子元素通过描点的形式排列&#xff0c;这些对象位于 Children 属性中。停靠面板其实就是在WinForm类似于Dock属性的元 素。DockPanel会对每个子元素进行排序&#xff0c;并停靠在面板的一侧&…

tabBar 自定义,小程序自定义底部导航栏

创建一个自定义组件 my_tab&#xff0c;组件代码在后面&#xff0c;先看调用自定义组件的代码&#xff0c;比如我需要在index 页面调用&#xff0c;就在index.json中引用组件&#xff0c;index.json 代码&#xff08;引用的路径为你创建的自定义组件路径&#xff09; {"n…

2015年最新出炉的JavaScript开发框架

前端框架简化了开发过程中&#xff0c;像 Bootstrap 和 Foundation 就是前端框架的佼佼者。在这篇文章了&#xff0c;我们编制了一组新鲜的&#xff0c;实用的&#xff0c;可以帮助您建立高质量的 Web 应用程序的 JavaScript 框架清单。 1.Aurelia Aurelia是下一代JavaScript客…

小程序前端性能测试_如何提高前端应用程序的性能

小程序前端性能测试If your website takes longer than 3 seconds to load, you could already be losing nearly half of your visitors.如果您的网站加载时间超过3秒&#xff0c;则可能已经失去了将近一半的访问者。 Yes this is a fact, proven by several research studie…

10-TypeScript中的接口

接口是一种规约的约定&#xff0c;从接口继承的类必须实现接口的约定。在高级开发中&#xff0c;通常接口是用于实现各种设计模式的基础&#xff0c;没有接口&#xff0c;设计模式无从谈起。 定义接口&#xff1a; interface ILog{recordlog():boolean; } 类从接口继承&#xf…

样式集(六)仿微信通讯录样式

效果图&#xff1a; 这里有引用到 自定义底部导航&#xff0c;自定义底部导航组件链接 <!--pages/chatList/chatList.wxml--><!-- <include src"/components/common/common" /> --> <view class"top"><view class"pageTi…

WCF动态添加ServiceKnownType

WCF中传输自定义类型时&#xff0c;必须在服务接口类&#xff08;服务协定&#xff09;上加上ServiceKnownType(typeof(yourClass)), 在实际应用中比较麻烦&#xff0c;可以用动态的办法来实现动态添加。 服务接口类&#xff0c;加上一行 [ServiceKnownType("GetKnownType…

博客 rss 如何使用_如何使用RSS从您的GatsbyJS博客自动交叉发布

博客 rss 如何使用With the recent exodus from Medium many developers are now creating their own GatsbyJS Blogs and then cross-posting to Medium or publications like freecodecamp.org and dev.to.随着Medium最近的离职&#xff0c;许多开发人员现在正在创建自己的Ga…

大型技术网站的技术( 高并发、大数据、高可用、分布式....)(一)

面对高并发、大流量、高可用、海量数据、用户分布广泛、网络情况复杂这类网站系统我们如何应对&#xff1f;&#xff1f;&#xff1f; 第一阶段 一台服务器不行就上多台服务器 1.应用程序与数据服务分离 将应用程序、数据库、文件等资源放在一台服务器上&#xff0c;面对海量…

BestCoder Round #65 B C D || HDU 5591 5592 5593

B 题意&#xff1a;ZYB在远足中,和同学们玩了一个“数字炸弹”游戏&#xff1a;由主持人心里想一个在[1,N][1,N]中的数字XX&#xff0c;然后玩家们轮流猜一个数字&#xff0c;如果一个玩家恰好猜中XX则算负&#xff0c;否则主持人将告诉全场的人当前的数和XX比是偏大还是偏小&a…

数组去重,ES6数组去重 new Set()

普通数组去重 var b [...new Set([1,2, 3, 4, 5, 5, 5, 5])]console.log(b); 输出结果&#xff1a; 包含对象的数组去重 var o {a:1}var b [...new Set([o, o, 3, 4, 5, 5, 5, 5])]console.log(b); 输出结果&#xff1a; 包含对象的数组去重有一个坑 var b [...new Set([{…

使用angular的好处_在项目中使用Angular的最大好处

使用angular的好处by Irina Sidorenko伊琳娜西多连科(Irina Sidorenko) 在项目中使用Angular的最大好处 (The top benefits of using Angular for your project) 在项目实施中使用Angular的11个理由及其好处 (11 reasons to use Angular and its benefits for your project im…

python之路——模块和包

一、模块 1、什么是模块&#xff1f; 常见的场景&#xff1a;一个模块就是一个包含了Python定义和声明的文件&#xff0c;文件名就是模块名字加上.py的后缀。 但其实import加载的模块分为四个通用类别&#xff1a; 1、使用Python编写的代码&#xff08;.py文件&#xff09; 2、…

夺命雷公狗---linux NO:3 centos_mini版的安装和备份

废话不多说&#xff0c;和前面的其实是差不多的&#xff0c;如下图所示&#xff1a; 安装其实是和桌面版的差不多的&#xff0c;但是经典版的不能自定义分区&#xff08;如详细区&#xff0c;如home之类的&#xff09;。。。 因为我们使用的是命令行方式的所以直接选英文&#…

快速学习 async await 的使用, Demo 解析

async 和 await 字面都很好理解&#xff0c;分别是异步和等待。 来两个简单的 demo&#xff0c; demo1 tt2(){return new Promise(rps>{setTimeout(() > {rps(true)}, 1500);})},async tt1(){var a await this.tt2();console.log(a)},/*** 生命周期函数--监听页面加载*…