twitter时间序列源码_为什么要拥有自己的Twitter机器人,以及如何在不到30分钟的时间内构建一个机器人...

松安民
2023-12-01

twitter时间序列源码

UPDATE 20171102: Since this story was originally posted back in January 2017 there have been a few things that have changed with the repository on GitHub, if you are going to be following along I’d suggest using the repository README.md in conjunction with this story to save any confusion.

更新20171102:自从这个故事最初于2017年1月发布以来,GitHub上的存储库发生了一些变化,如果您将继续关注,我建议结合使用README.md故事,以免造成任何混乱。

Twitter bots can do a heck of a lot more than just spam trending hashtags and relentlessly follow users.

Twitter机器人不仅可以使垃圾邮件成为趋势标签,而且可以不懈地关注用户,其所带来的好处还远远不止这些。

Take the Twisst ISS alerts bot, which sends you a direct message whenever the international space station (ISS) will be visible at your location.

Twisst ISS警报机器人为例,该机器人会在您所在位置可见国际空间站(ISS)时向您发送直接消息。

Or public service bots like the Earthquake Robot, which tweets about any earthquake greater than 5.0 on the Richter Scale as it happens.

地震机器人 ( Earthquake Robot)之类的公共服务机器人 ,它会在发生里氏震级时发布有关5.0级以上地震的推文。

And of course a robot that tweets poetry, poem.exe, along with one that will retweet your tweets that also happen to be an Accidental Haiku.

当然,还有一个自动发送诗歌推文的机器人poem.exe ,以及一个可以推文推文的机器人,这也恰好是偶然的Haiku

I personally use a bot to enhance my @ScottDevTweets account by liking and re-tweeting subjects I have an interest in.

我个人使用机器人通过喜欢并重新发布我感兴趣的主题来增强我的@ScottDevTweets帐户。

The #100DaysOfCode community challenge will congratulate you on starting the #100DaysOfCode challenge, and again when you reach specific milestones.

#100DaysOfCode社区挑战赛将祝贺您启动#100DaysOfCode挑战赛,并再次祝贺您达到特定的里程碑。

It will also reply with encouragement if it detects negative sentiment (frustration) in a tweet that has the #100DaysOfCode hashtag in it.

如果它在其中具有#100DaysOfCode主题标签的推文中检测到负面情绪(沮丧),它也会给予鼓励。

One question I’m asked in job interviews quite often is “what do you get out of working with technology?” I always answer that “I like to automate repetitive tasks to save me time so I can concentrate on other stuff. I like the the feeling of accomplishment that comes with having saved myself some time.”

我在工作面试中经常被问到的一个问题是“您会从使用技术中得到什么?” 我总是回答:“我喜欢自动执行重复性任务以节省时间,因此我可以专注于其他工作。 我喜欢节省自己一些时间所带来的成就感。”

In the case of my @ScottDevTweets bot, it’s usually an opener for a conversation with another person who follows me. So the bot can initiate the conversation, then I can carry on from where the bot left off.

就我的@ScottDevTweets机器人而言,它通常是与跟随我的另一个人进行对话的开启者。 这样,机器人可以发起对话,然后我可以从机器人停止的地方继续进行对话。

Bearing this in mind, a bot is only as ethical as the person who programmed it.

牢记这一点,机器人只与编写它的人一样道德。

If you have any doubts about the ethics of the bot you’re building, check out botwiki’s ethics section.

如果您对所构建的机器人的道德有任何疑问,请查看botwiki的道德部分。

So, ready to get started? OK. Let’s do this!

那么,准备开始了吗? 好。 我们开工吧!

如何在30分钟内建立Twitter Bot (How to build a Twitter Bot in 30 minutes)

You’re going to use the twit library to build a Twitter bot. It will like and re-tweet whatever you specify. It will also reply to your followers with a selection of canned responses.

您将使用twit库构建一个Twitter机器人。 它将喜欢并重新发布您指定的任何内容。 它还会以选择的罐头回复方式回复您的关注者。

Before starting the clock you’ll need to set up some accounts set up if you don’t have them already.

如果您还没有帐户,那么在开始计时之前,您需要设置一些帐户。

你需要什么 (What you’ll need)

步骤1:建立Twitter应用程式 (Step 1: Set up a Twitter application)

Either create a new Twitter account or use your own to create a new Twitter application.

创建一个新的Twitter帐户,或使用您自己的帐户创建一个新的Twitter应用程序

As an example, I’ll configure my old @DroidScott twitter account so you can follow along.

例如,我将配置旧的@DroidScott Twitter帐户,以便您可以继续进行。

Be sure to add your phone number to your Twitter account before clicking the Create your Twitter application button.

在单击“ 创建Twitter应用程序”按钮之前,请确保将您的电话号码添加到您的Twitter帐户中。

You should now be in the ‘Application Management’ section, where you will need to take a note of your keys. You should have your ‘Consumer Key (API Key)’ and ‘Consumer Secret (API Secret)’ already available.

您现在应该在“应用程序管理”部分,在这里您需要记录密钥。 您应该已经可以使用“消费者密钥(API密钥)”和“消费者密钥(API密钥)”。

You’ll need to scroll to the bottom of the page and click the Create my access token to get the ‘Access Token’ and ‘Access Token Secret’ take note of all four of them you’ll need them when setting up the bot.

您需要滚动到页面底部,然后单击“ 创建我的访问令牌”以获取“访问令牌”和“访问令牌密钥”,并记下设置机器人时需要使用的所有这四个。

步骤2:建立您的开发环境 (Step 2: Set up your development environment)

For this I’m just going to say use Cloud9 as you can be up and running in minutes with one of the pre-made Node.js environments.

为此,我只想说说使用Cloud9,因为您可以在其中一个预制的Node.js环境中在几分钟内启动并运行。

Note that if you choose to use Heroku and/or Cloud9 IDE in building this (like I do in this guide) in some regions you will be prompted to give a credit card number to create these accounts.

请注意,如果您选择在某些地区使用Heroku和/或Cloud9 IDE进行构建(如本指南中的操作),则会提示您输入信用卡号来创建这些帐户。

设置机器人 (Set up the bot)

In the project tree delete the example project files of client, package.json, README.md and server.js you’ll not need them, you can leave them there if you desire.

在项目树中,删除clientpackage.jsonREADME.mdserver.js的示例项目文件,您将不需要它们,可以根据需要将其保留在其中。

In your new Node.js c9 environment go to the terminal and enter:

在新的Node.js c9环境中,转到终端并输入:

git clone https://github.com/spences10/twitter-bot-bootstrap

项目结构 (Project structure)

The environment project tree should look something like this:

环境项目树应如下所示:

节点依赖性 (Node dependencies)

Before configuring the bot we’ll need to install the dependencies, cd into the project folder with cd tw* this will move you to :~/workspace/twitter-bot-bootstrap (master) $ from the terminal enter:

在配置机器人之前,我们需要安装依赖项,使用cd tw*将cd插入项目文件夹,这将使您从终端进入: :~/workspace/twitter-bot-bootstrap (master) $ ,输入:

npm install

This will install all the dependencies listed in the package.json file.

这将安装package.json文件中列出的所有依赖项。

If you get any errors then I suggest installing the dependencies one by one from the package.json file with the same command and the package name at the end:

如果遇到任何错误,建议您从package.json文件中逐个安装依赖项,并在末尾使用相同的命令和包名称:

Here is an example of the dependencies in the package,json file:

这是package,json文件中dependencies的示例:

"dependencies": {    "dotenv": "^4.0.0",    "twit": "^2.2.5",    "unique-random-array": "^1.0.0",    "unirest": "^0.5.1"  }

The npm command to install them all:

npm命令将其全部安装:

npm install --save dotenv twit unique-random-array unirest

If you get any WARN messages such as npm WARN package.json twitter-bot@1.0.0 No repository field this will not break the bot so it's safe to ignore.

