forked from nomlab/GN-B4-Exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TwitterBot.rb
62 lines (51 loc) · 1.51 KB
/
TwitterBot.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
# -*- coding: utf-8 -*-
##########################################################################
# 【GNグループ新人研修課題】RubyによるTwitterBotプログラムの作成
#
# ファイルの内容: TwitterBotの基本的な挙動を記述
#
# 作成者: 配布物
#
##########################################################################
require 'oauth'
require 'json'
require 'yaml'
#--------- TwitterBot ---------
class TwitterBot
def initialize
yml_data = YAML.load_file('./setting.yml')
@consumer = OAuth::Consumer.new(
@CONSUMER_KEY = yml_data["consumer_key"],
@CONSUMER_SECRET = yml_data["consumer_secret"],
:site => 'https://api.twitter.com/1.1'
)
@access_token = OAuth::AccessToken.new(
@consumer,
@ACCESS_TOKEN = yml_data["access_token"],
@ACCESS_TOKEN_SECRET = yml_data["access_token_secret"]
)
end
#--------- ツイート---------
def tweet( message )
@access_token.post(
'/statuses/update.json',
'status' => message
)
end
#--------- ツイート受信 <+0000の時刻,発言内容,発言者> ---------
def get_tweet
response = @access_token.get(
'/statuses/home_timeline.json'
)
tweet_resource = JSON.parse(response.body)
tweets = Array.new
tweet_resource.each do |tr|
tweets << {
"user_id" => tr["user"]["screen_name"],
"user_name" => tr["user"]["name"],
"message" => tr["text"],
"time" => tr["created_at"] }
end
return tweets
end
end