Miért van szükség saját Twitter botra, és hogyan lehet kevesebb mint 30 perc alatt felépíteni?

UPDATE 20171102: Amióta ezt a történetet eredetileg 2017 januárjában tették közzé, néhány dolog megváltozott a GitHub adattáránál. Ha követni fogja, azt javasoljuk, README.mdhogy mentéshez használja a tárat ezzel a történettel együtt bármilyen zavar.

A Twitter-botok egy csomó mást is képesek megtenni, nem csak a felkapott spamek hashtagjeit és könyörtelenül követik a felhasználókat.

Vegyük a Twisst ISS riasztási botot, amely közvetlen üzenetet küld Önnek, amikor a nemzetközi űrállomás (ISS) látható lesz az Ön tartózkodási helyén.

Vagy olyan közszolgálati robotok, mint a Earthquake Robot, amely tweetel a Richter-skála 5,0-nél nagyobb földrengéséről.

És természetesen egy robot, amely a verseket, a poem.exe-t tweeteli, valamint egy olyan, amely retweeteli a tweetjeit, amelyek véletlenül véletlenül is Haiku-k.

Én személy szerint botot használok a @ScottDevTweets fiók javításához azáltal, hogy kedvelem és újból tweetelek az érdeklődő alanyokat.

A # 100DaysOfCode közösségi kihívás gratulálni fog Önnek a # 100DaysOfCode kihívás elindításakor, és akkor is, amikor elér egy adott mérföldkövet.

Akkor is biztatóan válaszol, ha negatív hangulatot (frusztrációt) észlel egy tweetben, amelyben a # 100DaysOfCode hashtag található.

Az egyik állásinterjún feltett kérdésem az, hogy „mit hoz ki a technológiával való munkából?” Mindig azt válaszolom, hogy „szeretek automatizálni az ismétlődő feladatokat, hogy időt spóroljak meg, hogy más dolgokra tudjak koncentrálni. Tetszik a teljesítmény érzése, ami azzal jár, hogy megspóroltam magamnak egy kis időt. ”

A @ScottDevTweets botom esetében ez általában nyitó egy másik emberrel folytatott beszélgetéshez, aki követ engem. Tehát a bot kezdeményezheti a beszélgetést, majd onnan folytathatom, ahol a bot abbahagyta.

Ezt szem előtt tartva egy bot csak annyira etikus, mint az a személy, aki beprogramozta.

Ha kétségei vannak a létrehozott bot etikájával kapcsolatban, nézze meg a botwiki etikai szakaszát.

Tehát készen áll a kezdésre? RENDBEN. Csináljuk!

Hogyan készítsünk egy Twitter Botot 30 perc alatt

A twitkönyvtár segítségével Twitter botot fog építeni. Kedveli és újra tweetel, amit csak megad. Ezenkívül válogatott konzerv válaszokkal válaszol a követőinek.

Az óra megkezdése előtt be kell állítania néhány fiókot, ha még nincs.

Amire szüksége lesz

  • Twitter
  • Cloud9 IDE
  • Heroku

1. lépés: Állítson be egy Twitter alkalmazást

Vagy hozzon létre új Twitter-fiókot, vagy használjon saját Twitter-alkalmazást.

Példaként konfigurálom a régi @DroidScott twitter fiókomat, hogy nyomon követhesse.

Mielőtt rákattintana a Twitteralkalmazás létrehozása gombra, feltétlenül adja hozzá telefonszámát Twitter-fiókjához .

Most az „Alkalmazáskezelés” szakaszban kell lennie, ahol fel kell jegyeznie a kulcsokat. A „Consumer Key (API Key)” és a „Consumer Secret (API Secret)” már elérhetőnek kell lennie.

Görgessen az oldal aljára, és kattintson a Hozzáférési token létrehozása elemre , hogy az „Access Token” és az „Access Token Secret” tudomásul vegye mind a négyet, amire szüksége lesz rájuk a bot beállításakor.

2. lépés: Állítsa be a fejlesztői környezetet

Ehhez csak azt akarom mondani, hogy használja a Cloud9-et, mivel percek alatt fel tud működni az előre elkészített Node.js környezetek egyikével.

Ne feledje, hogy ha a Heroku és / vagy a Cloud9 IDE használatát választja ennek felépítéséhez (például én ebben az útmutatóban), egyes régiókban a rendszer kéri, hogy adjon meg hitelkártyaszámot e számlák létrehozásához.

Állítsa be a botot

A projekt fa törölje a példaprojektet fájlokat client, package.json, README.mdés server.jsakkor nem kell őket, akkor hagyja ott őket, ha kívánják.

Az új Node.js c9 környezetben lépjen a terminálra, és írja be:

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

A projekt felépítése

A környezeti projekt fának körülbelül így kell kinéznie:

Csomópontfüggőségek

Mielőtt konfigurálnánk a botot, telepítenünk kell a függőségeket, a cd a projekt mappába ezzel cd tw*átkerül :~/workspace/twitter-bot-bootstrap (master) $a terminál belépőjéből:

npm install

Ez telepíti a package.jsonfájlban felsorolt ​​összes függőséget .

Ha bármilyen hibát észlel, javaslom a függőségek egyesével történő telepítését a package.jsonfájlból ugyanazzal a paranccsal és a csomag nevével a végén:

Íme egy példa dependenciesa package,jsonfájlban található:

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

Az npm parancs az összes telepítéséhez:

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

Ha bármilyen WARNilyen üzenetet kap , npm WARN package.json [email protected] No repository fieldakkor az nem bontja meg a botot, így biztonságosan figyelmen kívül hagyhatja.

Most konfigurálhatja a botot. A terminálról írja be:

npm init

Ez package.jsontetszés szerint konfigurálja a fájlt az Ön adataival. Csak folytassa a visszatérést, ha elégedett az alapértelmezettekkel.

Most hozzá kell adnia a Twitter-kulcsokat a .envfájlhoz. Csak írja be a kulcsokat a megfelelő mezőkbe, és mentse a fájlt.

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.

The SENTIMENT_KEY you can get a new API key at //market.mashape.com/vivekn/sentiment-3 your key is in the 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 ed> in the gif.

Original text


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

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

NOTE none of the .env items have quotes '' round them.
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.

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.

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

Check the Twitter account:

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.

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.

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 CLI

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

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

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! ?

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.

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:

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:

require('dotenv').config();

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

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

Then you can check the Heroku logs again with:

$ 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 ?

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.

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 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:

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.

Other useful Heroku commands I use:

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:

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:

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

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

Wrapping up

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

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.

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

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

twitter-mctwitbot

awesome-twitter-bots

Other posts detailing useful Twitter bots.

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

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.

You can read other articles like this on my blog.