|
| 1 | +import { |
| 2 | + Action, |
| 3 | + IAgentRuntime, |
| 4 | + Memory, |
| 5 | + State, |
| 6 | + composeContext, |
| 7 | + elizaLogger, |
| 8 | + ModelClass, |
| 9 | + formatMessages, |
| 10 | + generateObject, |
| 11 | +} from "@elizaos/core"; |
| 12 | +import { Scraper } from "agent-twitter-client"; |
| 13 | +import { tweetTemplate } from "../templates"; |
| 14 | +import { isTweetContent, TweetSchema } from "../types"; |
| 15 | + |
| 16 | +async function composeTweet( |
| 17 | + runtime: IAgentRuntime, |
| 18 | + _message: Memory, |
| 19 | + state?: State |
| 20 | +): Promise<string> { |
| 21 | + try { |
| 22 | + const context = composeContext({ |
| 23 | + state, |
| 24 | + template: tweetTemplate, |
| 25 | + }); |
| 26 | + |
| 27 | + const tweetContentObject = await generateObject({ |
| 28 | + runtime, |
| 29 | + context, |
| 30 | + modelClass: ModelClass.SMALL, |
| 31 | + schema: TweetSchema, |
| 32 | + stop: ["\n"], |
| 33 | + }); |
| 34 | + |
| 35 | + if (!isTweetContent(tweetContentObject.object)) { |
| 36 | + elizaLogger.error( |
| 37 | + "Invalid tweet content:", |
| 38 | + tweetContentObject.object |
| 39 | + ); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + const trimmedContent = tweetContentObject.object.text.trim(); |
| 44 | + |
| 45 | + // Skip truncation if TWITTER_PREMIUM is true |
| 46 | + if ( |
| 47 | + process.env.TWITTER_PREMIUM?.toLowerCase() !== "true" && |
| 48 | + trimmedContent.length > 180 |
| 49 | + ) { |
| 50 | + elizaLogger.warn( |
| 51 | + `Tweet too long (${trimmedContent.length} chars), truncating...` |
| 52 | + ); |
| 53 | + return trimmedContent.substring(0, 177) + "..."; |
| 54 | + } |
| 55 | + |
| 56 | + return trimmedContent; |
| 57 | + } catch (error) { |
| 58 | + elizaLogger.error("Error composing tweet:", error); |
| 59 | + throw error; |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +async function postTweet(content: string): Promise<boolean> { |
| 64 | + try { |
| 65 | + const scraper = new Scraper(); |
| 66 | + const username = process.env.TWITTER_USERNAME; |
| 67 | + const password = process.env.TWITTER_PASSWORD; |
| 68 | + const email = process.env.TWITTER_EMAIL; |
| 69 | + const twitter2faSecret = process.env.TWITTER_2FA_SECRET; |
| 70 | + |
| 71 | + if (!username || !password) { |
| 72 | + elizaLogger.error( |
| 73 | + "Twitter credentials not configured in environment" |
| 74 | + ); |
| 75 | + return false; |
| 76 | + } |
| 77 | + |
| 78 | + // Login with credentials |
| 79 | + await scraper.login(username, password, email, twitter2faSecret); |
| 80 | + if (!(await scraper.isLoggedIn())) { |
| 81 | + elizaLogger.error("Failed to login to Twitter"); |
| 82 | + return false; |
| 83 | + } |
| 84 | + |
| 85 | + // Send the tweet |
| 86 | + elizaLogger.log("Attempting to send tweet:", content); |
| 87 | + const result = await scraper.sendTweet(content); |
| 88 | + |
| 89 | + const body = await result.json(); |
| 90 | + elizaLogger.log("Tweet response:", body); |
| 91 | + |
| 92 | + // Check for Twitter API errors |
| 93 | + if (body.errors) { |
| 94 | + const error = body.errors[0]; |
| 95 | + elizaLogger.error( |
| 96 | + `Twitter API error (${error.code}): ${error.message}` |
| 97 | + ); |
| 98 | + return false; |
| 99 | + } |
| 100 | + |
| 101 | + // Check for successful tweet creation |
| 102 | + if (!body?.data?.create_tweet?.tweet_results?.result) { |
| 103 | + elizaLogger.error( |
| 104 | + "Failed to post tweet: No tweet result in response" |
| 105 | + ); |
| 106 | + return false; |
| 107 | + } |
| 108 | + |
| 109 | + return true; |
| 110 | + } catch (error) { |
| 111 | + // Log the full error details |
| 112 | + elizaLogger.error("Error posting tweet:", { |
| 113 | + message: error.message, |
| 114 | + stack: error.stack, |
| 115 | + name: error.name, |
| 116 | + cause: error.cause, |
| 117 | + }); |
| 118 | + return false; |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +export const postAction: Action = { |
| 123 | + name: "POST_TWEET", |
| 124 | + similes: ["TWEET", "POST", "SEND_TWEET"], |
| 125 | + description: "Post a tweet to Twitter", |
| 126 | + validate: async ( |
| 127 | + runtime: IAgentRuntime, |
| 128 | + message: Memory, |
| 129 | + state?: State |
| 130 | + ) => { |
| 131 | + const hasCredentials = |
| 132 | + !!process.env.TWITTER_USERNAME && !!process.env.TWITTER_PASSWORD; |
| 133 | + elizaLogger.log(`Has credentials: ${hasCredentials}`); |
| 134 | + |
| 135 | + return hasCredentials; |
| 136 | + }, |
| 137 | + handler: async ( |
| 138 | + runtime: IAgentRuntime, |
| 139 | + message: Memory, |
| 140 | + state?: State |
| 141 | + ): Promise<boolean> => { |
| 142 | + try { |
| 143 | + // Generate tweet content using context |
| 144 | + const tweetContent = await composeTweet(runtime, message, state); |
| 145 | + |
| 146 | + if (!tweetContent) { |
| 147 | + elizaLogger.error("No content generated for tweet"); |
| 148 | + return false; |
| 149 | + } |
| 150 | + |
| 151 | + elizaLogger.log(`Generated tweet content: ${tweetContent}`); |
| 152 | + |
| 153 | + // Check for dry run mode - explicitly check for string "true" |
| 154 | + if ( |
| 155 | + process.env.TWITTER_DRY_RUN && |
| 156 | + process.env.TWITTER_DRY_RUN.toLowerCase() === "true" |
| 157 | + ) { |
| 158 | + elizaLogger.info( |
| 159 | + `Dry run: would have posted tweet: ${tweetContent}` |
| 160 | + ); |
| 161 | + return true; |
| 162 | + } |
| 163 | + |
| 164 | + return await postTweet(tweetContent); |
| 165 | + } catch (error) { |
| 166 | + elizaLogger.error("Error in post action:", error); |
| 167 | + return false; |
| 168 | + } |
| 169 | + }, |
| 170 | + examples: [ |
| 171 | + [ |
| 172 | + { |
| 173 | + user: "{{user1}}", |
| 174 | + content: { text: "You should tweet that" }, |
| 175 | + }, |
| 176 | + { |
| 177 | + user: "{{agentName}}", |
| 178 | + content: { |
| 179 | + text: "I'll share this update with my followers right away!", |
| 180 | + action: "POST_TWEET", |
| 181 | + }, |
| 182 | + }, |
| 183 | + ], |
| 184 | + [ |
| 185 | + { |
| 186 | + user: "{{user1}}", |
| 187 | + content: { text: "Post this tweet" }, |
| 188 | + }, |
| 189 | + { |
| 190 | + user: "{{agentName}}", |
| 191 | + content: { |
| 192 | + text: "I'll post that as a tweet now.", |
| 193 | + action: "POST_TWEET", |
| 194 | + }, |
| 195 | + }, |
| 196 | + ], |
| 197 | + [ |
| 198 | + { |
| 199 | + user: "{{user1}}", |
| 200 | + content: { text: "Share that on Twitter" }, |
| 201 | + }, |
| 202 | + { |
| 203 | + user: "{{agentName}}", |
| 204 | + content: { |
| 205 | + text: "I'll share this message on Twitter.", |
| 206 | + action: "POST_TWEET", |
| 207 | + }, |
| 208 | + }, |
| 209 | + ], |
| 210 | + [ |
| 211 | + { |
| 212 | + user: "{{user1}}", |
| 213 | + content: { text: "Post that on X" }, |
| 214 | + }, |
| 215 | + { |
| 216 | + user: "{{agentName}}", |
| 217 | + content: { |
| 218 | + text: "I'll post this message on X right away.", |
| 219 | + action: "POST_TWEET", |
| 220 | + }, |
| 221 | + }, |
| 222 | + ], |
| 223 | + [ |
| 224 | + { |
| 225 | + user: "{{user1}}", |
| 226 | + content: { text: "You should put that on X dot com" }, |
| 227 | + }, |
| 228 | + { |
| 229 | + user: "{{agentName}}", |
| 230 | + content: { |
| 231 | + text: "I'll put this message up on X.com now.", |
| 232 | + action: "POST_TWEET", |
| 233 | + }, |
| 234 | + }, |
| 235 | + ], |
| 236 | + ], |
| 237 | +}; |
0 commit comments