E.g.: Integrating with X/Twitter

In this example, we are going to create a single-file agent to retrieve a tweet from X and reply to it. Once we have successfully fetched the tweet, we will utilize Syntax Agent's API capabilities to generate a response. After that, you can reply to the original tweet with a generated response. This process will allow us to seamlessly integrate tweet retrieval with automated response generation, showcasing the potential of utilizing APIs for enhanced interaction.

Set up the environment

Before proceeding, create a .env file with the following content. You can generate SYNTAX_API_KEY in the Creator Dashboard page. To get the TWITTER_COOKIE_CT0 and TWITTER_COOKIE_AUTH values, you need to log in to your X account and get data from the developer tools.

.env
TWITTER_USERNAME=
TWITTER_PASSWORD=
# Log in the browser with your account and copy the `ct0` and `auth_token` cookies.
# (You can find them in the Application => Cookies in the dev tools)
TWITTER_COOKIE_CT0=
TWITTER_COOKIE_AUTH=

SYNTAX_API_KEY=
SYNTAX_API="http://api.spectrallabs.xyz"

Initialize your project based on the preferred programming language.

$ npm init --yes
$ npm pkg set type="module"
$ npm install dotenv agent-twitter-client

Create the agent

Once you successfully set up the project, create a file that acts as your agent. We've commented out the code that generates the reply in the last line to avoid accidentally submitting a reply to the original tweet. Feel free to uncomment it if you want.

index.js
import 'dotenv/config';
import { Scraper } from 'agent-twitter-client';

const username = process.env.TWITTER_USERNAME;
const password = process.env.TWITTER_PASSWORD;

const syntaxApi = process.env.SYNTAX_API;
const syntaxApiKey = process.env.SYNTAX_API_KEY;

if (!username || !password || !syntaxApi || !syntaxApiKey) {
  console.error("Invalid env vars. Please check your .env file.");
  process.exit(1);
}

const [targetUser] = process.argv.slice(2);

if (!targetUser) {
  console.error("Please provide a valid twitter username.");
  process.exit(1);
}

const scraper = new Scraper();
await scraper.login(username, password);

async function chat(content) {
  return await fetch(`${syntaxApi}/chat`, {
    method: 'POST',
    headers: {
      'Authorization': `Bearer ${syntaxApiKey}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      content
    }),
  })
  .then((res) => res.json())
  .then((res) => res.data.content)
  .catch((err) => console.error(err.message));
}

console.log('Feching tweets from:', targetUser);
const tweets = await scraper.getTweets(targetUser, 1);
const [tweet] = await Array.fromAsync(tweets);
console.log('Fetched tweet:', tweet.text);

const content = `
  Hey, reply to this tweet from ${tweet.name}?

  Tweet: ${tweet.text}

  ${tweet.isQuoted ? `The tweet quoted this tweet: ${tweet.quotedStatus.text}` : ''}
`;

const result = await chat(content);
console.log('Generate response from the agent:', result);

// Make a reply to the original tweet
// await scraper.sendTweet(result, tweet.id).then(res => res.json());

You can test and run the agent with the following commands. Don't forget to pass a valid X account name.

$ node index.js elonmusk

Last updated