Fix tweet event handling
This commit is contained in:
parent
e646e24744
commit
746d218896
1 changed files with 32 additions and 35 deletions
|
@ -112,50 +112,48 @@ module Ebooks
|
||||||
next if ev.sender.screen_name == @username # Don't reply to self
|
next if ev.sender.screen_name == @username # Don't reply to self
|
||||||
log "DM from @#{ev.sender.screen_name}: #{ev.text}"
|
log "DM from @#{ev.sender.screen_name}: #{ev.text}"
|
||||||
@on_message.call(ev) if @on_message
|
@on_message.call(ev) if @on_message
|
||||||
end
|
elsif ev.respond_to?(:name) && ev.name == :follow
|
||||||
|
|
||||||
next unless ev.respond_to? :name
|
|
||||||
|
|
||||||
if ev.name == :follow
|
|
||||||
next if ev.source.screen_name == @username
|
next if ev.source.screen_name == @username
|
||||||
log "Followed by #{ev.source.screen_name}"
|
log "Followed by #{ev.source.screen_name}"
|
||||||
@on_follow.call(ev.source) if @on_follow
|
@on_follow.call(ev.source) if @on_follow
|
||||||
end
|
elsif ev.is_a? Twitter::Tweet
|
||||||
|
next unless ev.text # If it's not a text-containing tweet, ignore it
|
||||||
|
next if ev.user.screen_name == @username # Ignore our own tweets
|
||||||
|
|
||||||
next unless ev.text # If it's not a text-containing tweet, ignore it
|
meta = {}
|
||||||
next if ev.user.screen_name == @username # Ignore our own tweets
|
mentions = ev.attrs[:entities][:user_mentions].map { |x| x[:screen_name] }
|
||||||
|
|
||||||
meta = {}
|
reply_mentions = mentions.reject { |m| m.downcase == @username.downcase }
|
||||||
mentions = ev.attrs[:entities][:user_mentions].map { |x| x[:screen_name] }
|
reply_mentions = [ev.user.screen_name] + reply_mentions
|
||||||
|
|
||||||
reply_mentions = mentions.reject { |m| m.downcase == @username.downcase }
|
meta[:reply_prefix] = reply_mentions.uniq.map { |m| '@'+m }.join(' ') + ' '
|
||||||
reply_mentions = [ev.user.screen_name] + reply_mentions
|
meta[:limit] = 140 - meta[:reply_prefix].length
|
||||||
|
|
||||||
meta[:reply_prefix] = reply_mentions.uniq.map { |m| '@'+m }.join(' ') + ' '
|
mless = ev.text
|
||||||
meta[:limit] = 140 - meta[:reply_prefix].length
|
begin
|
||||||
|
ev.attrs[:entities][:user_mentions].reverse.each do |entity|
|
||||||
mless = ev.text
|
last = mless[entity[:indices][1]..-1]||''
|
||||||
begin
|
mless = mless[0...entity[:indices][0]] + last.strip
|
||||||
ev.attrs[:entities][:user_mentions].reverse.each do |entity|
|
end
|
||||||
last = mless[entity[:indices][1]..-1]||''
|
rescue Exception
|
||||||
mless = mless[0...entity[:indices][0]] + last.strip
|
p ev.attrs[:entities][:user_mentions]
|
||||||
|
p ev.text
|
||||||
|
raise
|
||||||
end
|
end
|
||||||
rescue Exception
|
meta[:mentionless] = mless
|
||||||
p ev.attrs[:entities][:user_mentions]
|
|
||||||
p ev.text
|
|
||||||
raise
|
|
||||||
end
|
|
||||||
meta[:mentionless] = mless
|
|
||||||
|
|
||||||
# To check if this is a mention, ensure:
|
# To check if this is a mention, ensure:
|
||||||
# - The tweet mentions list contains our username
|
# - The tweet mentions list contains our username
|
||||||
# - The tweet is not being retweeted by somebody else
|
# - The tweet is not being retweeted by somebody else
|
||||||
# - Or soft-retweeted by somebody else
|
# - Or soft-retweeted by somebody else
|
||||||
if mentions.map(&:downcase).include?(@username.downcase) && !ev.retweeted_status? && !ev.text.start_with?('RT ')
|
if mentions.map(&:downcase).include?(@username.downcase) && !ev.retweeted_status? && !ev.text.start_with?('RT ')
|
||||||
log "Mention from @#{ev.user.screen_name}: #{ev.text}"
|
log "Mention from @#{ev.user.screen_name}: #{ev.text}"
|
||||||
@on_mention.call(ev, meta) if @on_mention
|
@on_mention.call(ev, meta) if @on_mention
|
||||||
|
else
|
||||||
|
@on_timeline.call(ev, meta) if @on_timeline
|
||||||
|
end
|
||||||
else
|
else
|
||||||
@on_timeline.call(ev, meta) if @on_timeline
|
log "Unsure how to handle event: ", ev
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -191,7 +189,6 @@ module Ebooks
|
||||||
# Reply to a tweet or a DM.
|
# Reply to a tweet or a DM.
|
||||||
# Applies configurable @reply_delay range
|
# Applies configurable @reply_delay range
|
||||||
def reply(ev, text, opts={})
|
def reply(ev, text, opts={})
|
||||||
p "reply???"
|
|
||||||
opts = opts.clone
|
opts = opts.clone
|
||||||
|
|
||||||
if ev.is_a? Twitter::DirectMessage
|
if ev.is_a? Twitter::DirectMessage
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue