From 0a35519a723c3a6934da90f2267f1220131545b2 Mon Sep 17 00:00:00 2001 From: Alex Oladele Date: Wed, 29 Apr 2020 20:48:25 -0400 Subject: [PATCH] Initial implementation of Avan fact bot --- app/fact_bot.py | 8 +++++++ app/fact_service.py | 13 ++++++++++- app/groupme.py | 36 ++++++++++++++++++++++++------ model/db_service.py | 4 ++-- util/{api_keys.py => constants.py} | 1 + 5 files changed, 52 insertions(+), 10 deletions(-) rename util/{api_keys.py => constants.py} (57%) diff --git a/app/fact_bot.py b/app/fact_bot.py index a22db72..e50108d 100644 --- a/app/fact_bot.py +++ b/app/fact_bot.py @@ -1,8 +1,16 @@ from app import fact_service, groupme +from util import constants if __name__ == '__main__': # Get Groupme api token to post with groupme_client = groupme.get_client() + # Get specific group from groupme + fact_bot = groupme.get_bot(constants.BOT_NAME) + # Get Random Fact fact = fact_service.get_random_fact() + + # Post fact in Group + # groupme_client. + message = fact_bot.post(text=fact) diff --git a/app/fact_service.py b/app/fact_service.py index 01b1205..da1df68 100644 --- a/app/fact_service.py +++ b/app/fact_service.py @@ -1,4 +1,15 @@ +from model.text_db import TextDb + + def get_random_fact(): ret_fact = None - + db_service = TextDb() + ret_fact = db_service.get_fact() + return ret_fact + + +def get_multiple_random_facts(): + ret_fact = None + db_service = TextDb() + ret_fact = db_service.get_multiple_facts() return ret_fact diff --git a/app/groupme.py b/app/groupme.py index c828513..5226c8f 100644 --- a/app/groupme.py +++ b/app/groupme.py @@ -1,8 +1,13 @@ from groupy import Client -from util import api_keys +from util import constants -GROUPME_CLIENT = None +GROUPME_CLIENT: Client = None + + +def init_client(): + global GROUPME_CLIENT + GROUPME_CLIENT = Client.from_token(constants.GROUPME) def get_client(): @@ -11,8 +16,25 @@ def get_client(): return GROUPME_CLIENT -def init_client(): - global GROUPME_CLIENT - GROUPME_CLIENT = Client.from_token(api_keys.GROUPME) - if not list(GROUPME_CLIENT.groups.list_all()): - raise Exception("Invalid groupme token or not apart of any groups.") +def get_group(group_name): + found_group = Exception("Input group not found") + if GROUPME_CLIENT is None: + init_client() + group_list = GROUPME_CLIENT.groups.list(per_page=50) + for ret_group in group_list: + if group_name.casefold() == ret_group.name.casefold(): + found_group = ret_group + break + return found_group + + +def get_bot(bot_name): + found_bot = Exception("Input bot not found") + if GROUPME_CLIENT is None: + init_client() + bot_list = GROUPME_CLIENT.bots.list() + for ret_bot in bot_list: + if bot_name.casefold() == ret_bot.name.casefold(): + found_bot = ret_bot + break + return found_bot diff --git a/model/db_service.py b/model/db_service.py index 7450cee..89ab2b8 100644 --- a/model/db_service.py +++ b/model/db_service.py @@ -8,8 +8,8 @@ class DbService: @abstractmethod def get_fact(self): - pass + raise NotImplementedError @abstractmethod def get_multiple_facts(self, count=5): - pass + raise NotImplementedError diff --git a/util/api_keys.py b/util/constants.py similarity index 57% rename from util/api_keys.py rename to util/constants.py index 3d3d03d..ff19829 100644 --- a/util/api_keys.py +++ b/util/constants.py @@ -1,3 +1,4 @@ import os +BOT_NAME = os.environ.get("BOT_NAME") GROUPME = os.environ.get("GROUPME_KEY")