å°ãªããšãTwisst ISSãããããã¢ã©ãŒããåãåããŸãããã®ããã㯠ãããªãã®å Žæããåœéå®å®ã¹ããŒã·ã§ã³ïŒISSïŒãèŠãããã³ã«ã¡ãã»ãŒãžãéä¿¡ããŸãã
ãŸãã¯ã å°éããããã®ãããªå ¬å ±ãµãŒãã¹ã®ãããã¯ããªãã¿ãŒã¹ã±ãŒã«ã§5ãè¶ ããéåãçºçãããšããã«ãã€ãŒããéä¿¡ããŸãã
ãããŠãã¡ãããè©©ãæçš¿ããããããpoem.exeãšã誀ã£ãŠä¿³å¥åœ¢åŒã§æžãããæçš¿ããªãã€ãŒãããå¶çºçãªä¿³å¥ããããŸãã
å人çã«ã¯ããããã䜿çšããŠ@ScottDevTweetsã¢ã«ãŠã³ããäœæããŸãããã®ãããã¯ãèå³ã®ãããããã¯ã«é¢ããæçš¿ãé«ãè©äŸ¡ã ããªãã€ãŒãããŸãã
ïŒ100DaysOfCodeã³ãã¥ããã£ã¯ãïŒ100DaysOfCode Challengeãéå§ãããšãã«ãç¥ãã®ã¡ãã»ãŒãžãéä¿¡ããŸãããŸããæé ãé²ããŠãããšãããã«ããã€ãã®ã¡ãã»ãŒãžãéä¿¡ãããŸãã
ãããã¯ãŠãŒã¶ãŒãç¥çŠããŸã
圌ã¯ãŸããããã·ã¥ã¿ã°ïŒ100DaysOfCodeã䜿ã£ãŠãåŠå®çãªææ ïŒè¿·æãªã©ïŒãèªèããå Žåããã€ãŒããžã®å±ãŸãã®å¿çãæ®ããŸãã
ãããã¯ææ ãèªã¿åã
ã€ã³ã¿ãã¥ãŒã§ã¯ããããITåéã®ä»äºã¯ããªãã«äœãããããã®ãããšãã質åãããŸããç§ã¯ãã€ãçããŸãã èªåã®ããã«æéãç¯çŽã§ããããšãå¬ããæããŸããã
ç§ã®@ScottDevTweetsããããšããã°ãç§ã¯äž»ã«ããã䜿çšããŠãµãã¹ã¯ã©ã€ããŒãšã®äŒè©±ãéå§ããŸãã ãããã£ãŠããããã¯äŒè©±ãéå§ã§ããç§ã¯ãããæŸããŸãã
äžèšã®ãã¹ãŠã念é ã«çœ®ããŠããããã¯ãããããã°ã©ã ãã人ã»ã©å«ççã§ãã
äœæãããããã®å«çã«ã€ããŠçåãããå Žåã¯ã botwikiã®å¯Ÿå¿ããã»ã¯ã·ã§ã³ãåç §ããŠãã ãã ã
ããã§ãå§ããæºåã¯ã§ããŸãããïŒ ããã§ã¯è¡ããïŒ
30åã§Twitterããããäœæããæ¹æ³
twitã©ã€ãã©ãªã䜿çšããŠTwitterããããäœæããŸãã äœæ¥ã®å®äºåŸã圌ã¯ããªããæå®ãããã®ã奜ãã«ãªãã€ãŒãããæºåããããã³ãã¬ãŒãããåçãéžæããããšã§èªè ã«åçããããšãã§ããŸãã
ããããæéãåŸ ã€ã æåã«ãããã€ãã®ã¢ã«ãŠã³ããäœæããå¿ èŠããããŸã-ãã¡ããããŸã æã£ãŠããªãå Žåã
次ã®ãã®ãå¿ èŠã§ãã
ã¹ãããïŒ1ïŒTwitterã¢ããªãã»ããã¢ãããã
æ°ããTwitterã¢ã«ãŠã³ããäœæããããæ¢åã®ã¢ã«ãŠã³ãã䜿çšããŠæ°ããTwitterã¢ããªã±ãŒã·ã§ã³ãäœæããŸã ã
äŸãšããŠãå€ã@DroidScottã¢ã«ãŠã³ããèšå®ããŸãããããªãã¯ç§ã®åŸã«ç¹°ãè¿ããŸãã
[ Twitterã¢ããªã±ãŒã·ã§ã³ã®äœæ ]ãã¿ã³ãã¯ãªãã¯ããåã«ãå¿ ãé»è©±çªå·ãã¢ã«ãŠã³ãã«ãã€ã³ãããŠãã ããã
ãã®åŸãããŒãèŠããŠããå¿ èŠãããã¢ããªã±ãŒã·ã§ã³ç®¡çã»ã¯ã·ã§ã³ã«ããããšã«æ°ä»ãã§ãããã ã³ã³ã·ã¥ãŒãããŒïŒAPIããŒïŒãšã³ã³ã·ã¥ãŒãã·ãŒã¯ã¬ããïŒAPIã·ãŒã¯ã¬ããïŒã¯æ¢ã«å©çšå¯èœã§ããã¯ãã§ãã
ããŒãžã®äžéšã«ç§»åãã[ ã¢ã¯ã»ã¹ããŒã¯ã³ã®äœæ ]ãã¿ã³ãã¯ãªãã¯ããŠãã¢ã¯ã»ã¹ããŒã¯ã³ãšã¢ã¯ã»ã¹ããŒã¯ã³ã·ãŒã¯ã¬ãããååŸããå¿ èŠããããŸãã ããããå¿ããªãã§ãã ãã-ããããã»ããã¢ãããããšãã¯ã4ã€ãã¹ãŠãå¿ èŠã«ãªããŸãã
ã¹ãããïŒ2ïŒéçºç°å¢ãã»ããã¢ãããã
ããã§1ã€èšããŸããCloud9ã䜿çšããŸããNode.jsã®æ¢è£œã®ç°å¢ã®1 ã€ã§ã¯ ãããã»ã¹å šäœã«æ°åããããŸãã
å± äœå°åã«å¿ããŠããããã®äœææã«Herokuããã³/ãŸãã¯Cloud9 IDEïŒä»¥äžã®ããã«ïŒã䜿çšããããšã«ããå Žåãã¯ã¬ãžããã«ãŒãçªå·ã®å ¥åãæ±ããããããšããããŸãã
ãããã®ã»ããã¢ãã
ãããžã§ã¯ãããªãŒã§ããµã³ãã«ãããžã§ã¯ããã¡ã€ã«clientãpackage.jsonãREADME.mdãserver.jsãåé€ããŸãã ãããã¯å¿ èŠãããŸããããå¿ èŠã«å¿ããŠæ®ãããšãã§ããŸãã
æ°ããNode.js c9ç°å¢ã§ãã¿ãŒããã«ã«ç§»åããŠæ¬¡ã®ããã«å ¥åããŸãã
git clone https://github.com/spences10/twitter-bot-bootstrap
ãããžã§ã¯ãæ§é
ãããžã§ã¯ãç°å¢ããªãŒã¯æ¬¡ã®ããã«ãªããŸãã
ããŒãã®äŸåé¢ä¿
ããããã»ããã¢ããããåã«ãäŸåé¢ä¿ãã€ã³ã¹ããŒã«ããå¿ èŠããããŸãã cd tw *ã³ãã³ãã䜿çšããŠãããžã§ã¯ããã©ã«ããŒã«ç§»åããŸããããã«ãããã/ workspace / twitter-bot-bootstrapïŒmasterïŒ$ã«ç§»åããŸãã
ã¿ãŒããã«ããã次ãå ¥åããŸãã
npm install
ããã«ãããpackage.jsonãã¡ã€ã«ã«ãªã¹ããããŠãããã¹ãŠã®äŸåé¢ä¿ãã€ã³ã¹ããŒã«ãããŸãã ãšã©ãŒãçºçããå Žåã¯ãåãã³ãã³ãã䜿çšããŠpackage.jsonãã¡ã€ã«ããäŸåé¢ä¿ãã€ã³ã¹ããŒã«ããæåŸã«ããã±ãŒãžã®ååãè¿œå ããããšããå§ãããŸãã
package.jsonãã¡ã€ã«ã®äŸåé¢ä¿ã®äŸã次ã«ç€ºããŸãã
"dependencies": { "dotenv": "^4.0.0", "twit": "^2.2.5", "unique-random-array": "^1.0.0", "unirest": "^0.5.1" }
ãããŠããã¹ãŠãã€ã³ã¹ããŒã«ããã³ãã³ãã¯æ¬¡ã®ãšããã§ãã
npm install --save dotenv twit unique-random-array unirest
npm WARN package.json twitter-bot@1.0.0ãªã©ã®èŠåã¡ãã»ãŒãžã衚瀺ãããå Žåããªããžããªãã£ãŒã«ãã¯ãããŸãã-倧äžå€«ã§ãããããã¯ãããå£ãããç¡èŠã§ããŸãã
ããã§ãããããæ§æã§ããŸãã ã¿ãŒããã«ããå ¥åããŸãã
npm init
ããã«ãããpackage.jsonãã¡ã€ã«ãå¿ èŠãªè©³çŽ°ã§ã«ã¹ã¿ãã€ãºã§ããŸãã ããã©ã«ãã®èšå®ãé©åãªå Žåã¯ãåã«ReturnããŒãæŒããŸãã
ããã§ãTwitterããŒã.envãã¡ã€ã«ã«è¿œå ããå¿ èŠããããŸãã é©åãªãã£ãŒã«ãã«å ¥åããŠããã¡ã€ã«ãä¿åããã ãã§ãã
c9ãããžã§ã¯ãã®ãã¡ã€ã«ã®äžã«.envãã¡ã€ã«ãèŠã€ãããªãå Žåã¯ã ãé ããã¡ã€ã«ã衚瀺ããªãã·ã§ã³ãæå¹ã«ããå¿ èŠããããŸãã ãã¡ã€ã«ãã©ãŠã¶ã§ãèšå®ã«ç§»åãã ãé ããã¡ã€ã«ã衚瀺ãè¡ã®æšªã«ããããã¯ã¹ããã§ãã¯ããŸãïŒååšããªãå ŽåïŒã
SENTIMENT_KEYã«ã€ããŠã¯ããªã³ã¯ããæ°ããAPIããŒãååŸã§ããŸããmarket.mashape.com / vivekn / sentiment-3ããŒã¯REQUEST EXAMPLEãã£ãŒã«ãã«ãããŸãã
gifãèŠãŠããªã³ã¯ããã©ãããã°ã€ã³ãŸãã¯mashapeã§æ°ããã¢ã«ãŠã³ããäœæããå³åŽã®ããã«ã§ããŒããã¯ãªãã¯ããŠãããŠã¹ã§ããŒãéžæããŸãã gifäžã§åŒ·èª¿è¡šç€ºãããŠããå¿ èŠãããå Žæã«ããå¿ èŠããããŸãã
ãã®ããŒãä»ã®å šå¡ãšäžç·ã«.envãã¡ã€ã«ã«è¿œå ããŸãã
次ã«ãTwitterãããã®ååãå ¥åãããããïŒãæ®ããŠåæçš¿ããééãååäœã§èšå®ããå¿ èŠããããŸãã
泚ïŒ.envãã¡ã€ã«ã®å€ã¯åŒçšç¬Šã§å²ãŸããŠããŸããã
CONSUMER_KEY=Fw***********P9 CONSUMER_SECRET=TD************Cq ACCESS_TOKEN=31**************UC ACCESS_TOKEN_SECRET=r0************S2 SENTIMENT_KEY=Gj************lF TWITTER_USERNAME=DroidScott TWITTER_RETWEET_RATE=5 TWITTER_FAVORITE_RATE=5
ãã®åŸãæ€çŽ¢å¯Ÿè±¡ã®strings.jsãã¡ã€ã«ãšãµãã¯ãšãªã«ããŒã¯ãŒããè¿œå ã§ããŸãã
ã¯ãšãªãšãµãã¯ãšãªã®ãªã¹ããè¿œå ããŸãã äœãããã©ãã¯ãªã¹ãã«ç»é²ããå Žåã¯ããããã¯ããããªã¯ãšã¹ããæŽæ°ããããšãã§ããŸã
ãµãã¯ãšãªã®ãªã¹ããå ¥åãããšãã¯ãå¿ ããã®åã«ã¹ããŒã¹ãæ®ããŠãã ããã ãªãã§ïŒ ãnode.jsããšçµåããããã«ããªã¯ãšã¹ããhandy tipãã¯ãnode.jshandy tipã§ã¯ãªãnode.js handy tipã®åœ¢åŒãåããŸãã
çè«çã«ã¯ä»¥äžã§ãã ã¿ãŒããã«ã«ç§»åããnpm startãšå ¥åããŸãã 次ã®åºåãåŸãããŸãã
Twitterã¢ã«ãŠã³ãã確èªããŸãã
ã¹ãããïŒ3ïŒHerokuãæ§æãã
ããã§ãéçºç°å¢ã§ãã¹ãã§ããããããã§ããŸããã ãããã圌ãããã«æ®ãããšã¯ã§ããŸãããHerokuã«å±éããå¿ èŠããããŸãã
ãŸã äœæããŠããªãå Žåã¯ãHerokuã§ã¢ã«ãŠã³ãã äœæã ãããã·ã¥ããŒãã®å³äžã®ããããããŠã³ãªã¹ããã[ æ°ããã¢ããªã®äœæ]ãéžæããŸã ã 次ã®ç»é¢ã§ãå¿ èŠã«å¿ããŠã¢ããªã±ãŒã·ã§ã³ã®ååãå ¥åãã[ã¢ããªã®äœæ]ãã¿ã³ãã¯ãªãã¯ããŸãã
ã¢ããªã±ãŒã·ã§ã³ããã·ã¥ããŒããšå±éæ¹æ³ã®æé ã衚瀺ãããŸãã
ã¢ããªã±ãŒã·ã§ã³ã®ååã¯ããã·ã¥ããŒãã®äžéšã«è¡šç€ºãããŸã-ã¢ããªã±ãŒã·ã§ã³ããããã€ããããã«äœ¿çšããHerokuã³ãã³ãã©ã€ã³ã€ã³ã¿ãŒãã§ã€ã¹ã䜿çšããŠHerokuã«ãã°ã€ã³ãããšãã«å¿ èŠã«ãªããŸãã
Heroku CLI
æåã«ãHerokuã³ãã³ãã©ã€ã³ã€ã³ã¿ãŒãã§ã€ã¹ïŒCLIïŒã䜿çšããŠãããã€ããŸãã c9ã¿ãŒããã«ã䜿çšããŠHerokuã«ãã°ã€ã³ããŸãïŒããã©ã«ãã§ã€ã³ã¹ããŒã«ãããŸãïŒã
heroku login
è³æ Œæ å ±ãå ¥åããŠãã ããïŒ
cd twitter-bot-bootstrap git init heroku git:remote -a your-heroku-app-name
ã¢ããªã±ãŒã·ã§ã³ããããã€ããŸãã
git add . git commit -am 'make it better' git push heroku master
ã¿ãŒããã«ã§ååŸãããã«ãåºåã¯æ¬¡ã®ãšããã§ãã
次ã®ã³ãã³ãã§åºåã確èªããŸãã
heroku logs -t
ããŸããããŸãããïŒ ãã£ãããïŒ
Herokuå€æ°ã®èšå®
ããããHerokuäžã«ããã®ã§ãTwitterããŒãä¿åããããã®ç°å¢å€æ°ãè¿œå ããå¿ èŠããããŸãã å®éã«ã¯ãããããèšé²ãããŠãã.envãã¡ã€ã«ã¯.gitignoreãã¡ã€ã«ã«ç€ºãããŠããããªã¹ãããããã¡ã€ã«ãHerokuã«ã¢ããããŒãããªãããã«ã³ãã³ããgitã«éä¿¡ãããŸãã
ãããã£ãŠãå°æ¥GitHubã«ã³ãŒããé 眮ããå ŽåãããŒã.envãã¡ã€ã«ãšãšãã«å ¬éãããããšãå¿é ããå¿ èŠã¯ãããŸãããèªåçã«é€å€ãããŸãã
å¿ èŠãªã®ã¯ãHerokuã®ã¢ããªã±ãŒã·ã§ã³ã®ã³ã³ãœãŒã«ã«ç§»åãã[èšå®]ã»ã¯ã·ã§ã³ãéžæããŠã.envãã¡ã€ã«ããTwitterããŒãè¿œå ããããšã ãã§ãã 次ã«ãã æ§æå€æ°ã®è¡šç€ºããã¿ã³ãã¯ãªãã¯ããŠãé©åãªå€ãæã€å€æ°ãè¿œå ããŸãã
CONSUMER_KEY CONSUMER_SECRET ACCESS_TOKEN ACCESS_TOKEN_SECRET
Herokuã§å€æ°ãèšå®ããããçŸåšã®ãããžã§ã¯ãã®config.jsãã¡ã€ã«ã«ç§»åããŸãã 次ã®è¡ãåé€ããå¿ èŠããããŸãã
require('dotenv').config();
ããã§ãHerokuãå床ãããã€ããæºåããã¹ãŠæŽããŸããã ã³ã³ãœãŒã«ã®ã³ãã³ãã¯æ¬¡ã®ããã«ãªããŸãã
$ git add . $ git commit -m 'add environment variables' $ git push heroku master
次ã«ãã³ãã³ãã§Herokuã®ãã°ã確èªã§ããŸã
$ heroku logs -t
å®äºïŒæ©èœãæ°žä¹ ã«å®è¡ãããããããããŸãã ãŸãããŸãã¯æ€çŽ¢æ¡ä»¶ãå€æŽããããšã決å®ãããŸã§ã
GitHubã§Herokuããããã€ãã
GitHubã«æ¥ç¶ããŠã¢ããªã±ãŒã·ã§ã³ããããã€ããGitHubã§ãã¹ã¿ãŒãã©ã³ããæŽæ°ããããã³ã«Herokuã«èªåçã«ãããã€ããããšãã§ããŸãã ããã§ã¯ãã¹ãŠãéæã§ãã
Herokuã®Deployããã«ã«ç§»åããGitHubã¢ã«ãŠã³ããHerokuã¢ã«ãŠã³ãã«ãªã³ã¯ãããŠããå ŽåãGitHubã¢ã«ãŠã³ããHerokuã¢ã«ãŠã³ãã«ãªã³ã¯ãããŠããå ŽåãGitHubãéžæããŸãããããã£ãŠããã®ãªããžããªãã³ããŒããå Žåã¯ãtwitter-bot-bootstrapãå ¥åããŠã¯ãªãã¯ããã ãã§ãæ€çŽ¢ ã次ã«[ æ¥ç¶ ]ãã¿ã³ãã¯ãªãã¯ããŸã-ããã ãã§ãGitHubããèªåçã«ãããã€ã§ããŸãã
Herokuã®ãã©ãã«ã·ã¥ãŒãã£ã³ã°
ãå£ããããšã¯ã©ãããæå³ã§ããïŒïŒ
äžè¬ã«ãã¯ãŒã«ãŒã¯WebãšããŠå®çŸ©ãããããšããããã¯ã©ãã·ã¥ã«ã€ãªããããšããããŸãã ã¯ãŒã«ãŒã®èšå®ã次ã®ããã«å€æŽããŠã¿ãŠãã ããã
heroku ps:scale worker=0 heroku ps:scale worker=1
ããã§ãããŸããããªãå Žåã¯ã[ãªãœãŒã¹]ã¿ãã«ç§»åããããã§èšå®ãè©ŠããŸãã ããã£ãããã«ãwebãherokuãworkerã®ã¹ã©ã€ããŒãåãããšãååãšããŠãã¹ãŠãæ£åžžã«æ»ããŸãã äžçªäžã®è¡ã¯ãDynoã¯ãŒã«ãŒãã¢ã¯ãã£ãã«ããå¿ èŠãããããšã§ããã·ã¹ãã ã代ããã«ä»ã®ãªãœãŒã¹ã䜿çšããããšããããããšã©ãŒR10ïŒããŒãã¿ã€ã ã¢ãŠãïŒãšã©ãŒãçºçããŸãã
Herokuã®å¥ã®äŸ¿å©ãªã³ãã³ãã次ã«ç€ºããŸãã
heroku restart
ããã©ã«ãã§ã¯ãéçºãã©ã³ãïŒããšãã°ãdevïŒã§äœæ¥ããå Žåãã¡ã€ã³ãã©ã³ãã®ã¿ãããã·ã¥ã§ããŸãã Herokuã§ãã¹ãããå Žåã¯ã次ã®ã³ãã³ãã䜿çšã§ããŸãã
git push heroku dev:master
圹ç«ã€ã¢ããã€ã¹
ããããã¹ãŠãGitHubãªããžããªã«è¿œå ãããããããŒãäžçãšå ±æããããªãå Žåã¯ã.envãã¡ã€ã«ã®è¿œè·¡ããªãã«ããŠãã ããã 端æ«ããã³ãã³ããå ¥åããŸãã
$ git update-index --assume-unchanged .env
æããã䜿çšããã³ãã³ãã¯ãããã§èŠç¹ã説æããŸãã ã
ãããã«
ããã§ãTwitterãããã皌åããŠããŸãã å°æ¥çã«ã¯ãåŒãç¶ãäœæ¥ããŠèšå®ãå€æŽã§ããŸãã
ã³ããŒãããŠã³ããŒãããŠãã«ãªã¯ãšã¹ãã䜿çšããŠè²¢ç®ãããå Žåã®ãªããžããªãžã®ãªã³ã¯ã次ã«ç€ºããŸãã è²¢ç®ã倧å°ãåãã-è¿œå æ©èœããã°ä¿®æ£ãçµ±åãã¹ã-æè¿ããŸãããæ éã«æ€èšããè°è«ããŸãã
è¬èŸ
ã€ã³ã¹ãã¬ãŒã·ã§ã³ãåŸãããã«ã @ amanhimselfãšåœŒèªèº«ã®Twitterãããã®äœæã«é¢ããæçš¿ã«æè¬ããå¿ èŠããããŸãã
â ã·ã³ãã«ãªtwitter-bot-with-node-jsã®äœæ
â ããŠããŒ-to-make-a-twitter-bot-with-nodejs
â twitter-mctwitbot
â awesome-twitter-bots
ãŸãã䟿å©ãªTwitterãããã«é¢ããä»ã®æçš¿ïŒ
â www.brit.co/twitter-bots-to-follow
â www.hongkiat.com/using-twitter-bots