如果收到任何WARN消息,例如npm WARN package.json twitter-bot@1.0.0 No repository field则不会破坏机器人,因此可以放心地忽略它。

Now you can configure the bot. From the terminal enter:

现在,您可以配置机器人了。 从终端输入:

npm init

This will configure the package.json file with your details as desired. Just keep hitting return if you're happy with the defaults.

这将根据需要使用您的详细信息配置package.json文件。 如果您对默认设置感到满意,只需保持点击即可。

Now you’ll need to add your Twitter keys to the .env file. Just input the keys in their corresponding fields and save the file.

现在,您需要将Twitter密钥添加到.env文件。 只需在相应的字段中输入密钥并保存文件即可。

If you can not find the .env file in the file structure of your c9 project then you will need to enable the Show Hidden Filesoption. In the file view select the settings cog then tick the Show Hidden Files option if it is not already checked.

如果在c9项目的文件结构中找不到.env文件,则需要启用“ Show Hidden Files选项。 在文件视图中,选择设置齿轮,然后勾选“ Show Hidden Files选项(如果尚未选中)。

The SENTIMENT_KEY you can get a new API key at https://market.mashape.com/vivekn/sentiment-3 your key is in the REQUEST EXAMPLE

您可以在https://market.mashape.com/vivekn/sentiment-3上获得SENTIMENT_KEY新API密钥,您的密钥在REQUEST EXAMPLE

Take a look at the gif, click the link, sign up for or sign into mashape, click on node in the right hand panel and select out your API key, it will be in the space highlighted <required> in the gif.

看一下gif,单击链接,注册或登录mashape ,在右侧面板上单击node并选择您的API密钥,它将位于gif中突出显示的<requir ed>区域。

Add your API key to the .env file along with your Twitter API keys ?

将您的API密钥与Twitter API密钥一起添加到.env文件中?

Here you should add your Twitter account name, and how often you want the bot to run the retweet and favorite functions in minutes.

在这里,您应该添加您的Twitter帐户名,以及您希望该漫游器在几分钟内运行转发和收藏夹功能的频率。

NOTE none of the .env items have quotes '' round them.

注意.env项都没有引号''

CONSUMER_KEY=Fw***********P9CONSUMER_SECRET=TD************CqACCESS_TOKEN=31**************UCACCESS_TOKEN_SECRET=r0************S2SENTIMENT_KEY=Gj************lFTWITTER_USERNAME=DroidScottTWITTER_RETWEET_RATE=5TWITTER_FAVORITE_RATE=5

You can then add some keywords into the strings.js file for what you want to search for as well as sub-queries.

然后,您可以将一些关键字添加到strings.js文件中,以查找您要搜索的内容以及子查询。

When adding sub-query strings make sure you leave a space at the beginning of the string so ' handy tip' gets concatenated onto 'node.js' as node.js handy tip and not node.jshandy tip.

添加子查询字符串时,请确保在字符串的开头留一个空格,以使' handy tip'被连接到'node.js'作为node.js handy tip而不是node.jshandy tip

That should be it, go to the terminal and enter npm start you should get some output:

就是这样,转到终端并输入npm start您应该得到一些输出:

Check the Twitter account:

检查Twitter帐户:

步骤3:设定Heroku (Step 3: Setting up Heroku)

Cool, now we have a bot that we can test on our dev environment but we can’t leave it there, we’ll need to deploy it to Heroku.

太酷了,现在我们有了一个可以在开发环境上进行测试的机器人,但是我们不能将其留在那儿,我们需要将其部署到Heroku。

If you haven’t done so already set up a Heroku account then select Create a new app from the dropdown box top right of your dashboard, in the next screen name the app it if you want, then click Create App.

如果尚未设置好Heroku帐户 ,请从信息中心右上方的下拉框中选择创建新应用 ,在下一个屏幕名称中将该应用命名(如果需要),然后点击创建应用

You’ll be presented with your app dashboard and instructions for the deployment method.

系统将为您提供应用程序仪表板和有关部署方法的说明。

