Skip to main content

Twitter Langchain Toolkit

This project has been archived.

The maintainers of this project have marked this project as archived. No new releases are expected.

Project description

Twitter (X) Langchain Toolkit

Twitter integration with Langchain to enable agentic workflows using the core primitives defined in cdp-agentkit-core.

This toolkit contains tools that enable an LLM agent to interact with Twitter. The toolkit provides a wrapper around the Twitter (X) API, allowing agents to perform social operations like posting text.

Setup

Prerequisites

Installation

pip install twitter-langchain

Environment Setup

Set the following environment variables:

export OPENAI_API_KEY=<your-openai-api-key>
export TWITTER_API_KEY=<your-api-key>
export TWITTER_API_SECRET=<your-api-secret>
export TWITTER_ACCESS_TOKEN=<your-access-token>
export TWITTER_ACCESS_TOKEN_SECRET=<your-access-token-secret>
export TWITTER_BEARER_TOKEN=<your-bearer-token>

Usage

Basic Setup

from twitter_langchain import (
    TwitterApiWrapper,
    TwitterToolkit
)

# Initialize TwitterApiwrapper
twitter_api_wrapper = TwitterApiWrapper()

# Create TwitterToolkit from the api wrapper
twitter_toolkit = TwitterToolkit.from_twitter_api_wrapper(twitter_api_wrapper)

View available tools:

tools = twitter_toolkit.get_tools()
for tool in tools:
    print(tool.name)

Available Tools

The toolkit provides the following tools:

  1. account_details - Get the authenticated account details
  2. account_mentions - Get mentions for the account
  3. post_tweet - Post a tweet to the account
  4. post_tweet_reply - Post a reply to a tweet on Twitter

Using with an Agent

import uuid

from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage
from langgraph.prebuilt import create_react_agent

llm = ChatOpenAI(model="gpt-4o-mini")

# Create agent
agent_executor = create_react_agent(llm, tools)

# Example - post tweet
events = agent_executor.stream(
    {
        "messages": [
            HumanMessage(content=f"Please post 'hello, world! {uuid.uuid4().hex}' to twitter"),
        ],
    },
    stream_mode="values",
)

for event in events:
    event["messages"][-1].pretty_print()

Expected output:

================================ Human Message =================================
Please post 'hello, world! c4b8e3744c2e4345be9e0622b4c0a8aa' to twitter
================================== Ai Message ==================================
Tool Calls:
    post_tweet (call_xVx4BMCSlCmCcbEQG1yyebbq)
    Call ID: call_xVx4BMCSlCmCcbEQG1yyebbq
    Args:
        tweet: hello, world! c4b8e3744c2e4345be9e0622b4c0a8aa
================================= Tool Message =================================
Name: post_tweet
Successfully posted!
================================== Ai Message ==================================
The message "hello, world! c4b8e3744c2e4345be9e0622b4c0a8aa" has been successfully posted to Twitter!

Examples

Check out twitter-langchain/examples for inspiration and help getting started!

  • Chatbot Python: Simple example of a Python Chatbot that can interact on Twitter (X), using OpenAI.

Contributing

See CONTRIBUTING.md for detailed setup instructions and contribution guidelines.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

twitter_langchain-0.0.10.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

twitter_langchain-0.0.10-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file twitter_langchain-0.0.10.tar.gz.

File metadata

  • Download URL: twitter_langchain-0.0.10.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for twitter_langchain-0.0.10.tar.gz
Algorithm Hash digest
SHA256 ed84fc3d69c29aa62a0bf93f962ff003704a7322baea92ab011752c001d6c449
MD5 416ad8b32f0023698f69cae85118788b
BLAKE2b-256 2abbe238a7725e2e5466651d79d3b2c55b0ea6e3bc97a1dc5b89c6e92e00d7f8

See more details on using hashes here.

File details

Details for the file twitter_langchain-0.0.10-py3-none-any.whl.

File metadata

File hashes

Hashes for twitter_langchain-0.0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 2a1a310951b6d4d9a01e8cbe81823b6f2d458bb4d5acd524db60d79d0700a216
MD5 38b3c5cb2a6dc27a6f556adb9ce20cba
BLAKE2b-256 a4286d72e8b6457b0345ba0795595c7c228c269c933ce2ed56f735560f4e6bc1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page