INTRODUCTION
The Account Activity API provides you the ability to subscribe to realtime activities related to a user account via webhooks. This means that you can receive realtime Tweets, Direct Messages, and other account events from one or more of your owned or subscribed accounts through a single connection.
Supported Activity types
- Tweets
- @mentions
- Replies
- Retweets
- Quote Tweets
- Retweets of Quoted Tweets
- Likes
- Subscription Revokes
- Follows
- Blocks
- Mutes
- Send Direct Messages
- Receive Direct Messages
- Typing indicators
- Read receipts
- Tweet deletes
The Account Activity API is a webhook-based API that sends account events to a web app you develop, deploy and host. Check here more
Requirements
- Twitteroauth Package
- Domain with SSL support
- Twitter App
Setup
- Create a Twitter App
- Configure your Callback URL, App keys in config.php
//config.php
define('CONSUMER_KEY', 'YOUR CONSUMER KEY');
define('CONSUMER_SECRET', 'YOUR CONSUMER SECRET');
define('OAUTH_CALLBACK', 'YOUR CALLBACK URL');
Securing Your webhook
Below code should be implemented in your webhook file. During Webhook registration Twitter will send a crc_token parameter. All events where triggered to this file after the webhook registration. Please go through securing-webhooks
// webhook.php
include "../config.php";
if(isset($_REQUEST['crc_token'])) {
$signature = hash_hmac('sha256', $_REQUEST['crc_token'], CONSUMER_SECRET, true);
$response['response_token'] = 'sha256='.base64_encode($signature);
print json_encode($response);
} else {
// Here You Recieve Realtime Webhook Events.
}
1. Register Webhook
/* Create a TwitterOauth object with consumer/user tokens. */
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
$url = "Your Webhook URL";
$content = $connection->post("account_activity/all/your-env_name/webhooks", ["url" => $url]);
Response
{
"id": "1234567890",
"url": "https://your_domain.com/webhook/twitter",
"valid": true,
"created_at": "2016-06-02T23:54:02Z"
}
2. Subscribe User to Webhook
/* Create a TwitterOauth object with consumer/user tokens. */
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
$content = $connection->post("account_activity/all/your-env_name/subscriptions");
// Subscribes user to a webhook. Returns 200 status.
3. Update Your Webhook File
// webhook.php
include "../config.php";
if(isset($_REQUEST['crc_token'])) {
$signature = hash_hmac('sha256', $_REQUEST['crc_token'], CONSUMER_SECRET, true);
$response['response_token'] = 'sha256='.base64_encode($signature);
print json_encode($response);
} else {
// Here You Recieve Realtime Webhook Events
// Writing the recieved events to file
$eventJSON = file_get_contents('php://input');
$myfile = fopen("events.txt", "w") or die("Unable to open file!");
fwrite($myfile, $eventJSON . PHP_EOL);
fclose($myfile);
}
After User subscription. Log into Twitter (use the subscribed Twitter Account), Then trigger an event Eg: favorite a Tweet, send a direct message, retweet a post. Then check the directory you can see a text file "events.txt" with event payloads
4. Checking Webhook Status using Bearer Token
// appcall.php
/* Build TwitterOAuth object with client credentials. */
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
$request_token = $connection->oauth2('oauth2/token', ['grant_type' => 'client_credentials']);
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $request_token->access_token);
$content = $connection->get("account_activity/all/your:envName/subscriptions/list");
print_r($content);
Returns a list of the current All Activity type subscriptions. Note that the /list endpoint requires application-only OAuth, so requests should be made using a bearer token instead of user context.
5. Reference
API call's using Bearer Token. Call these endpoints from appcall.php .
-
GET account_activity/all/webhooks
Returns all environments, webhook URLs and their statuses for the authenticating app. Currently, only one webhook URL can be registered to each environment.
-
GET account_activity/all/:env_name/subscriptions/list
Returns a list of the current All Activity type subscriptions. Note that the /list endpoint requires application-only OAuth, so requests should be made using a bearer token instead of user context.
-
GET account_activity/all/subscriptions/count
Returns the count of subscriptions that are currently active on your account for all activities. Note that the /count endpoint requires application-only OAuth, so that you should make requests using a bearer token instead of user context.
Check all supported endpoints here .