Skip to content

Instantly share code, notes, and snippets.

@ahoward
Created May 12, 2014 19:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahoward/d31fc57067a15c0387bf to your computer and use it in GitHub Desktop.
Save ahoward/d31fc57067a15c0387bf to your computer and use it in GitHub Desktop.
dojo4's custom email and txt commands for https://github.com/ahoward/fbomb
FBomb {
# use the .need command when you really need someone. it'll email and txt msg
# them from flowdock!
#
command(:need){
help "/need username (some optional message)"
call do |*args|
username = args.shift.to_s.strip
message = args.join(' ').squeeze(' ').strip
name, info = Roboto.directory.lookup(username)
user = FBomb::Command.user
emailed, texted = nil
catch(:done){
#
if name.nil? or info.nil? or info['phone'].nil? or info['email'].nil?
speak "sorry, incomplete info for #{ username }"
throw(:done)
end
#
speak "hey @#{ name } - #{ message } (#{ user.name } needs you!)"
#
email = info['email']
subject = "#{ user.name } needs you!"
body = message
begin
Roboto.email(
:to => email,
:subject => subject,
:body => body
)
emailed = :emailed
rescue Object => e
end
#
phone = info['phone']
body = "#{ message } (#{ user.name } needs you!)"
begin
Roboto.txt(
:to => phone,
:body => body
)
texted = :texted
rescue Object => e
end
#
paste "@#{ user.name } roboto just #{ [emailed, texted].join(' and ') } #{ name }"
}
end
}
# list directory deets for all configured users
#
command(:directory){
help "lists people's deets"
call do |*args|
list =
Roboto.directory.map do |name, config|
info = config.to_a.map{|k, v| "#{ k }=#{ v }"}.join(', ')
"#{ name } #=> #{ info }"
end
directory = list.join("\n")
paste(directory)
end
}
# text msg yo ass
#
command(:txt){
help "/txt username (some optional < 140 char message)"
call do |*args|
username = args.shift.to_s.strip
message = args.join(' ').squeeze(' ').strip
user = FBomb::Command.user
src_name, src_info = Roboto.directory.lookup(user.name.strip.split(/\s+/).first)
dst_name, dst_info = Roboto.directory.lookup(username)
catch(:done){
[ [src_name, src_info], [dst_name, dst_info] ].each do |pair|
name, info = pair
if name.nil? or info.nil? or info['phone'].nil? or info['email'].nil?
speak "sorry, incomplete info for #{ username }"
throw(:done)
end
end
dst_phone = dst_info['phone']
src_phone = src_info['phone']
body = "#{ message } - #{ src_name } #{ src_phone }"
begin
Roboto.txt(
:to => dst_phone,
:body => body
)
paste "txt'd #{ dst_name } #=> #{ body }"
rescue Object => e
paste "fubar'd txt'ing #{ dst_name }"
end
}
end
}
}
# clean? maybe not, but better than global methods ;-)
module Roboto
def email(options, &block)
#
require 'mail' unless defined?(Mail)
#
mail = Mail.new
#
mail.delivery_method(:smtp,
:address => 'smtp.gmail.com',
:domain => 'foo.com',
:port => 587,
:user_name => 'foo@bar.com',
:password => 'passwd',
:authentication => 'plain',
:enable_starttls_auto => true
)
#
mail.from = 'foo@bar.com'
mail.to = options[:to]
mail.subject = options[:subject]
mail.body = options[:body]
#
mail.deliver
end
def txt(options = {}, &block)
#
require 'twilio-ruby' unless defined?(Twilio)
#
account_sid = '0123456789'
auth_token = '0123456789'
#
client = Twilio::REST::Client.new(account_sid, auth_token)
#
to = options[:to]
body = Array(options[:body]).join(' ')[0, 140]
#
client.account.messages.create(
:from => '+0123456789',
:to => to,
:body => body
)
end
DIRECTORY = <<-__
Ara :
phone : +0123456789
email : a@foo.com
Corey :
phone : +0123456789
email: c@foo.com
Jetha :
phone : +0123456789
email: j@foo.com
Steve :
phone : +0123456789
email: s@foo.com
Anthony :
phone : +0123456789
email: a@foo.com
Peter :
phone : +0123456789
email: p@foo.com
Lorena :
phone : +0123456789
email: l@foo.com
Miles :
phone : +0123456789
email: m@foo.com
Noah :
phone : +0123456789
email: n@foo.com
Spike :
phone : +0123456789
email: s@foo.com
Jeremy :
phone : +0123456789
email: j@foo.com
Kerry :
phone: +0123456789
email: k@foo.com
Michael :
phone: +0123456789
email: m@foo.com
Garett :
phone : +0123456789
email: g@foo.com
Ryan :
phone : +0123456789
email: r@foo.com
__
Directory = YAML.load(DIRECTORY)
def directory
Directory
end
def Directory.lookup(username)
re = %r/#{ username.to_s.scan(/[\d\w\s]+/).join(' ').gsub(/\s+/, '\s+') }/i
name = nil
info = nil
Directory.each do |n, i|
if n =~ re
name, info = n, i
break
end
end
[name, info]
end
extend(Roboto)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment