综合介绍
agent-twitter-client是一个无需Twitter API密钥即可使用的Twitter客户端。该项目基于@the-convocation/twitter-scraper进行了修改,增加了发送推文和转推的功能。它可以在浏览器和服务器上运行,适用于需要自动化Twitter操作的用户。通过配置环境变量进行身份验证,用户可以轻松地发送推文、获取推文和进行其他Twitter操作。
功能列表
- 发送推文和转推
- 获取特定用户的推文
- 获取推文及其回复
- 获取最新推文
- 使用Twitter API v2进行推文和投票功能
- 支持代理请求
- 获取Twitter Cookies以避免频繁登录
使用帮助
安装流程
- 克隆项目仓库:
git clone https://github.com/elizaOS/agent-twitter-client.git
- 进入项目目录:
cd agent-twitter-client
- 安装依赖:
npm install
配置环境变量
在项目根目录下创建一个.env
文件,并添加以下内容:
TWITTER_USERNAME=你的Twitter用户名
TWITTER_PASSWORD=你的Twitter密码
TWITTER_EMAIL=你的Twitter邮箱
PROXY_URL=你的代理URL(如果需要)
TWITTER_API_KEY=你的Twitter API密钥
TWITTER_API_SECRET_KEY=你的Twitter API密钥
TWITTER_ACCESS_TOKEN=你的Twitter访问令牌
TWITTER_ACCESS_TOKEN_SECRET=你的Twitter访问令牌密钥
使用示例
获取推文
const { Scraper } = require('agent-twitter-client');
const scraper = new Scraper();
(async () => {
await scraper.login('你的用户名', '你的密码');
const tweets = await scraper.getTweets('elonmusk', 10);
console.log(tweets);
})();
发送推文
const { Scraper } = require('agent-twitter-client');
const scraper = new Scraper();
(async () => {
await scraper.login('你的用户名', '你的密码');
await scraper.sendTweet('Hello world!');
})();
获取最新推文
const { Scraper } = require('agent-twitter-client');
const scraper = new Scraper();
(async () => {
await scraper.login('你的用户名', '你的密码');
const latestTweet = await scraper.getLatestTweet('elonmusk');
console.log(latestTweet);
})();
详细功能介绍
- 发送推文和转推:用户可以通过配置环境变量进行身份验证,然后使用
sendTweet
方法发送推文,或使用sendRetweet
方法进行转推。 - 获取推文:使用
getTweets
方法可以获取特定用户的推文,getTweetsAndReplies
方法可以获取推文及其回复。 - 获取最新推文:使用
getLatestTweet
方法可以获取特定用户的最新推文。 - 使用Twitter API v2:通过配置Twitter API v2的密钥和令牌,用户可以使用
sendTweetV2
方法发送带有投票功能的推文。 - 代理请求:支持通过配置
PROXY_URL
环境变量来使用代理进行请求。 - 获取Twitter Cookies:使用
getCookies
方法获取Twitter Cookies,以避免频繁登录。