This repository has been archived by the owner on Mar 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathngskbot.rb
64 lines (54 loc) · 1.8 KB
/
ngskbot.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/local/bin/ruby
# -*- coding: utf-8 -*-
=begin
ngskbot の本体。
userstream でリプを拾って返事をします。
userstream からの応答がしばらくないと終了するので、正常終了時にもう一つngskbotを起動するというちょっとアレな方法で対処してます。
=end
# デバッグするかどうか。
# trueならtwitterにポストせずに標準出力に出すだけ
DEBUG_FLG = true
# 各種ファイルのパス
FILES_PATH = gets.chomp
dir = File.dirname File.expand_path(__FILE__)
require 'pp'
require 'user_stream'
require "#{dir}/message_check.rb"
require "#{dir}/tweet.rb"
# 標準入力からトークン、botname,username(複数可)を取得
tokens = {
"consumer_key" => gets.chomp,
"consumer_secret" => gets.chomp,
"oauth_token" => gets.chomp,
"oauth_token_secret" => gets.chomp
}
bot_name = gets.chomp
user_name = []
while true
tmp = gets.chomp
break if tmp.empty?
user_name.push tmp
end
# UserStreamに認証
UserStream.configure do |config|
config.consumer_key = tokens["consumer_key"]
config.consumer_secret = tokens["consumer_secret"]
config.oauth_token = tokens["oauth_token"]
config.oauth_token_secret = tokens["oauth_token_secret"]
end
# ツイート関連用クラス作成
twitter = Tweet.new tokens,FILES_PATH,DEBUG_FLG
# チェック用クラス生成
check = MessageCheck.new bot_name,user_name
# UserStream 作成
client = UserStream.client
# UserStream で tweet を受信
client.user do |status|
if status.has_key? "text"
# tweetが流れてきたらcheckしてreplyを返す
twitter.tweet_reply(status,check.format_check(status))
end
end
# 再起動
# このファイルと同じディレクトリに起動用の run_ngskbot.rb がある前提。
exec "ruby #{File.dirname File.expand_path(__FILE__)}/run_ngskbot.rb"