This repository has been archived by the owner on Feb 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpush-member-data.php
66 lines (54 loc) · 1.99 KB
/
push-member-data.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
require __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/settings.php';
/**
* Returns an authorized API client.
* @return Google_Client the authorized client object
*/
function getClient()
{
putenv('GOOGLE_APPLICATION_CREDENTIALS=' . __DIR__ . '/google_credentials.json');
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setScopes(Google_Service_Sheets::SPREADSHEETS);
return $client;
}
function publicSectorEmail ( $email ) {
/* Only do this for:
- *.gov.uk
- *.police.uk
- *.nhs.uk
- (*.)nhs.net
- (*.)gov.je
- (*.)gov.scot
- (*.)gov.wales
*/
return preg_match( '/^[\w\'\.+\-]+@(?>[\w\.\-]+\.(?>gov|police|nhs)\.uk|(?:[\w\.\-]+\.)?(?>nhs.net|gov.je|gov.scot|gov.wales))$/i', $email );
}
function inviteToSlack( $email )
{
try {
// If there's no Slack token, don't bother
if ( empty( SLACK_TOKEN ) )
return false;
if ( !publicSectorEmail( $email ) )
return false;
$client = new GuzzleHttp\Client([ 'base_uri' => 'https://localgovdigital.slack.com' ]);
// Based on guidance in https://github.com/ErikKalkoken/slackApiDoc/blob/master/users.admin.invite.md
// Invite email address and resend if the invite email was sent a while ago
$response = $client->request('POST', '/api/users.admin.invite', [
'form_params' => [
'email' => $email,
'token' => SLACK_TOKEN,
'set_active' => true,
'resend' => true
]
]);
$body = json_decode( $response->getBody() );
// If OK or if already invited etc, return true
return ($body->{'ok'} || ( isset( $body->{'error'} ) && in_array( $body->{'error'}, array('already_invited', 'already_in_team', 'sent_recently', 'user_disabled') ) ) );
} catch (Exception $e) {
// Any sort of error, return false for further investigation
return false;
}
}