📮

SendGridのEvent Webhookをlocaltunnelで受信する

2022/01/04に公開

最近、トンネリングツールとしてngrokやserveoの代わりにlocaltunnelを使っています。

npmでインストールします。

npm install -g localtunnel

グローバルインストールするとCLIツールが使えてltコマンドでサクッとトンネリングできます。

lt --port 3000

応答でパブリックなURLが返ります。

your url is: https://example.com

あとはNode.jsでローカルにサーバを立てて

app.js
const express = require('express');
const bodyParser = require('body-parser');
const app = express();

app.use(bodyParser.json());
app.post('/', (req, res) => {
    console.log(req.body);
    res.status(200);
})

app.listen(3000);

起動します。

node app.js

localtunnelで発行されたURLをSendGridのEvent Webhookにセットします。

Test Your Integrationボタンを押すとEventを受信できます。お手軽です。

[
  {
    email: 'example@test.com',
    timestamp: 1641303018,
    'smtp-id': '<14c5d75ce93.dfd.64b469@ismtpd-555>',
    event: 'processed',
    category: [ 'cat facts' ],
    sg_event_id: '2_hDLXCc1vG2g6Ojo0S2vQ==',
    sg_message_id: '14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0'
  },
  {
    email: 'example@test.com',
    timestamp: 1641303018,
    'smtp-id': '<14c5d75ce93.dfd.64b469@ismtpd-555>',
    event: 'deferred',
    category: [ 'cat facts' ],
    sg_event_id: 'xXTkrGISlT9cJcJqUKN5iA==',
    sg_message_id: '14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0',
    response: '400 try again later',
    attempt: '5'
  },
  {
    email: 'example@test.com',
    timestamp: 1641303018,
    'smtp-id': '<14c5d75ce93.dfd.64b469@ismtpd-555>',
    event: 'delivered',
    category: [ 'cat facts' ],
    sg_event_id: 'pLAb_HydwnxVZGHLRPHgYg==',
    sg_message_id: '14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0',
    response: '250 OK'
  },
  {
    email: 'example@test.com',
    timestamp: 1641303018,
    'smtp-id': '<14c5d75ce93.dfd.64b469@ismtpd-555>',
    event: 'open',
    category: [ 'cat facts' ],
    sg_event_id: 'Zk6-0zuh6Jg5-VKVihMk3A==',
    sg_message_id: '14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0',
    useragent: 'Mozilla/4.0 (compatible; MSIE 6.1; Windows XP; .NET CLR 1.1.4322; .NET CLR 2.0.50727)',
    ip: '255.255.255.255'
  },
  {
    email: 'example@test.com',
    timestamp: 1641303018,
    'smtp-id': '<14c5d75ce93.dfd.64b469@ismtpd-555>',
    event: 'click',
    category: [ 'cat facts' ],
    sg_event_id: '2DhV4KqiPpA8PkrJ14cnMg==',
    sg_message_id: '14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0',
    useragent: 'Mozilla/4.0 (compatible; MSIE 6.1; Windows XP; .NET CLR 1.1.4322; .NET CLR 2.0.50727)',
    ip: '255.255.255.255',
    url: 'http://www.sendgrid.com/'
  },
  {
    email: 'example@test.com',
    timestamp: 1641303018,
    'smtp-id': '<14c5d75ce93.dfd.64b469@ismtpd-555>',
    event: 'bounce',
    category: [ 'cat facts' ],
    sg_event_id: 'tSoVGJwNV1infI2Z0rFEAw==',
    sg_message_id: '14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0',
    reason: '500 unknown recipient',
    status: '5.0.0'
  },
  {
    email: 'example@test.com',
    timestamp: 1641303018,
    'smtp-id': '<14c5d75ce93.dfd.64b469@ismtpd-555>',
    event: 'dropped',
    category: [ 'cat facts' ],
    sg_event_id: 'ZBVx-_CV-k6YMf0ioqjdrA==',
    sg_message_id: '14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0',
    reason: 'Bounced Address',
    status: '5.0.0'
  },
  {
    email: 'example@test.com',
    timestamp: 1641303018,
    'smtp-id': '<14c5d75ce93.dfd.64b469@ismtpd-555>',
    event: 'spamreport',
    category: [ 'cat facts' ],
    sg_event_id: 'Wu6pYWSfb7rAN6y3kezvpQ==',
    sg_message_id: '14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0'
  },
  {
    email: 'example@test.com',
    timestamp: 1641303018,
    'smtp-id': '<14c5d75ce93.dfd.64b469@ismtpd-555>',
    event: 'unsubscribe',
    category: [ 'cat facts' ],
    sg_event_id: 'y7g_R2mkxSGuWbKzFq5tLA==',
    sg_message_id: '14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0'
  },
  {
    email: 'example@test.com',
    timestamp: 1641303018,
    'smtp-id': '<14c5d75ce93.dfd.64b469@ismtpd-555>',
    event: 'group_unsubscribe',
    category: [ 'cat facts' ],
    sg_event_id: 'Ez9Xdtn_JeFvHoK-RwETjw==',
    sg_message_id: '14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0',
    useragent: 'Mozilla/4.0 (compatible; MSIE 6.1; Windows XP; .NET CLR 1.1.4322; .NET CLR 2.0.50727)',
    ip: '255.255.255.255',
    url: 'http://www.sendgrid.com/',
    asm_group_id: 10
  },
  {
    email: 'example@test.com',
    timestamp: 1641303018,
    'smtp-id': '<14c5d75ce93.dfd.64b469@ismtpd-555>',
    event: 'group_resubscribe',
    category: [ 'cat facts' ],
    sg_event_id: '0Ja0jmih63uucZ3W3XrWGw==',
    sg_message_id: '14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0',
    useragent: 'Mozilla/4.0 (compatible; MSIE 6.1; Windows XP; .NET CLR 1.1.4322; .NET CLR 2.0.50727)',
    ip: '255.255.255.255',
    url: 'http://www.sendgrid.com/',
    asm_group_id: 10
  }
]

Discussion