Your app name should be displayed on the top of your dashboard, you’ll need this when logging in with the Heroku command line interface, which we’ll use to deploy your app.

您的应用程序名称应显示在仪表板的顶部,使用Heroku命令行界面登录时将需要此名称,我们将使用它来部署您的应用程序。

Heroku CLI (Heroku CLI)

We’re going to deploy initially via the Heroku Command Line Interface (CLI).

我们将首先通过Heroku命令行界面( CLI )进行部署。

On your c9 environment terminal, log into Heroku [it should be installed by default]

在c9环境终端上,登录Heroku [默认情况下应安装]

heroku login

Enter your credentials:

输入您的凭证:

cd twitter-bot-bootstrap git init heroku git:remote -a your-heroku-app-name

Deploy your application:

部署您的应用程序:

git add . git commit -am 'make it better' git push heroku master

You should get build output in the terminal:

您应该在终端中获得构建输出:

Then check the output with:

然后使用以下命令检查输出:

heroku logs -t

All good? Cool! ?

都好? 凉! ?

配置Heroku变量 (Configuring Heroku variables)

Now that we have our bot on Heroku we need to add environment variables to store our Twitter keys. This is because the .env file where we stored our keys is listed in the .gitignore file, which tells git not to upload that file to Heroku. It also makes it so if in the future we want to add our code to GitHub we don't have to worry about the .env file making our keys public, because the file will automatically be ignored.

现在我们的机器人在Heroku上了,我们需要添加环境变量来存储我们的Twitter密钥。 这是因为.gitignore文件中列出了存储密钥的.env文件,该文件告诉git不要将该文件上传到Heroku。 这也使得如果将来我们想将代码添加到GitHub时,我们不必担心.env文件将密钥公开,因为该文件将被自动忽略。

All you need to do is go to the console of your Heroku app and select the ‘Settings’ sections and add in your Twitter keys from the .env file. Click the 'Reveal Config Vars' button and add in the variables with their corresponding values:

您所需要做的就是转到Heroku应用程序的控制台,然后选择“设置”部分,然后从.env文件中添加Twitter密钥。 单击“ Reveal Config Vars”按钮,并添加变量及其相应的值:

CONSUMER_KEYCONSUMER_SECRETACCESS_TOKENACCESS_TOKEN_SECRETSENTIMENT_KEY

Once you have the Heroku vars set up, take a look at the config.js file of this project. You are going to delete this line:

设置好Heroku变量后,请查看该项目的config.js文件。 您将删除此行:

require('dotenv').config();

You’re now ready to deploy to Heroku again. Your console commands should look something like this:

现在您可以再次部署到Heroku了。 您的控制台命令应如下所示:

$ git add .$ git commit -m 'add environment variables'$ git push heroku master

Then you can check the Heroku logs again with:

然后,您可以使用以下命令再次检查Heroku日志:

$ heroku logs -t

You should now have a bot you can leave to do its thing forever more, or until you decide you want to change the search criteria ?

现在,您应该拥有一个机器人,您可以离开它做更多的事情,或者直到您决定更改搜索条件?

通过GitHub部署Heroku (Heroku deployment via GitHub)

You can also deploy your app by connecting to GitHub and deploy automatically to Heroku each time your master branch is updated on GitHub, this is straight forward enough.

您还可以通过连接到GitHub来部署应用程序,并在每次在GitHub上更新主分支时自动将其部署到Heroku,这很简单。

Go to the ‘Deploy’ dashboard on Heroku, select GitHub as the deployment method if you have connected your GitHub account to your Heroku account then you can search for the repository so if you forked this repo then you can just enter twitter-bot-bootstrap and Search you can then click the Connect button, you can then auto deploy from GitHub.

转到Heroku上的“部署”仪表板,如果将GitHub帐户连接到Heroku帐户,则选择GitHub作为部署方法,然后可以搜索存储库,因此,如果您分叉了此仓库,则只需输入twitter-bot-bootstrap然后单击搜索 ,然后单击连接按钮,然后可以从GitHub自动部署。

