Envoy(受付アプリ)のSlack通知が遅いので自作

PHP

概要

  • Envoyというオフィスのエントランスで受付をするシステムがあります。
  • 理想のワークフローは、Envoyで訪問者が受付をした際に、担当者宛にSlackで通知を飛ばすこと。
  • Envoyが提供しているSlackのIntegrationを使うと、通知が届くまで4〜5分かかるのでかなり問題。(どうやら、2回めの通知は早い。SlackのIntegrationの初回呼び出しが遅い感じ。)
  • Envoyからのwebhookは即時で届くので、SlackのIntegrationを使うのではなく自作したプログラムを使います。

設計

通信のフローは以下のようになります。

Envoy => (webhook) => なんかしらのScript => (webhook) => Slack

Scriptの部分を作って設置します。

スクリプトは以下のような感じです。EnvoyからはPOSTでいろいろなデータが送られてくるので、必要なデータを抽出してSlackへ通知します。

SlackのWebhookのURLはパラメータで受け取れるようにしておきます。

<?php
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
print 'This script should be requested by POST method.';
exit;
}
$LOG_FILE = dirname(__FILE__).'/hook.txt';
$body = file_get_contents('php://input');
if (!$body) { throw new Exception('input is not found'); }
$payload = json_decode($body, true);
# log
file_put_contents($LOG_FILE, date("[Y-m-d H:i:s]")." ".print_r($payload,1)."\n", FILE_APPEND|LOCK_EX);
$url = $_GET['slack_webhook_url'];
$rows = array();
foreach ($payload['payload']['attributes']['user-data'] as $array) {
$rows[] = (array('title' => $array['field'], 'value' => $array['value']));
}
$rows[] = (array('title' => '訪問時間', 'value' => $payload['payload']['attributes']['signed-in-at']));
$message = [
'username' => 'neo-envoy',
'text' => sprintf('%s 様が%s %s の受付でお待ちです。',
$payload['payload']['attributes']['full-name'],
$payload['meta']['company']['attributes']['name'],
$payload['meta']['location']['attributes']['name']
),
'attachments' => [
[
'text' => $payload['payload']['attributes']['full-name'],
'color' => 'good',
'fields' => $rows
],
]
];
if ($payload['payload']['attributes']['host']) {
$message['attachments'][] =
[
'text' => '訪問先',
'color' => 'warning',
'fields' => [
[
'title' => $payload['payload']['attributes']['host'],
'value' => $payload['payload']['attributes']['host-email'],
],
]
];
}
$ch = curl_init();
$options = [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query([
'payload' => json_encode($message)
])
];
curl_setopt_array($ch, $options);
curl_exec($ch);
curl_close($ch);
view raw slack.php hosted with ❤ by GitHub

設定

予め、Slack側で通知を送りたいチャネルでwebhookのURLを発行しておきます。

Envoyに管理者でログインして、上記のスクリプトのURLを登録します。

Envoy

設定するWebhookのURLには忘れずにurlパラメータでslackの通知先webhookを入力します。一応、urlencodeした値を入れておきます。

URL例

https://example.com/slack.php?url=https%3A%2F%2Fhooks.slack.com%2Fservices%2FT0000XXXX%2Fxxxxxxxxxxx%2Fxxxxxxxxxxxxxxxxxxxxxxxxx

通知のサンプル

まとめ

  • ほぼ即時に通知が来るようになりました。
  • webhookを動かしているPHPのアプリケーションサーバが落ちると通知が来なくて辛い状態になります。
  • mentionさせるためには、SlackとEnvoy間でユーザの紐付けをしないといけないので今回はやってません。

参考資料

EnvoyからWebhookで送られてくるサンプルデータはこちらです。

Array
(
    [event] => entry_sign_in
    [meta] => Array
        (
            [location] => Array
                (
                    [id] => xxxx
                    [type] => locations
                    [attributes] => Array
                        (
                            [name] => xx
                            [company-name-override] =>
                            [time-zone] => Asia/Tokyo
                            [timezone] => Asia/Tokyo
                            [locale] => ja
                            [address] => 〒xxx-xxxx xxxxxxxxxxx
                            [address-line-one] => undefined
                            [address-line-two] => 1F
                            [city] => xxxx
                            [state] => xxxx
                            [country] => xx
                            [zip] => xxx-xxxx
                            [longitude] => xxxx.xxxxxx
                            [latitude] => xx.xxxxxxxxx
                            [created-at] => 20xx-04-11T08:18:24.829Z
                        )

                    [relationships] => Array
                        (
                            [company] => Array
                                (
                                    [data] => Array
                                        (
                                            [id] => xxxx
                                            [type] => companies
                                        )

                                )

                        )

                )

            [company] => Array
                (
                    [id] => xxx
                    [type] => companies
                    [attributes] => Array
                        (
                            [name] => xxxxxxxxx
                            [plan-intent] => basic
                            [buy-intent] =>
                            [active] => 1
                            [created-at] => 20xx-04-11T08:18:24.795Z
                        )

                )

        )
    [payload] => Array
        (
            [id] => xxxxxxxxxxxxxxx
            [type] => entries
            [attributes] => Array
                (
                    [agreements-status] => not_applicable
                    [full-name] => xxxxxx
                    [email] =>
                    [employee-screening-flow] =>
                    [host] => @here
                    [host-email] => [email protected]
                    [private-notes] =>
                    [signed-in-at] => 20xx-03-24 02:21:53 UTC
                    [device-session-uuid] =>
                    [signed-in-via] => xxxxx
                    [signed-in-by] =>
                    [signed-out-via] => xxxxxx
                    [signed-out-by] =>
                    [signed-out-at] => 20xx-03-24 02:21:53 UTC
                    [thumbnails] => Array
                        (
                            [large] =>
                            [original] =>
                            [small] =>
                        )

                    [flow-name] => Visitor
                    [original-nda-sign-date] =>
                    [group-name] =>
                    [id-check-status] =>
                    [is-delivery] => 1
                    [agreement-refused] =>
                    [pov-key] => Purpose of visit
                    [user-data] => Array
                        (
                            [0] => Array
                                (
                                    [field] => Host
                                    [value] => @here
                                )

                        )

                    [sms-status] => not_sent
                    [approval-status] =>
                    [push-status] => not_sent
                    [email-status] => not_sent
                )
            [relationships] => Array
                (
                    [location] => Array
                        (
                            [data] => Array
                                (
                                    [type] => locations
                                    [id] => xxxxx
                                )

                        )

                    [device] => Array
                        (
                            [data] => Array
                                (
                                    [type] => devices
                                    [id] => xxxxx
                                )

                        )

                    [employee] => Array
                        (
                            [data] => Array
                                (
                                    [type] => employees
                                    [id] => xxxxxx
                                )

                        )

                    [flow] => Array
                        (
                            [data] => Array
                                (
                                    [type] => flows
                                    [id] => xxxxxx
                                )

                        )

                    [previous-entries] => Array
                        (
                            [links] => Array
                                (
                                    [related] => https://app.envoy.com/api/v3/entries/xxxxxxxxx/previous-entries
                                )

                        )

                    [platform-jobs] => Array
                        (
                            [links] => Array
                                (
                                    [related] => https://app.envoy.com/api/v3/entries/xxxxxxxx/platform-jobs
                                )

                        )

                )

        )

)

コメント