2015-07-02 15:13:04 +00:00
|
|
|
# Copyright (C) 2012-2015 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2015-12-14 09:23:14 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
fetch tweets from twitter account
|
|
|
|
|
|
|
|
options = {
|
|
|
|
adapter: 'twitter',
|
|
|
|
auth: {
|
|
|
|
consumer_key: consumer_key,
|
|
|
|
consumer_secret: consumer_secret,
|
|
|
|
oauth_token: armin_theo_token,
|
|
|
|
oauth_token_secret: armin_theo_token_secret,
|
|
|
|
},
|
|
|
|
sync: {
|
|
|
|
search: [
|
|
|
|
{
|
|
|
|
term: '#citheo42',
|
|
|
|
group_id: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
term: '#citheo24',
|
|
|
|
group_id: 1,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
mentions: {
|
|
|
|
group_id: 2,
|
|
|
|
},
|
|
|
|
direct_messages: {
|
|
|
|
group_id: 2,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
instance = Channel::Driver::Twitter.new
|
|
|
|
result = instance.fetch(options, channel)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
{
|
|
|
|
result: 'ok',
|
|
|
|
}
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2015-08-28 00:53:14 +00:00
|
|
|
class Channel::Driver::Twitter
|
2015-07-02 15:13:04 +00:00
|
|
|
|
2015-12-14 09:23:14 +00:00
|
|
|
def fetch (options, channel)
|
2015-07-02 15:13:04 +00:00
|
|
|
|
2015-12-14 09:23:14 +00:00
|
|
|
@tweet = Tweet.new(options[:auth])
|
|
|
|
@sync = options[:sync]
|
2015-07-02 15:13:04 +00:00
|
|
|
@channel = channel
|
|
|
|
|
2015-07-03 09:42:54 +00:00
|
|
|
Rails.logger.debug 'twitter fetch started'
|
2015-07-02 15:13:04 +00:00
|
|
|
|
|
|
|
fetch_search
|
|
|
|
fetch_mentions
|
|
|
|
fetch_direct_messages
|
|
|
|
|
|
|
|
disconnect
|
|
|
|
|
|
|
|
Rails.logger.debug 'twitter fetch completed'
|
2015-12-14 09:23:14 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
result: 'ok',
|
|
|
|
}
|
2015-07-02 15:13:04 +00:00
|
|
|
end
|
|
|
|
|
2015-12-14 09:23:14 +00:00
|
|
|
=begin
|
2015-07-02 15:13:04 +00:00
|
|
|
|
2015-12-14 09:23:14 +00:00
|
|
|
instance = Channel::Driver::Twitter.new
|
|
|
|
instance.send(
|
|
|
|
{
|
|
|
|
adapter: 'twitter',
|
|
|
|
auth: {
|
|
|
|
consumer_key: consumer_key,
|
|
|
|
consumer_secret: consumer_secret,
|
|
|
|
oauth_token: armin_theo_token,
|
|
|
|
oauth_token_secret: armin_theo_token_secret,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
twitter_attributes,
|
|
|
|
notification
|
|
|
|
)
|
2015-07-02 15:13:04 +00:00
|
|
|
|
2015-12-14 09:23:14 +00:00
|
|
|
=end
|
2015-07-02 15:13:04 +00:00
|
|
|
|
2015-12-14 09:23:14 +00:00
|
|
|
def send(options, article, _notification = false)
|
|
|
|
|
|
|
|
# return if we run import mode
|
|
|
|
return if Setting.get('import_mode')
|
|
|
|
|
|
|
|
@tweet = Tweet.new(options[:auth])
|
|
|
|
tweet = @tweet.from_article(article)
|
|
|
|
disconnect
|
2015-07-02 15:13:04 +00:00
|
|
|
tweet
|
|
|
|
end
|
|
|
|
|
|
|
|
def disconnect
|
|
|
|
@tweet.disconnect
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def fetch_search
|
|
|
|
|
|
|
|
return if !@sync[:search]
|
2015-07-03 06:55:59 +00:00
|
|
|
return if @sync[:search].empty?
|
2015-07-02 15:13:04 +00:00
|
|
|
|
|
|
|
# search results
|
|
|
|
@sync[:search].each { |search|
|
|
|
|
|
|
|
|
result_type = search[:type] || 'mixed'
|
|
|
|
|
|
|
|
Rails.logger.debug " - searching for '#{search[:term]}'"
|
|
|
|
|
|
|
|
counter = 0
|
2015-12-13 12:08:58 +00:00
|
|
|
@tweet.client.search(search[:term], result_type: result_type).collect { |tweet|
|
2015-07-02 15:13:04 +00:00
|
|
|
|
|
|
|
break if search[:limit] && search[:limit] <= counter
|
2015-12-13 12:08:58 +00:00
|
|
|
break if Ticket::Article.find_by(message_id: tweet.id)
|
2015-07-02 15:13:04 +00:00
|
|
|
|
2015-12-14 09:23:14 +00:00
|
|
|
@tweet.to_group(tweet, search[:group_id], @channel)
|
2015-07-02 15:13:04 +00:00
|
|
|
|
|
|
|
counter += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_mentions
|
|
|
|
|
|
|
|
return if !@sync[:mentions]
|
2015-07-03 06:55:59 +00:00
|
|
|
return if @sync[:mentions].empty?
|
2015-07-02 15:13:04 +00:00
|
|
|
|
|
|
|
Rails.logger.debug ' - searching for mentions'
|
|
|
|
|
|
|
|
counter = 0
|
|
|
|
@tweet.client.mentions_timeline.each { |tweet|
|
|
|
|
|
|
|
|
break if @sync[:mentions][:limit] && @sync[:mentions][:limit] <= counter
|
2015-12-13 12:08:58 +00:00
|
|
|
break if Ticket::Article.find_by(message_id: tweet.id)
|
2015-07-02 15:13:04 +00:00
|
|
|
|
2015-12-14 09:23:14 +00:00
|
|
|
@tweet.to_group(tweet, @sync[:mentions][:group_id], @channel)
|
2015-07-02 15:13:04 +00:00
|
|
|
|
|
|
|
counter += 1
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_direct_messages
|
|
|
|
|
|
|
|
return if !@sync[:direct_messages]
|
2015-07-03 06:55:59 +00:00
|
|
|
return if @sync[:direct_messages].empty?
|
2015-07-02 15:13:04 +00:00
|
|
|
|
|
|
|
Rails.logger.debug ' - searching for direct_messages'
|
|
|
|
|
|
|
|
counter = 0
|
|
|
|
@tweet.client.direct_messages.each { |tweet|
|
|
|
|
|
|
|
|
break if @sync[:direct_messages][:limit] && @sync[:direct_messages][:limit] <= counter
|
2015-12-13 12:08:58 +00:00
|
|
|
break if Ticket::Article.find_by(message_id: tweet.id)
|
2015-07-02 15:13:04 +00:00
|
|
|
|
2015-12-14 09:23:14 +00:00
|
|
|
@tweet.to_group(tweet, @sync[:direct_messages][:group_id], @channel)
|
2015-07-02 15:13:04 +00:00
|
|
|
|
|
|
|
counter += 1
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|