読者です 読者をやめる 読者になる 読者になる

ソムトル

Webアプリ系の勉強をし始めたのでそのメモなどを。突っ込み大歓迎です。英語に慣れるためにひどい英語も書いていきます。

twitter bot by node.js + mysql on mac

I made twitter bot by node.js + mysql.
but, my knowledge for web application is too short.
so, you can not trust this article.

Preparation

node.js

if gcc is Installed in mac, use below shell script.

Install npm

node.js use modules that is like saying a library in C.
Modules is installed by npm that is package manager of node.js.
if shell script installing node.js works well, npm is installed together.

install modules to use bot

install mysql

The bot tweets wisdom in database.
Because Using mysql for database, install a 'mysql' module to use mysql from node.js.
To pathing to dir installed node.js, write below code to ~/.bash_profile.

export PATH=$HOME/local/bin:$PATH

then, run below code.

source .bash_profile
npm install mysql

install oauth

The bot use TwitterAPI that needs OAuth.
So, install 'oauth' module to use easily oauth.

npm install oauth

Twitter

create acount

Using a alias of gmail is suitable to create a bot acount.
if you have hoge@gmail.com, you can get new address hoge+xxxx@gmail.com.

get infomations needed twitterAPI.

Register the application on Twitter, to get the information you need to OAuth= consumer key, consumer secret, access token and access token secret.

1.move to https://dev.twitter.com/
2.mode to my application from right-up
3.click "Create a new application"
4.Fill in the appropriate fields. and, regist a app.
5.select app that is made by 4. from "my applications".
6.change Application Type to Read and Write.
7. click "create access token" at your access token.
8.reload several times, you can see your Access token, Access token secret.
9.you can get Consumer key, Consumer secret, Access token, Access token secret.

mysql

install

Using port, install mysql to mac.

sudo port install mysql5-server

if successed, you can see below message.

If this is a new install, in order to setup the database you might want to run:
sudo -u _mysql mysql_install_db5

so, run below code.

sudo -u _mysql mysql_install_db5

create database

if you want to use japanese, write these codes in /etc/my.cnf.

[mysqld]
default-character-set=utf8

[mysql]
default-character-set=utf8

[mysqldump]
default-character-set=utf8

create database.

#sudo /opt/local/share/mysql5/mysql/mysql.server start
#mysql5 -u root
#create NAME OF DATABASE
#create table NAME OF TABLE
->(name_of_id_column BIGINT PRIMARY KEY AUTO_INCREMENT,
->name_of_wisdom_column VARCHAR(256) UNIQUE NOT NULL);
#insert into wisdom VALUES(null, 'wisdom you like');

execute

Download displayed next.
you have to chenge variables wrote by japanese to suitable value.
then, run below.

node tweet_wisdom_bot.js

code

crontab

if you want to run interval, use crontab.
this is example that interval 1 hour.

crontab -e
00 * * * * ~/local/bin/node ~/tweet_wisdom_bot.js  

sample

https://twitter.com/#!/grasshopper_bot (tweet only japanese)