I'll provide a minimal, actionable playbook to set up your Twitter engagement project using ElizaOS.
Minimal Setup Playbook
1. Initialize Project Structure
mkdir twitter-engagement
cd twitter-engagement
pnpm init
2. Create Essential Files Structure
mkdir -p src/actions src/templates src/types
touch .env
Your minimal project structure should look like:
twitter-engagement/
├── src/
│ ├── actions/
│ │ ├── scrape.ts
│ │ └── post.ts
│ ├── templates/
│ │ └── tweet.ts
│ └── types/
│ └── index.ts
├── .env
├── character.json
└── package.json
3. Install Required Dependencies
# Core dependencies
pnpm add @elizaos/core @elizaos/plugin-twitter
# Development dependencies
pnpm add -D typescript @types/node
4. Configure Environment
# .env
TWITTER_USERNAME=your_username
TWITTER_PASSWORD=your_password
TWITTER_EMAIL=your_email
TWITTER_DRY_RUN=false
OPENAI_API_KEY=your_api_key # If using OpenAI for response generation
5. Create Character Configuration
// character.json
{
"name": "Twitter Engagement Bot",
"description": "Helps manage Twitter engagement through automated responses",
"modelProvider": "openai",
"clients": ["twitter"],
"actions": ["SCRAPE_TWEETS", "POST_TWEET"],
"settings": {
"MAX_TWEET_LENGTH": 280,
"TWITTER_DRY_RUN": false
}
}
6. Implement Core Files
Tweet Template
// src/templates/tweet.ts
export const tweetTemplate = `
# Context
Original Tweet: {{tweet}}
Keywords: {{keywords}}
# Task
Generate a response that:
1. Is relevant to the original tweet
2. Includes the keywords naturally
3. Must be under 280 characters
4. Maintains professional tone
Return in format: {"text": "your tweet here"}`;
Types
// src/types/index.ts
export interface TweetContent {
text: string;
}
export interface ScrapeParams {
handles: string[];
keywords: string[];
}
Scrape Action
// src/actions/scrape.ts
import { Action, IAgentRuntime, Memory } from "@elizaos/core";
import { Scraper } from "agent-twitter-client";
export const scrapeAction: Action = {
name: "SCRAPE_TWEETS",
similes: ["FETCH_TWEETS"],
description: "Scrape tweets based on handles and keywords",
handler: async (runtime: IAgentRuntime, message: Memory) => {
const scraper = new Scraper();
try {
await scraper.login(
runtime.getSetting("TWITTER_USERNAME"),
runtime.getSetting("TWITTER_PASSWORD"),
runtime.getSetting("TWITTER_EMAIL")
);
const { handles, keywords } = message.content;
return await scraper.searchTweets(handles, keywords);
} catch (error) {
console.error("Scraping error:", error);
return false;
}
},
examples: [
[
{
user: "user1",
content: { text: "Fetch tweets from @handle about #AI" }
}
]
]
};
Post Action
// src/actions/post.ts
import { postAction } from "@elizaos/plugin-twitter";
export { postAction };
7. Start the Project
- Start ElizaOS with your character:
pnpm start --characters="character.json"
- Test Basic Functionality:
// Example interaction
const response = await runtime.handleMessage({
user: "user1",
content: {
text: "[Redacted Person] tweets from @[Redacted Person] about AI",
handles: ["@[Redacted Person]"],
keywords: ["AI", "artificial intelligence"]
}
});
Quick Testing Guide
- Test Twitter Authentication:
# Set TWITTER_DRY_RUN=true in .env for safe testing
- Test Tweet Scraping:
// Test with a single handle
const testScrape = {
handles: ["@testhandle"],
keywords: ["test"]
};
- Test Response Generation:
// Test with a sample tweet
const testTweet = "Original tweet content";
const response = await generateResponse(testTweet);
Next Steps
- Add more sophisticated tweet filtering:
// Add to scrapeAction
const filterRelevantTweets = (tweets, keywords) => {
return tweets.filter(tweet =>
keywords.some(keyword =>
tweet.text.toLowerCase().includes(keyword.toLowerCase())
)
);
};
- Implement rate limiting:
// Add to scrapeAction
const rateLimit = {
requests: 0,
resetTime: Date.now() + 15 * 60 * 1000
};
- Add response generation:
// Add to postAction
const generateResponse = async (tweet, keywords) => {
const context = composeContext({
template: tweetTemplate,
tweet,
keywords
});
return await runtime.generateObject({
context,
schema: TweetSchema
});
};
To extend functionality:
- Add more actions in
src/actions/
- Enhance templates in
src/templates/
- Add error handling and logging
- Implement user interface if needed
Remember:
- Always test with
TWITTER_DRY_RUN=true first
- Keep API rate limits in mind
- Backup your Twitter credentials securely
- Monitor your application's performance
Would you like me to explain any part in more detail?