-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter.php
35 lines (28 loc) · 1.18 KB
/
twitter.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
<?php
require_once 'config.php';
use TPGwidget\Bot\Controllers\TwitterController;
use TPGwidget\Bot\Models\Twitter;
// Required Challenge Response Check
// @link https://dev.twitter.com/webhooks/securing
if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['crc_token'])) {
echo json_encode(Twitter::getChallengeResponse($_GET['crc_token']));
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$requestBody = file_get_contents('php://input');
// Verify if the request comes from Twitter (see https://dev.twitter.com/webhooks/securing)
$signature = $_SERVER['HTTP_X_TWITTER_WEBHOOKS_SIGNATURE'];
if (! Twitter::verifySignature($signature, $requestBody)) {
header('HTTP/1.1 403 Forbidden');
die('Invalid signature');
}
$requestBody = json_decode($requestBody);
if (!is_array($requestBody->direct_message_events)) {
die();
}
foreach ($requestBody->direct_message_events as $event) {
// Only the new message that weren’t sent by the bot account
if (($event->type === 'message_create') && ($event->message_create->sender_id !== getenv('TWITTER_ACCOUNT_ID'))) {
TwitterController::newMessage($event);
}
}
}