Unlocking Twitter Data: A Developer's Guide to Cognitive Actions

Integrating with social media platforms has become essential for developers looking to enhance their applications with rich data. The Twitter Cognitive Actions provide a powerful set of tools to interact with Twitter's API, enabling you to fetch follower data, resolve URLs, retrieve tweets, and much more. These pre-built actions simplify the process of accessing and managing Twitter data, making it easier for developers to build engaging applications.
Prerequisites
Before you can start using the Twitter Cognitive Actions, ensure you have the following:
- An API key for the Cognitive Actions platform.
- Basic knowledge of how to make HTTP requests and handle JSON data.
- Familiarity with Python programming for conceptual examples.
Authentication typically involves passing your API key in the headers of your requests.
Cognitive Actions Overview
Retrieve Follower IDs
This action allows you to fetch up to 5000 follower IDs per request using the Twitter API, providing quick access to user follower data.
Input:
- Required Fields:
userId: The unique identifier for the user (e.g.,44196397).
- Optional Fields:
count: Number of items to retrieve per request (default is500).cursor: String used for pagination.username: Username associated with the account (default iselonmusk).
Example Input JSON:
{
"count": 500,
"userId": "44196397",
"username": "elonmusk"
}
Output: Returns a list of follower IDs along with pagination information.
Example Output JSON:
{
"ids": [1426186035814273000, 1910180286253367300, ...],
"next_cursor": 1828986467172608000,
"total_count": null,
"previous_cursor": 0
}
Conceptual Usage Example (Python):
import requests
import json
COGNITIVE_ACTIONS_API_KEY = "YOUR_COGNITIVE_ACTIONS_API_KEY"
Cognitive_Actions_Execute_URL = "https://api.cognitiveactions.com/actions/execute"
action_id = "0b2c0a70-f928-43ca-9cbe-37fefe1e26fd"
payload = {
"count": 500,
"userId": "44196397",
"username": "elonmusk"
}
headers = {
"Authorization": f"Bearer {COGNITIVE_ACTIONS_API_KEY}",
"Content-Type": "application/json"
}
response = requests.post(
Cognitive_Actions_Execute_URL,
headers=headers,
json={"action_id": action_id, "inputs": payload}
)
Resolve Shortened URLs
This action enables you to resolve and expand up to 100 t.co shortened URLs to their original forms, an essential utility for web scraping and understanding links shared on Twitter.
Input:
- Required Fields:
urlList: A comma-separated list of URLs to be processed.
Example Input JSON:
{
"urlList": "https://t.co/aNux1s4HGP,https://t.co/6e7LFNBv"
}
Output: Returns the resolved URLs.
Example Output JSON:
{
"resolvedUrls": [
"https://feastables.com/",
"https://help.x.com/articles/20170002"
]
}
Conceptual Usage Example (Python):
action_id = "3698ff11-496f-483a-b063-7f77f28533c5"
payload = {
"urlList": "https://t.co/aNux1s4HGP,https://t.co/6e7LFNBv"
}
response = requests.post(
Cognitive_Actions_Execute_URL,
headers=headers,
json={"action_id": action_id, "inputs": payload}
)
Fetch Multiple User Tweets
This action allows you to retrieve up to 100 tweets for each of up to 15 Twitter users simultaneously using their unique identifiers.
Input:
- Required Fields:
userIdentifiers: A comma-separated string of unique user identifiers.
- Optional Fields:
count: The number of items to be retrieved (default is20).
Example Input JSON:
{
"count": 20,
"userIdentifiers": "13298072,12"
}
Output: Returns tweets or an error if the endpoint is disabled.
Example Output JSON:
{
"error": "This endpoint is disabled for now."
}
Conceptual Usage Example (Python):
action_id = "878e6b8e-3e3a-43e9-a311-69457c0b9758"
payload = {
"count": 20,
"userIdentifiers": "13298072,12"
}
response = requests.post(
Cognitive_Actions_Execute_URL,
headers=headers,
json={"action_id": action_id, "inputs": payload}
)
Retrieve Twitter User Details by IDs
Access detailed profiles of Twitter users by providing a comma-separated string of user IDs to retrieve multiple user profiles with one request.
Input:
- Required Fields:
identifierList: A comma-separated string of unique identifiers.
Example Input JSON:
{
"identifierList": "44196397,155659213"
}
Output: Returns user details for the specified IDs.
Example Output JSON:
{
"users": [
{
"result": {
"id": "VXNlcjo0NDE5NjM5Nw==",
"legacy": {
"name": "Elon Musk",
"screen_name": "elonmusk",
...
}
}
},
...
]
}
Conceptual Usage Example (Python):
action_id = "9e31c847-2815-4f0c-b85e-0dd5fa514383"
payload = {
"identifierList": "44196397,155659213"
}
response = requests.post(
Cognitive_Actions_Execute_URL,
headers=headers,
json={"action_id": action_id, "inputs": payload}
)
Convert Usernames to User IDs
This action retrieves user IDs from a list of Twitter usernames, providing an easy way to access user data based on usernames.
Input:
- Required Fields:
userNames: A comma-separated list of usernames to query.
Example Input JSON:
{
"userNames": "elonmusk,spacex,tesla"
}
Output: Returns the corresponding user IDs.
Example Output JSON:
{
"ids": [
44196397,
34743251,
13298072
]
}
Conceptual Usage Example (Python):
action_id = "abf4f7e7-37ec-4447-b697-efd62c55c78c"
payload = {
"userNames": "elonmusk,spacex,tesla"
}
response = requests.post(
Cognitive_Actions_Execute_URL,
headers=headers,
json={"action_id": action_id, "inputs": payload}
)
Fetch Tweet Information
Retrieve detailed information about a specific tweet using its unique tweet ID, simplifying data extraction.
Input:
- Required Fields:
tweetId: A unique identifier for the tweet.
Example Input JSON:
{
"tweetId": "1631781099415257088"
}
Output: Returns detailed information about the specified tweet.
Example Output JSON:
{
"data": {
"tweetResult": {
"result": {
"core": {
"user_results": {
"result": {
"name": "MrBeast",
...
}
}
}
}
}
}
}
Conceptual Usage Example (Python):
action_id = "f7023e7b-eeef-4812-8209-b0d3ae53e811"
payload = {
"tweetId": "1631781099415257088"
}
response = requests.post(
Cognitive_Actions_Execute_URL,
headers=headers,
json={"action_id": action_id, "inputs": payload}
)
Conclusion
The Twitter Cognitive Actions provide a robust way to interact with Twitter's vast data ecosystem. By leveraging these actions, developers can build applications that enrich user experiences, streamline data retrieval, and enhance functionality. Whether you're fetching follower IDs, resolving URLs, or retrieving tweets, these actions simplify the integration process and open up new possibilities for your applications. Explore the capabilities of these actions and start building innovative solutions today!