Pivotal TrackerとSlackを連携する

# 追記
- [コメント](https://qiita.com/yuki777/items/e43e1ad37ba2867f775e/#comment-b1419dafcde212068629)頂いた通り、 [Pivotal Tracker Notifications (Legacy)](https://slack.com/apps/A0F82E7H8-pivotal-tracker-notifications-legacy)  で連携できます。Thanks @making@github 

# 説明
- Pivotal Trackerでの更新をSlackで受け取りたいがIntegrationが見つからない :(
- https://www.pivotaltracker.com/integrations/slack はあったけど、これはFrom:Slack => To:Pivotal Trackerというかんじ。
- とにかく連携する方法が見つからないので書いてみた
- Pivotal Tracker側では `Activity Web Hook` をAddする
<img width="666" alt="スクリーンショット 2019-05-21 1.10.26.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/8352/106157ce-68f0-ed3f-5281-cd8a466bd22f.png">
- Slack側では `Incoming WebHooks` を設定する
<img width="592" alt="スクリーンショット 2019-05-21 1.12.49.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/8352/f4677055-0115-ea2d-4e7e-ef048fae7f8c.png">
- あとはコードをインストール、設定すると、Pivotal Tracker側の更新がSlackに通知される :)
<img width="476" alt="スクリーンショット 2019-05-21 1.18.19.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/8352/d2d81aea-bf9f-906c-f2f2-64473d7e625e.png">


# コード
https://github.com/yuki777/pivotaltracker-slack

```php

...

    public function run()
    {
        $activity = json_decode(file_get_contents("php://input"), true);

        if(! $this->validate($activity)) return false;

        $this->sendSlack($this->getSummary($activity));
    }

    private function validate($activity)
    {
        if(! isset($activity['kind'])) return false;
        if( ($activity['kind'] != 'story_create_activity')
        and ($activity['kind'] != 'story_update_activity')
        and ($activity['kind'] != 'task_create_activity')
        and ($activity['kind'] != 'task_update_activity')
        and ($activity['kind'] != 'task_delete_activity')
        and ($activity['kind'] != 'comment_create_activity')
        ) return false;

        if(! isset($activity['message'])) return false;
        if(! isset($activity['highlight'])) return false;

        if(! isset($activity['primary_resources'][0]['name'])) return false;
        if(! isset($activity['primary_resources'][0]['story_type'])) return false;
        if(! isset($activity['primary_resources'][0]['url'])) return false;

        if(! isset($activity['performed_by']['name'])) return false;

        return true;
    }

    private function getSummary($activity)
    {
        $summary = [
            'user'       => $activity['performed_by']['name'],
            'message'    => $activity['message'],
            'highlight'  => $activity['highlight'],
            'story_name' => $activity['primary_resources'][0]['name'],
            'story_type' => $activity['primary_resources'][0]['story_type'],
            'story_url'  => $activity['primary_resources'][0]['url'],
        ];

        if(isset($activity['changes'][0]['new_values']['description'])){
            $summary['description'] = $activity['changes'][0]['new_values']['description'];
        }

        return $summary;
    }

    private function sendSlack($summary)
    {
        $settings = ['link_names' => true];
        $client = new Nexy\Slack\Client($_ENV['SLACK_WEBHOOK_URL'], $settings);
        $fields = [
            new \Nexy\Slack\AttachmentField('User', $summary['user'], true),
            new \Nexy\Slack\AttachmentField('Highlight', $summary['highlight'], true),
            new \Nexy\Slack\AttachmentField('Story Name', $summary['story_name']),
            new \Nexy\Slack\AttachmentField('Message', $summary['message']),
        ];

        if(isset($summary['description'])){
            $fields[] = new \Nexy\Slack\AttachmentField('Description', $summary['description']);
        }

        return $client->attach((new \Nexy\Slack\Attachment())->setFields($fields))->send($summary['story_url']);
    }

...
```