In this section, we present how to connect Twitter accounts to MindsDB.

Twitter is a widely recognized social media platform and microblogging service that allows users to share short messages called tweets.

The Twitter handler enables you to fetch tweets and create replies utilizing AI models wthin MindsDB. Furthermore, you can automate the process of fetching tweets, preparing replies, and sending replies to Twitter.

Prerequisites

Before proceeding, ensure the following prerequisites are met:

  1. Install MindsDB locally via Docker or use MindsDB Cloud.
  2. To connect Twitter to MindsDB, install the required dependencies following this instruction.
  3. Install or ensure access to Twitter.

Connection

To connect a Twitter account to MindsDB, you need a Twitter developer account.

Please note that it requires a paid developer account.

We recommend you use the Elevated access allowing you to pull 2m tweets and to avoid parameters or authentication issue error you might get sometimes. You can check this step-by-step guide describing how to apply for the Elevated access.

If you don’t already have a Twitter developer account, follow the steps in the video below to apply for one.

If you already have a Twitter developer account, you need to generate API keys following the instructions below or heading to the Twitter developer website.

Once you have all the tokens and keys, here is how to connect your Twitter account to MindsDB:

CREATE DATABASE my_twitter 
WITH 
    ENGINE = 'twitter',
    PARAMETERS = {
      "bearer_token": "twitter bearer token",
      "consumer_key": "twitter consumer key",
      "consumer_secret": "twitter consumer key secret",
      "access_token": "twitter access token",
	  "access_token_secret": "twitter access token secret"
    };

Usage

The my_twitter database contains a table called tweets by default.

Here is how to search tweets containing mindsdb keyword:

SELECT id, created_at, author_username, text 
FROM my_twitter.tweets 
WHERE query = '(mindsdb OR #mindsdb) -is:retweet -is:reply'
AND created_at > '2023-02-16' 
LIMIT 20;

Please note that we can see only recent tweets from the past seven days. The created_at column condition is skipped if the provided date is earlier than seven days.

Alternatively, you can use a Twitter native query, as below:

SELECT * FROM my_twitter (
  search_recent_tweets(
    query = '(mindsdb OR #mindsdb) -is:retweet -is:reply',
    start_time = '2023-03-16T00:00:00.000Z',
    max_results = 2
  )
);

To learn more about native queries in MindsDB, visit our docs here.

Here is how to write tweets:

INSERT INTO my_twitter.tweets (reply_to_tweet_id, text)
VALUES 
    (1626198053446369280, 'MindsDB is great! now its super simple to build ML powered apps'),
    (1626198053446369280, 'Holy!! MindsDB is the best thing they have invented for developers doing ML');

For more information about available actions and development plans, visit this page.

What’s next?

Check out the tutorial on how to create a Twitter chatbot to see one of the interesting applications of this integration.