Heroku故障排除 (Heroku troubleshooting)

What do you mean it crashed!?

你是什​​么意思崩溃了?

Ok, I found that sometimes the worker is set as web and it crashes out, try setting the worker again with:

好的,我发现有时将worker程序设置为web并崩溃,请尝试使用以下方法再次设置worker

heroku ps:scale worker=0 heroku ps:scale worker=1

If that still crashes out then try setting the Resources on the Heroku dashboard, I found if you toggle between the web, heroku and worker it usually settles down. Basically you need to be set to the worker Dyno this is what causes the Error R10 (Boot timeout) crashes because it's trying to use one of the other resources when it should be using the worker Dyno.

如果仍然崩溃,请尝试在Heroku仪表板上设置“ Resources ”,我发现如果您在webherokuworker之间切换,它通常会停顿下来。 基本上,您需要设置为worker Dyno,这是导致Error R10 (Boot timeout)崩溃的原因,因为它在应使用worker Dyno时尝试使用其他资源之一。

Other useful Heroku commands I use:

我使用的其他有用的Heroku命令:

heroku restart

By default you can only push your master branch if you are working on a development branch i.e. dev branch. If you want to test on Heroku, then you can use:

默认情况下,只有在开发分支(即dev分支)上工作时,才可以推送master分支。 如果要在Heroku上进行测试,则可以使用:

git push heroku dev:master

方便的提示 (Handy tip)

If you want to add this to your own GitHub repo and don’t want to share your API keys ? with the world then you should turn off tracking on the .env file. From the terminal enter this git command:

如果要将此添加到自己的GitHub存储库中,并且不想共享API密钥? 与世界接触时,您应该关闭跟踪。 env文件。 从终端输入此git命令:

$ git update-index --assume-unchanged .env

I have added my most used git command I use in this gist

我已经添加了我在本要点中最常用的git命令

结语 (Wrapping up)

Your Twitter bot should now be live. You can tinker with it and further configure it.

您的Twitter机器人现在应该可以使用了。 您可以修改它并进一步配置它。

Here’s my repository if you’d like to fork it and contribute back using pull requests. Any contributions large or small — major features, bug-fixes, integration tests — are welcome, but will be thoroughly reviewed and discussed.

这是我的存储库,如果您想将其分叉并使用拉取请求回馈。 我们欢迎任何大小的贡献,包括主要功能,错误修复,集成测试,但将进行彻底的审查和讨论。

致谢 (Acknowledgements)

Credit for the inspiration for this should go to @amanhimself and his posts on creating your own twitter bot.

对此的灵感应归功于@amanhimself以及他创建自己的Twitter机器人的文章。

create-a-simple-twitter-bot-with-node-js

用节点js创建一个简单的Twitter机器人

how-to-make-a-twitter-bot-with-nodejs

如何用nodejs制作一个twitter机器人

twitter-mctwitbot

微博

awesome-twitter-bots

真棒推特机器人

Other posts detailing useful Twitter bots.

其他文章详细介绍了有用的Twitter机器人。

www.brit.co/twitter-bots-to-follow

www.brit.co/twitter-bots-to-follow

www.hongkiat.com/using-twitter-bots

www.hongkiat.com/using-twitter-bots

Made it this far? Wow, thanks for reading! If you liked this story, please don’t forget to recommend it by clicking the ❤ button on the side, and by sharing it with your friends through social media.

到此为止了吗? 哇,谢谢阅读! 如果您喜欢这个故事,请不要忘记通过单击侧面的❤按钮并通过社交媒体与您的朋友分享来推荐它。

If you want to learn more about me, visit my blog, my Github, or tweet me @ScottDevTweets.

如果您想了解更多关于我的信息,请访问我的博客 ,我的Github或向我发送@ScottDevTweets信息

You can read other articles like this on my blog.

您可以在我的博客上阅读其他类似的文章

翻译自: https://www.freecodecamp.org/news/easily-set-up-your-own-twitter-bot-4aeed5e61f7f/

twitter时间序列源码

 类似资料: