Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature urls by tacos #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
bots/MsTaco/db/*.json
bots/MsTaco/src/config.py
**/__pycache__/*
*.pyc

6 changes: 4 additions & 2 deletions bots/MsTaco/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def handle_mention(event):
channel = event["channel"]

if giver_id != slack.starterbot_id and receiver_id != slack.starterbot_id and giver_id != receiver_id:
use_cases.give_tacos(giver_id, receiver_id, given_tacos, False, channel)
use_cases.give_tacos(giver_id, receiver_id, given_tacos, False, channel, event["text"])


def handle_reaction(event):
Expand All @@ -31,9 +31,11 @@ def handle_reaction(event):
receiver_id = event['item_user']
given_tacos = 1 if event['reaction'] == TACO.replace(':', '') else 0.5
channel = event["item"]["channel"]
ts = event["item"]['ts']
messages = slack.get_messages(channel, ts)

if giver_id != slack.starterbot_id and receiver_id != slack.starterbot_id and giver_id != receiver_id:
use_cases.give_tacos(giver_id, receiver_id, given_tacos, True, channel)
use_cases.give_tacos(giver_id, receiver_id, given_tacos, True, channel, messages[0])


def handle_direct_command(event):
Expand Down
Empty file added bots/MsTaco/src/__init__.py
Empty file.
1 change: 1 addition & 0 deletions bots/MsTaco/src/config.sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
MSTACO_BOT_TOKEN = "aqui va el token"
RTM_READ_DELAY = 1 # 1 second delay between reading from RTM
MENTION_REGEX = "<@(|[WU].+?)>(.*)"
URL_REGEX = "(.*?)(https:\/\/.+?)($| (.*))"
RESET_HOUR = 2 # Reset system at 6am.

TACO = ":taco:"
Expand Down
8 changes: 7 additions & 1 deletion bots/MsTaco/src/persistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def get_user(user_id):
'user_id': user_id,
'daily_tacos': DAILY_TACOS,
'daily_bonus': True,
'urls': [],
'owned_tacos': 0
})
db_user = users_db.get(doc_id=db_user_id)
Expand All @@ -49,10 +50,12 @@ def get_prev_weekly_info():
def update(self):
self.user = users_db.get(Query()['user_id'] == self.user_id)

def add_tacos(self, amount, bonus=False):
def add_tacos(self, amount, bonus=False, url=''):
users_db.update(add('owned_tacos', amount), Query()['user_id'] == self.user_id)
if bonus:
users_db.update({'daily_bonus': False}, Query()['user_id'] == self.user_id)
if url:
users_db.update(add('urls', [url]), Query()['user_id'] == self.user_id)
self.update()

def remove_tacos(self, amount):
Expand All @@ -62,6 +65,9 @@ def remove_tacos(self, amount):
def remaining_tacos(self):
return self.user['daily_tacos']

def urls(self):
return self.user['urls']

def owned_tacos(self):
return self.user['owned_tacos']

Expand Down
11 changes: 10 additions & 1 deletion bots/MsTaco/src/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

slack_client = SlackClient(MSTACO_BOT_TOKEN)

channels = slack_client.api_call("conversations.list")['channels']
channels = slack_client.api_call("conversations.list").get('channels',[])
users = slack_client.api_call("users.list")

starterbot_id = None
Expand All @@ -23,6 +23,15 @@ def send_message(user_id, message):
text=message
)

def get_messages(channel, msg_ts):
resp = slack_client.api_call(
"conversations.history",
channel=channel,
inclusive=true,
limit=1,
latest=msg_ts
)
return resp.json()["messages"]

def setup():
global starterbot_id
Expand Down
10 changes: 6 additions & 4 deletions bots/MsTaco/src/use_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
import operator
import src.persistence as persistence
import src.slack as slack
from src.config import DAILY_TACOS, MENTION_REGEX
from src.config import DAILY_TACOS, MENTION_REGEX, URL_REGEX
from src.time_utils import get_today, get_time_left


def give_tacos(giver_id, receiver_id, given_tacos, reaction=False, channel=None):
def give_tacos(giver_id, receiver_id, given_tacos, reaction=False, channel=None, message=""):
giver = persistence.DBUser(giver_id)
receiver = persistence.DBUser(receiver_id)

match_url = re.search(URL_REGEX, message)
url = match_url.group(2) if match_url else None

if giver.remaining_tacos() >= given_tacos:
receiver.add_tacos(given_tacos)
receiver.add_tacos(given_tacos, url=url)
giver.remove_tacos(given_tacos)

_notify_tacos_sent(giver.user_id, receiver.user_id, given_tacos, giver.remaining_tacos())
Expand Down
Empty file added bots/MsTaco/test/__init__.py
Empty file.
27 changes: 27 additions & 0 deletions bots/MsTaco/test/persistence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import unittest
from src.persistence import users_db, DBUser

class PersistenceTestFixture(unittest.TestCase):

def setUp(self):
users_db.purge()
self.db_user = DBUser(1)

def test_AddTwoTacos(self):
self.db_user.add_tacos(1)
self.db_user.add_tacos(1)
tacos = self.db_user.owned_tacos()
self.assertEqual(2, tacos)

def test_endWithOneTacoAfterAddingAndRemoving(self):
self.db_user.add_tacos(2)
self.db_user.remove_tacos(1)
tacos = self.db_user.owned_tacos()
self.assertEqual(2, tacos)

def tearDown(self):
del(self.db_user)


if __name__ == '__main__':
unittest.main()
71 changes: 71 additions & 0 deletions bots/MsTaco/test/use_cases.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import unittest
import src.use_cases as u
from src.persistence import users_db, DBUser

ALICE = 10
BOB = 20
CRIS = 20

class UseCasesTestFixture(unittest.TestCase):

def setUp(self):
users_db.purge()

def test_AliceGiveTacoToBob(self):
#given
self.assertEqual(5, DBUser(ALICE).remaining_tacos())
self.assertEqual(0, DBUser(BOB).owned_tacos())

#when
u.give_tacos(ALICE, BOB, 1)

#then
self.assertEqual(4, DBUser(ALICE).remaining_tacos())
self.assertEqual(1, DBUser(BOB).owned_tacos())

def test_AliceGiveTacoToBobWithUrl_conTextoAntesYDespues(self):
#given
self.assertEqual([], DBUser(BOB).urls())

#when
u.give_tacos(ALICE, BOB, 1, message="aqui https://github.com/ml-hispano/MLH_bot/issues/14 pueden ver el issue")

#then
self.assertEqual(['https://github.com/ml-hispano/MLH_bot/issues/14'], DBUser(BOB).urls())

def test_AliceGiveTacoToBobWithUrl_conTextoAntes(self):
#given
self.assertEqual([], DBUser(BOB).urls())

#when
u.give_tacos(ALICE, BOB, 1, message="aqui pueden ver el issue https://github.com/ml-hispano/MLH_bot/issues/14")

#then
self.assertEqual(['https://github.com/ml-hispano/MLH_bot/issues/14'], DBUser(BOB).urls())

def test_AliceGiveTacoToBobWithUrl_conTextoDespues(self):
#given
self.assertEqual([], DBUser(BOB).urls())

#when
u.give_tacos(ALICE, BOB, 1, message="https://github.com/ml-hispano/MLH_bot/issues/14 aqui pueden ver el issue")

#then
self.assertEqual(['https://github.com/ml-hispano/MLH_bot/issues/14'], DBUser(BOB).urls())

def test_AliceGiveTacoToBobWithUrl_conUrlRepetida(self):
#given
self.assertEqual([], DBUser(BOB).urls())

#when
u.give_tacos(ALICE, BOB, 1, message="https://github.com/ml-hispano/MLH_bot/issues/14 aqui pueden ver el issue")
u.give_tacos(CRIS, BOB, 1, message="aqui pueden ver el issue https://github.com/ml-hispano/MLH_bot/issues/14")

#then
self.assertEqual([
'https://github.com/ml-hispano/MLH_bot/issues/14',
'https://github.com/ml-hispano/MLH_bot/issues/14',
], DBUser(BOB).urls())

if __name__ == '__main__':
unittest.main()