Skip to content

Commit

Permalink
Merge pull request #2 from RyanSept/develop
Browse files Browse the repository at this point in the history
DevelopSuccessfully integrated and merged
  • Loading branch information
Ian Otieno authored Jun 30, 2017
2 parents 860b304 + a5f76e0 commit fe1536c
Show file tree
Hide file tree
Showing 14 changed files with 509 additions and 156 deletions.
6 changes: 2 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
credentials
config.py
List of all emails
emails.json

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down Expand Up @@ -85,6 +86,3 @@ celerybeat-schedule
venv/
ENV/
.DS_Store

# Authentication Tokens
*.json
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Sakabot
Sakabot is a slackbot designed to help Andelans find the owners of equipment and report them either lost or found.
It's been built in part using this wrapper to the slack rtm api https://github.com/lins05/slackbot.

### Getting Started
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.

### Prerequisites
You'll need a slack api bot token https://api.slack.com/bot-users.

### Installing
Clone the repo from Github by running `$ git clone [email protected]:RyanSept/sakabot.git`

Change directory into package `$ cd sakabot`

Install the dependencies by running `$ pip install requirements.txt`

You can set the required environment variables like so

```
$ export BOT_TOKEN=<SLACK_API_BOT_TOKEN>
$ export MONGODB_URI=<URI_TO_MONGO_DATABASE>
$ export ERRORS_TO=<SLACK_USER_TO_SEND_ERRORS_TO>
```

Before running you need to setup the database and populate it by running the sprawler on the OPs spreadsheet with your email credentials
on it. To run the sprawler, you need credentials. To get these, you need to setup a project on the Google Developers Console.
Follow this guide https://developers.google.com/sheets/api/quickstart/python and copy the credentials file you download
to app/sprawler/credentials as sakabot-cred.json. Copy the client email value in the credentials file you got and share
the spreadsheet with that email.

### Deployment
To deploy on heroku, you need to push setup the app on heroku, add the appropriate configs(see installing section), set up a
mongodb and scale the dyno `heroku ps:scale worker=1`

### Usage
*Searching for an item's owner*
To search for an item's owner send `find charger|mac|thunderbolt <item_id>` to _@sakabot_.
eg. `find charger 41`

*Reporting that you've lost an item*
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._
eg. `lost thunderbolt 33`

*Submit a found item*
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_
eg. `found mac 67`
9 changes: 4 additions & 5 deletions app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
from pymongo import MongoClient
from slackclient import SlackClient
from app.config import MONGODB, BOT_TOKEN
import pymongo
from app.config import MONGODB_URI, BOT_TOKEN


mongodb_client = MongoClient()
db = mongodb_client[MONGODB]
mongodb_client = MongoClient(MONGODB_URI)
db = mongodb_client.get_default_database()

# db collection
chargers = db.chargers
macbooks = db.macbooks
thunderbolts = db.thunderbolts
lost = db.lost
found = db.found
slack_handles = db.slack_handles

# slack client
slack_client = SlackClient(BOT_TOKEN)
6 changes: 6 additions & 0 deletions app/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import os
HOME_DIR = os.path.dirname(os.path.abspath(__file__))
# slack bot token
BOT_TOKEN = os.getenv("BOT_TOKEN")
# get from env or use default mongo uri
MONGODB_URI = os.getenv('MONGODB_URI') or "mongodb://127.0.0.1:27017/saka"
134 changes: 105 additions & 29 deletions app/core.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,45 @@
from app import chargers, macbooks, thunderbolts, lost, found, slack_client
import re
import random
from app import chargers, macbooks, thunderbolts, lost, found, slack_client, slack_handles


def extract_id_from_slack_handle(slack_handle):
'''
Remove slack formatting of handle eg. <@U328FG73> => U328FG73
'''
match = re.findall("<@(.*)>", slack_handle)
return match[0] if match else slack_handle


def get_equipment(equipment_id, equipment_type):
'''
Get equipment from database
Get equipment from database by id
'''
equipment = None
if equipment_type in ["mac", "tmac", "macbook"]:
equipment = chargers.find_one({"equipment_id": equipment_id})
elif equipment_type in ["charger", "charge", "procharger"]:
equipment = macbooks.find_one({"equipment_id": equipment_id})
elif equipment_type in ["tb", "thunderbolt", "thunder"]:
equipment = thunderbolts.find_one({"equipment_id": equipment_id})
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):
'''
Get equipment by slack_id
'''
equipment = None
equipment_types = {
"macbook": macbooks,
"charger": chargers,
"thunderbolt": thunderbolts
}

collection = equipment_types[equipment_type]
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 @@ -57,11 +84,11 @@ def add_found_equipment(submitter, equipment_found):


def remove_from_lost(equipment):
lost.remove({"equipment": equipment})
lost.delete_one({"equipment": equipment})


def remove_from_found(equipment):
found.remove({"equipment": equipment})
found.delete_one({"equipment": equipment})


def search_found_equipment(equipment):
Expand All @@ -72,33 +99,82 @@ def search_lost_equipment(equipment):
return lost.find_one({"equipment": equipment})


def notify_user_equipment_found(submitter, equipment_type):
def notify_user_equipment_found(submitter, owner, equipment_type):
message = "The user <@{}> found your `{}`".format(
submitter, equipment_type)
slack_client.api_call("chat.postMessage", text=message, channel=submitter)
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["fellow_name"]),
"fallback": "Equipment ID - {} | Owner - {}".format(equipment["equipment_id"], equipment["fellow_name"]),
"color": "#4B719C",
"fields": [
{
"title": "Equipment ID",
"value": "{}".format(equipment["equipment_id"]),
"short": "true"
},
{
"title": "Owner",
"value": "{}".format(equipment["fellow_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": 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|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|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|tb> <item_id>` to _@sakabot_"
"\n eg. `found mac 67`"
}
],
}
]


loading_messages = [
Expand Down
Loading

0 comments on commit fe1536c

Please sign in to comment.