Slack 用の bot を Hubot で作成したことを書いた。
CentOS + hubot で Slack に指定時刻に自動発言する – with a Christian Wife
https://blog.withachristianwife.com/2018/02/11/auto-posting-on-slack-with-hubot-and-centos/
次は Twitter bot を作成してみたい。環境は VPS 上の CentOS 7.1 。
hubot や ruby を使う方法をいろいろ試してみたが、できたのは PHP を使う方法なので、それを紹介する。あらかじめ設定した定型文をランダムにツイートする。
(1)PHP をインストールする。
# yum install php
インストールの確認。
$ rpm -qa php
(2)適当なディレクトリ(twitter-bot とする)を作成。ディレクトリへ移動。
$ mkdir twitter-bot
$ cd twitter-bot
(3)TwistOauth.phar をダウンロード。
$ wget https://github.com/mpyw/TwistOAuth/raw/master/build/TwistOAuth.phar
(4)以下のページで consumer key、consumer secret, access token、access token secret を取得する。
Twitter Application Management
https://apps.twitter.com/
(5)twitter-bot.php というファイルを作成し、以下のように記述。4で取得したコードも記入する。
(6)list.txt というツイート用のテキストを作成。1行ごとに文章を記入。ここからランダムにツイートされる。
(7)試しに実行。ツイートされるはずだ。
$ php twitter-bot.php
cron に登録すれば自動的にツイートできるようになる。
参考:
2分でわかる!LinuxのコマンドラインでPHPを実行するphpコマンドの使い方
https://eng-entrance.com/linux-php-operate
自動ランダムツイートするTwitter BOTをPHPとさくらインターネットで作る
https://montblan9.net/archives/2537