module Sinatra
module CommandsEditContact
# ------------------------------------------------------------------------
# => SEPARATED CODE USED TO EDIT CONTACTS
# ------------------------------------------------------------------------
def view_and_edit client, event
puts event
puts "Formatted Text: #{event.formatted_text}"
return if event.formatted_text.nil?
is_admin = is_admin_or_owner client, event
if event.formatted_text.include? "help"
client.chat_postMessage(channel: event.channel, text: "Nobody can help you now.", as_user: true)
client.chat_postMessage(channel: event.channel, text: "I'm only having a laugh. Try typing `add`, `view`, or `when did I last contact Alfred?`.", as_user: true)
client.chat_postMessage(channel: event.channel, text: "If you've had enough of me, you can also `dismiss` me.", as_user: true)
return true
# View contacts
elsif event.formatted_text.starts_with? "view"
# print the list
contact_list = Contact.all
contact = ""
contact_list.each_with_index do |item, index|
contact += "#{ index+ 1 }. #{ item.name } \n"
end
client.chat_postMessage(channel: event.channel, text: "Here are all your contacts.\n" + contact , as_user: true )
client.chat_postMessage(channel: event.channel, text: "Type `delete contact` followed by a number to delete a contact from this list.\n" , as_user: true )
client.chat_postMessage(channel: event.channel, text: "Type `add` plus the First and Last name to add a new contact to the list.\n" , as_user: true )
return true
# Delete specific item
elsif event.formatted_text.starts_with? "delete contact"
contact_id = event.formatted_text.gsub( "delete contact", "" ).strip
input_index = contact_id.to_i - 1
all_contacts = Contact.order(:id)
all_contacts[input_index].destroy
client.chat_postMessage(channel: event.channel, text: "I have deleted #{all_contacts[input_index].name}. Type 'add' to create a new contact", as_user: true )
return true
# Delete all
elsif event.formatted_text == "delete all"
client.chat_postMessage(channel: event.channel, text: "Are you sure you want to delete all contacts? Type `yes delete all` to delete your entire contact list. Type `nooo` to cancel", as_user: true)
return true
elsif event.formatted_text == "yes delete all"
Contact.destroy_all
#create one step that's a warning and prompts yes
client.chat_postMessage(channel: event.channel, text: "I have deleted your entire list of contacts.", as_user: true)
return true
elsif ["nooo", "no", "noo"].any? { |n| event.formatted_text.starts_with? n }
#event.formatted_text == "nooo"
client.chat_postMessage(channel: event.channel, text: "Phew, that was a close one. Your database was left intact", as_user: true)
return true
#
# # Edit specific item
else
return false
end
end
# ------------------------------------------------------------------------
# => GETS USEFUL INFO FROM SLACK
# ------------------------------------------------------------------------
def get_user_name client, event
# calls users_info on slack
info = client.users_info(user: event.user_id )
info['user']['name']
end
def is_admin_or_owner client, event
# calls users_info on slack
info = client.users_info(user: event.user_id )
info['user']['is_admin'] || info['user']['is_owner']
end
end
end
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .