Skip to content

Instantly share code, notes, and snippets.

@aytch
Created January 18, 2013 07:50
Show Gist options
  • Select an option

  • Save aytch/4563011 to your computer and use it in GitHub Desktop.

Select an option

Save aytch/4563011 to your computer and use it in GitHub Desktop.
IRC bot using Cinch, plus a twitter link parser and join/part functions. This is ugly.
require 'cinch'
require 'cinch/plugins/AccidentalHaiku'
require 'cinch/plugins/CleverBot'
require 'Nokogiri'
require 'open-uri'
bot = Cinch::Bot.new do
configure do |c|
c.server = "irc.freenode.org"
#c.port = "7667"
c.channels = ["#reddit-sysadmin"]
c.nick = "ercbot"
# This is where the admin is declared in the example code
@@admin = "aytch"
c.plugins.plugins = [Cinch::Plugins::CleverBot, AccidentalHaiku]
end
on :message, "hello" do |m|
m.reply "Hello, #{m.user}!"
end
helpers do
def is_admin?(user)
# @admin = "b|work"
true if user.nick == @@admin
end
end
# current regex string matches too much stuff, though it doesn't break.
# Trying https?:\/\/twitter\.com\/(?:#!\/)?(\w+)\/status(es)?\/(\d+) instead
on :message, /(\b(https?):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/i do |m, message|
#on :message, /https?:\/\/twitter\.com\/(?:#!\/)?(\w+)\/status(es)?\/(\d+)/i do |m, message|
url = message.to_s
doc = Nokogiri::HTML(open(url))
user = doc.at_css("span.username.js-action-profile-name b").text
status = doc.xpath("//div[3]/div/div/div/div/p").text[/\S(.+)/]
message = "Twitter | @#{user}: #{status}"
m.reply message
end
on :message, /^!join (.+)/ do |m, channel|
bot.join(channel) if is_admin?(m.user)
end
on :message, /^!part (.+)/ do |m, channel|
channel = channel || m.channel
if channel
bot.part(channel) if is_admin?(m.user)
end
end
###
end
bot.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment