Skip to content

Instantly share code, notes, and snippets.

@memetor
Last active December 23, 2016 11:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save memetor/1622192b3456a34663b993c9d6da1284 to your computer and use it in GitHub Desktop.
Save memetor/1622192b3456a34663b993c9d6da1284 to your computer and use it in GitHub Desktop.
sample bot
require 'tw'
require 'json'
require 'ostruct'
# id, user, text, time, fav_count, rt_count
tweet = OpenStruct.new JSON.parse STDIN.gets
/^(@.+?)\s(.+)/ =~ tweet.text
user = $1
body = $2
abort 'invalid mention' if /@/ =~ body
dice = body
.split(/[[:space:]]/)
.select {|x| x.length > 0 }
.map {|x| x[0]}
.reject {|x|
(/[[:blank:]]/ =~ x) \
|| (/[[:cntrl:]]/ =~ x) \
|| (/[[:space:]]/ =~ x) \
|| (/[\/'"]/ =~ x)
}
.map {|x| [x, rand(100) + 1]}
.sort_by {|x| x[1] * -1}
.map {|x| x.join(' ')}
.slice(0, 10)
.join("\n")
message = [["@#{tweet.user}", 'にゃ〜ん'].join(' '), dice].join("\n")
client = Tw::Client.new
client.auth
client.tweet message, in_reply_to_status_id: tweet.id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment