Skip to content

Commit

Permalink
[Feature] Add search by slack_id
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanSept committed Jun 19, 2017
1 parent 47255fe commit a5f76e0
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 55 deletions.
77 changes: 45 additions & 32 deletions app/core.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
import random
from app import chargers, macbooks, thunderbolts, lost, found, slack_client, slack_handles


Expand All @@ -9,19 +10,18 @@ def extract_id_from_slack_handle(slack_handle):
match = re.findall("<@(.*)>", slack_handle)
return match[0] if match else slack_handle


def get_equipment(equipment_id, equipment_type):
'''
Get equipment from database by id
'''
equipment = None
if equipment_type in ["mac", "tmac", "macbook"]:
equipment = macbooks.find_one({"equipment_id": equipment_id})
elif equipment_type in ["charger", "charge", "procharger"]:
equipment = chargers.find_one({"equipment_id": equipment_id})
elif equipment_type in ["tb", "thunderbolt", "thunder"]:
equipment = thunderbolts.find_one({"equipment_id": equipment_id})

return equipment
equipment_types = {
"macbook": macbooks,
"charger": chargers,
"thunderbolt": thunderbolts
}
collection = equipment_types[equipment_type]
return collection.find({"equipment_id": equipment_id})


def get_equipment_by_slack_id(slack_id, equipment_type):
Expand All @@ -36,9 +36,9 @@ def get_equipment_by_slack_id(slack_id, equipment_type):
}

collection = equipment_types[equipment_type]
email = slack_handles.find_one({"slack_id": slack_id})
if email is not None:
email = email["email"]
slack = slack_handles.find_one({"slack_id": slack_id})
if slack is not None:
email = slack["email"]
equipment = collection.find({"owner_email": email})
return equipment

Expand Down Expand Up @@ -105,58 +105,71 @@ def notify_user_equipment_found(submitter, owner, equipment_type):
slack_client.api_call("chat.postMessage", text=message, channel=owner)


def generate_random_hex_color():
'''
Generate random hex color
'''
r = lambda: random.randint(0, 255)
return ('#%02X%02X%02X' % (r(), r(), r()))


def build_search_reply_atachment(equipment, category):
'''
Returns a slack attachment to show a result
'''
return [{
"text": "That {} belongs to {}".format(category, equipment["owner_name"]),
"fallback": "Equipment ID - {} | Owner - {}".format(equipment["equipment_id"], equipment["owner_name"]),
"color": "#4B719C",
"fields": [
{
"title": "Equipment ID",
"value": "{}".format(equipment["equipment_id"]),
"short": "true"
},
{
"title": "Owner",
"value": "{}".format(equipment["owner_name"]),
"short": "true"
}
return {
"text": "{}'s {}".format(equipment["owner_name"], category),
"fallback": "Equipment ID - {} | Owner - {}".format(equipment["equipment_id"], equipment["owner_name"]),
"color": generate_random_hex_color(),
"fields": [{
"title": "Equipment ID",
"value": "{}".format(equipment["equipment_id"]),
"short": "true"
},
{
"title": "Owner",
"value": "{}".format(equipment["owner_name"]),
"short": "true"
}
]
}]
}


def get_help_message():
return [
{
"text": "Sakabot helps you search, find or report a lost item "
"whether it be your macbook, thunderbolt or charger.\n *USAGE*",
"color": "#4B719C",
"color": generate_random_hex_color(),
"mrkdwn_in": ["fields", "text"],
"fields": [
{
"title": "Searching for an item's owner",
"value": "To search for an item's owner send "
"`find charger|mac|thunderbolt <item_id>` "
"`find <charger|mac|thunderbolt|tb> <item_id>` "
"to _@sakabot_.\n eg. `find charger 41`"
},
{
"title": "Check what items someone owns",
"value": "To check what item someone owns "
"`find <@mention|my> <charger|mac|thunderbolt>` "
"to _@sakabot_.\n eg. `find my charger` or `find @ianoti tb`"
},
{
"title": "Reporting that you've lost an item",
"value": "When you lose an item, there's a chance that "
"somebody has found it and submitted it to Sakabot. "
"In that case we'll tell you who found it, otherwise, "
"we'll slack you in case anyone reports they found it. To "
"report an item as lost send `lost charger|mac|thunderbolt <item_id>` to _@sakabot._"
"report an item as lost send `lost <charger|mac|thunderbolt|tb> <item_id>` to _@sakabot._"
"\n eg. `lost thunderbolt 33`"
},
{
"title": "Submit a found item",
"value": "When you find a lost item you can report that "
"you found it and in case a user had reported it lost, "
"we'll slack them immediately telling them you found it. "
"To report that you found an item send `found charger|mac|thunderbolt <item_id>` to _@sakabot_"
"To report that you found an item send `found <charger|mac|thunderbolt|tb> <item_id>` to _@sakabot_"
"\n eg. `found mac 67`"
}
],
Expand Down
59 changes: 36 additions & 23 deletions app/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
open(HOME_DIR + "/utils/fortunes.json", "r").read())

EQUIPMENT_TYPES = {}
EQUIPMENT_TYPES["macbook"] = EQUIPMENT_TYPES["tmac"] = EQUIPMENT_TYPES["mac"] = "macbook"
EQUIPMENT_TYPES["charger"] = EQUIPMENT_TYPES["charge"] = EQUIPMENT_TYPES["procharger"] = "charger"
EQUIPMENT_TYPES["tb"] = EQUIPMENT_TYPES["thunderbolt"] = EQUIPMENT_TYPES["thunder"] = "thunderbolt"
EQUIPMENT_TYPES["macbook"] = EQUIPMENT_TYPES[
"tmac"] = EQUIPMENT_TYPES["mac"] = "macbook"
EQUIPMENT_TYPES["charger"] = EQUIPMENT_TYPES[
"charge"] = EQUIPMENT_TYPES["procharger"] = "charger"
EQUIPMENT_TYPES["tb"] = EQUIPMENT_TYPES[
"thunderbolt"] = EQUIPMENT_TYPES["thunder"] = "thunderbolt"


@respond_to('hello$|hi$|hey$|aloha$|bonjour$', re.IGNORECASE)
Expand Down Expand Up @@ -58,21 +61,32 @@ def find_equipment_by_slack_id(message, command, owner_handle, equipment_type):
if equipment_type in EQUIPMENT_TYPES:
time.sleep(1)
message.reply(random.choice(loading_messages)["quote"])
time.sleep(2) # fake loading

equipment_type = EQUIPMENT_TYPES[equipment_type]
message.reply("Finding {}'s {}".format(owner_handle, equipment_type))

slack_id = extract_id_from_slack_handle(owner_handle)
equipment = get_equipment_by_slack_id(slack_id, equipment_type)
time.sleep(1)
message.reply(str([i for i in equipment]))
equipments = get_equipment_by_slack_id(slack_id, equipment_type)

if equipments is not None and equipments.count():
print equipments
attachments += [build_search_reply_atachment(
equipment, equipment_type) for equipment in equipments]
time.sleep(2) # fake loading
print attachments
message.send_webapi('', json.dumps(attachments))
else:
time.sleep(1)
message.reply("We were unable to find a "
"{} belonging to {} :snowman_without_snow:".format(
equipment_type,
owner_handle))
elif equipment_type not in EQUIPMENT_TYPES:
responses = [
"Yea, {} could use {}".format(owner_handle, equipment_type),
"I don't think {} has {}".format(owner_handle, equipment_type),
"That's highly unprofessional.:expressionless:",
"{} is perfectly fine without {}".format(owner_handle, equipment_type),
"{} is perfectly fine without {}".format(
owner_handle, equipment_type),
"Here you go {} : {}".format(owner_handle, equipment_type)
]
time.sleep(1)
Expand All @@ -82,27 +96,26 @@ def find_equipment_by_slack_id(message, command, owner_handle, equipment_type):

@respond_to("(find|get|search|retrieve) (mac|tmac|macbook|charger|charge|procharger|tb|thunderbolt|thunder).*?(\d+)", re.IGNORECASE)
def find_equipment(message, command, equipment_type, equipment_id):
time.sleep(1)
message.reply(random.choice(loading_messages)["quote"])

attachments = []
equipment_type = equipment_type.strip().lower()
print equipment_type
equipment_type = EQUIPMENT_TYPES[equipment_type.strip().lower()]
# get equipment from db
equipment = get_equipment(int(equipment_id), equipment_type)
equipments = get_equipment(int(equipment_id), equipment_type)

if equipment:
time.sleep(2) # fake loading
attachments.extend(
build_search_reply_atachment(equipment,
"item"))
if equipments is not None and equipments.count():
time.sleep(1)
message.send_webapi('', json.dumps(attachments))
message.reply(random.choice(loading_messages)["quote"])

attachments += [build_search_reply_atachment(
equipment, equipment_type) for equipment in equipments]
time.sleep(2) # fake loading

text = "\n" if equipments.count() < 2 else "\n*There seem to have been more than one version of this item*"
message.send_webapi(text, json.dumps(attachments))
return
else:
time.sleep(1)
message.reply("We were unable to find an "
"item by the id {} :snowman_without_snow:".format(equipment_id))
message.reply("We were unable to find a "
"{} by the id {} :snowman_without_snow:".format(equipment_type, equipment_id))


@respond_to("lost (mac|tmac|macbook|charger|charge|procharger|tb|thunderbolt|thunder).*?(\d+)")
Expand Down

0 comments on commit a5f76e0

Please sign in to comment.