forked from elizaOS/eliza
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostTweetAction.ts
128 lines (122 loc) · 3.87 KB
/
postTweetAction.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import {
Action,
elizaLogger,
IAgentRuntime,
Memory,
State,
} from "@elizaos/core";
import { TwitterScraper } from "../util/TwitterScraper.ts";
import { tweetProvider } from "../providers/tweetProvider.ts";
import {tokenPriceProvider} from "../providers/tokenPriceProvider.ts";
export const postTweetAction: Action = {
description: "Post a tweet on Twitter and be verified by Primus",
examples: [
[
{
user: "{{user1}}",
content: {
text: "Get the latest BTC price and post it on my twitter.",
},
},
{
user: "{{agentName}}",
content: {
text: "The latest tweet has posted.",
action: "POST_TWEET",
},
},
],
[
{
user: "{{user1}}",
content: {
text: "Help post a tweet which content is BTC price.",
},
},
{
user: "{{agentName}}",
content: {
text: "Completed!",
action: "POST_TWEET",
},
},
],
[
{
user: "{{user1}}",
content: {
text: "Post a tweet on twitter for me.",
},
},
{
user: "{{agentName}}",
content: {
text: "I'll post the latest tweet to your Twitter account now!",
action: "POST_TWEET",
},
},
],
],
handler: async (
runtime: IAgentRuntime,
message: Memory,
state?: State
): Promise<boolean> => {
const contentYouWantToPost = await tokenPriceProvider.get(runtime, message, state);
//check VERIFIABLE_INFERENCE_ENABLED
if (
!(
process.env.VERIFIABLE_INFERENCE_ENABLED === "true" &&
process.env.PRIMUS_APP_ID &&
process.env.PRIMUS_APP_SECRET
)
) {
elizaLogger.error(
`Parameter 'VERIFIABLE_INFERENCE_ENABLED' not set, Eliza will run this action!`
);
return false;
}
try {
if (
process.env.TWITTER_DRY_RUN &&
process.env.TWITTER_DRY_RUN.toLowerCase() === "true"
) {
elizaLogger.info(
`Dry run: would have posted tweet: ${contentYouWantToPost}`
);
return true;
}
const scraperWithPrimus = new TwitterScraper();
await scraperWithPrimus.login();
if (!(await scraperWithPrimus.getScraper().isLoggedIn())) {
elizaLogger.error("Failed to login to Twitter");
return false;
}
// post the tweet
elizaLogger.log("Attempting to send tweet:", contentYouWantToPost);
const result = await scraperWithPrimus.sendTweet(contentYouWantToPost);
elizaLogger.log("Tweet response:", result);
// Check for Twitter API errors
if (!result) {
elizaLogger.error(`Twitter API error ${result}`);
return false;
}
return true;
} catch (error) {
elizaLogger.error("Error in post action:", error);
return false;
}
},
name: "POST_TWEET",
similes: ["TWEET", "POST", "SEND_TWEET"],
validate: async (
runtime: IAgentRuntime,
message: Memory,
state?: State
) => {
const hasCredentials =
!!process.env.TWITTER_USERNAME && !!process.env.TWITTER_PASSWORD;
elizaLogger.log(`Has credentials: ${hasCredentials}`);
return hasCredentials;
},
};