1. Data Integrations
  2. Twitter

In this section, we present how to integrate a Twitter account with MindsDB.

Connection

Here are the requirements to connect a Twitter account to MindsDB:

  1. You need a Twitter developer account.
  2. You need to generate API keys following the instructions below or heading to the Twitter developer website.

How to get API keys?

  • Create a Twitter developer account and then create an application with Read/Write permissions activated:
    • Open developer portal.
    • Select the Add app button to create a new app.
    • Select the Create new button.
    • Select Production and give it a name.
    • Copy and populate the following in the below CREATE DATABASE statement:
      • Bearer Token as a value of the bearer_token parameter.
      • API Key as a value of the consumer_key parameter.
      • API Key Secret as a value of the consumer_secret parameter.
  • Click Setup on User authentication settings:
    • On Permissions, select Read and Write.
    • On Type of app, select Web App, Automated App or Bot.
    • On App info, provide any URL for the callback URL and website URL.
    • Click Save.
  • Once you are back in the app settings, click Keys and Tokens:
    • Generate Access Token and Access Token Secret and populate it in the below CREATE DATABASE statement:
      • Access Token as a value of the access_token parameter.
      • Access Token Secret as a value of the access_token_secret parameter.

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;

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');

What’s next?

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