Link Search Menu Expand Document

Notion To Twitter


Have you felt the need for distraction-free tweeting? What if you could tweet, without ever leaving your notetaking platform? Meet NotionToTwitter, a Python-based, fully open-source package, that allows you to draft, brainstorm and tweet your posts to Twitter, all from your Notion workspace.


Table of contents
  1. Description
  2. GitHub Repository
  3. Usage
    1. Twitter Setup
    2. Interfacing with Notion
    3. Running the script
  4. Requirements
  5. Directory Structure
  6. Sources

NotionToTwitter in actionNotionToTwitter in action

Description

This project allows you to post newly added threads written in your Notion database (along with the images) directly on to your twitter account via the APIs provided by the two. You can even schedule in advance when you want your tweet to be posted. Update the Post Date column in your Notion database to schedule your tweets. You can run the script scripts/runNotionToTwitter.sh peridocially at a reasonable frequency via a crontab job.

GitHub Repository

You can find the all the assosciated code for NotionToTwitter in this github repository

Usage

Twitter Setup

  • Register an app on Twitters’s developer portal (follow instructions online).
  • Obtain its APIConsumerKey, APIConsumerSecret, Bearer Token, AccessToken and AccessTokenSecret and add it to secrets/secrets_twitter.json in the following format:
{
    "APIConsumerKey": "your key here",
    "APIConsumerSecret": "your secret here",
    "BearerToken": "your token here",
    "AccessToken": "your token here",
    "AccessTokenSecret": "your access token here"
}

Interfacing with Notion

  • Register a private integration on your Notion workspace (follow instructions online)
  • Obtain its notionToken
  • Create a database on Notion to contain all the entries you need to post on Twitter. Make sure it has the following properties. If you want to add more properties or remove, modify the functions and classes in lib/port_utils.py.
    Title property: Tweet
    Formula properties: Image Path Prefix, Username, Start Tweet URL
    Bool properties: Tweeted?
    Date properties: Post Date
    Text properties: Error Message, Start Tweet ID
    URL properties: Retweet URL
    
  • Note: The formula for the Image Path Prefix property should be format("path-to-your-local-images-folder")
  • Note: The formula for the Username property should be format("your-twitter-username")
  • Note: Write your tweet / tweet thread in the page content (row) of the database – one tweet per block. If you have an image for any of the tweets, just add an <img> tag followed by the name of the image. e.g. tweet text here blah blach <img>image1.png. (Untested: If you want to include multiple images to a single tweet, separate the image names using ; without any spaces, i.e. this is my tweet text. Look out for images.<img>image1.png;image2.jpg;image3). If the first tweet in the thread (or the only tweet in the thread) is a quote retweet, add the URL of the retweet in the property Retweet URL.
  • Note: Only Tweet, Post Date, Retweet URL (optional) and Tweet page contents are to be filled by you. Image Path Prefix and Username are fixed to the database and the rest of the properties are filled in by the script.

  • Get the databaseID from Notion and add it to secrets/secrets_notion.json in the following format:
{
    "notionToken": "your notion token",
    "databaseID": "your notion database ID"
}

Running the script

  • Run the python script src/notionToTwitter.py with the correct Twitter username and Notion database (as given in the dictionaries in globalStore/constants.py) as command line arguments
  • You can periodically run this file again as a script scripts/runNotionToTwitter.sh using a crontab job to get periodic updates (I recommend every day). For more information on Crontab, check out this reference.

Requirements

You can install all the requirements using the following command:

pip install -r requirements.txt
pip install -e .

Directory Structure

.
+-- docs/
|   +-- images/
|   |   +-- demo.png
|   |   +-- icon.png
+-- globalStore/
|   +-- constants.py
+-- lib/
|   +-- port_utils.py
+-- notebooks/
|   +-- trial.py
|   +-- test port_utils.py
+-- scripts/
|   +-- runNotionToTwitter.sh
+-- secrets/
|   +-- secrets_notion.json
|   +-- secrets_twitter.json
+-- src/
|   +-- notionToTwitter.py
+-- .gitignore
+-- juyptext.toml
+-- LICENSE
+-- README.md
+-- requirements.txt
+-- STDOUTlog_examples.txt

Additional directory to store images. Make sure to sync a folder to a cloud service of your choice if you plan to run this codebase on a server (I use Google Drive). You should also add this path to the Notion DB in the Image Path Prefix column. For example, if you are using Google Drive, the path should be something like:

path to google drive/My Drive/Notion/TwitterImages

Sources



If you use this in your work and want to adapt this code, please consider starring the github repo or forking from it!

You can cite it as:

@misc{ nanbhas2023notionToTwitter,
  title         = "Notion To Twitter",
  author        = "Bhaskhar, Nandita",
  howpublished  = "GitHub Repo, https://github.com/nanbhas/NotionToTwitter",
  year          = "2023",
  url           = "https://web.stanford.edu/~nanbhas/projects/notion-to-twitter/"
}