繁体   English   中英

在 AWS 上设置 webhook - node.js APP

[英]Setting up a webhook on AWS - node.js APP

我设置了一个 node.js 应用程序并将其部署到弹性 beanstalk 上。 我需要创建 webhook,以便应用程序可以收听 typeform 调查。 做这个的最好方式是什么? 我应该使用 lambda 吗? 有没有办法在弹性豆茎上做到这一点?

Elastic beanstalk 支持许多不同的编程语言,例如 Node.js、Rails 或 PHP。

在 Node.js 中,您可能可以依靠Express框架创建一个简单的 HTTP 路由并监听 webhook 事件。

app.post('/typeform/webhook', bodyParser.raw({ type: 'application/json' }),(request, response) => {
  
  // security check, let's make sure request comes from typeform
  const signature = request.headers['typeform-signature']
  const isValid = verifySignature(signature, request.body.toString())

  if (!isValid) {
    throw new Error('Webhook signature is not valid, someone is faking this!');  
  }

  // send 200 status back, and notifies typeform 👌
  response.sendStatus(200)

  const { event_type, form_response } = JSON.parse(request.body);

  // filter response events only
  if (event_type === 'form_response') {
    // extract answers for example get email from ref
    const email_addr = form_response.answers.find(a => a.field.type === 'email').email
  }
});

verifySignature function 是可选的,但增加了安全性,因为它验证接收到的有效负载是否来自 Typeform。


// function to verify request signature comes from Typeform
const verifySignature = function(receivedSignature, payload){
  const hash = crypto
    .createHmac('sha256', process.env.TYPEFORM_WEBHOOK_SECRET)
    .update(payload)
    .digest('base64')
  return receivedSignature === `sha256=${hash}`
}

完成此快速应用程序设置后,您可以将其部署到 Elastic beanstalk 中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM