From 052490c0fc8aa39c731ef2da24ddc021b34f3ce8 Mon Sep 17 00:00:00 2001 From: Alex Oladele Date: Wed, 29 Apr 2020 12:38:41 -0400 Subject: [PATCH 1/7] Add rest of project files --- .idea/.gitignore | 8 ++++++ .idea/.name | 1 + .idea/groupme-facts-bot.iml | 13 ++++++++++ .../inspectionProfiles/profiles_settings.xml | 6 +++++ .idea/misc.xml | 7 +++++ .idea/modules.xml | 8 ++++++ .idea/vcs.xml | 6 +++++ app/__init__.py | 0 app/fact_bot.py | 8 ++++++ app/fact_service.py | 4 +++ app/groupme.py | 18 +++++++++++++ model/__init__.py | 3 +++ model/db_service.py | 15 +++++++++++ model/text_db.py | 26 +++++++++++++++++++ resources/facts_sample.txt | 11 ++++++++ tests/__init__.py | 0 tests/test_text_db.py | 15 +++++++++++ util/__init__.py | 0 util/api_keys.py | 3 +++ util/common.py | 6 +++++ 20 files changed, 158 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/.name create mode 100644 .idea/groupme-facts-bot.iml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 app/__init__.py create mode 100644 app/fact_bot.py create mode 100644 app/fact_service.py create mode 100644 app/groupme.py create mode 100644 model/__init__.py create mode 100644 model/db_service.py create mode 100644 model/text_db.py create mode 100644 resources/facts_sample.txt create mode 100644 tests/__init__.py create mode 100644 tests/test_text_db.py create mode 100644 util/__init__.py create mode 100644 util/api_keys.py create mode 100644 util/common.py diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..deb38e6 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +groupme-facts-bot \ No newline at end of file diff --git a/.idea/groupme-facts-bot.iml b/.idea/groupme-facts-bot.iml new file mode 100644 index 0000000..d5733cf --- /dev/null +++ b/.idea/groupme-facts-bot.iml @@ -0,0 +1,13 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..b5f1628 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..4267c41 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/__init__.py b/app/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/fact_bot.py b/app/fact_bot.py new file mode 100644 index 0000000..a22db72 --- /dev/null +++ b/app/fact_bot.py @@ -0,0 +1,8 @@ +from app import fact_service, groupme + +if __name__ == '__main__': + # Get Groupme api token to post with + groupme_client = groupme.get_client() + + # Get Random Fact + fact = fact_service.get_random_fact() diff --git a/app/fact_service.py b/app/fact_service.py new file mode 100644 index 0000000..01b1205 --- /dev/null +++ b/app/fact_service.py @@ -0,0 +1,4 @@ +def get_random_fact(): + ret_fact = None + + return ret_fact diff --git a/app/groupme.py b/app/groupme.py new file mode 100644 index 0000000..c828513 --- /dev/null +++ b/app/groupme.py @@ -0,0 +1,18 @@ +from groupy import Client + +from util import api_keys + +GROUPME_CLIENT = None + + +def get_client(): + if GROUPME_CLIENT is None: + init_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.") diff --git a/model/__init__.py b/model/__init__.py new file mode 100644 index 0000000..b62f6ca --- /dev/null +++ b/model/__init__.py @@ -0,0 +1,3 @@ +from model import db_service + +from model import text_db diff --git a/model/db_service.py b/model/db_service.py new file mode 100644 index 0000000..7450cee --- /dev/null +++ b/model/db_service.py @@ -0,0 +1,15 @@ +from abc import abstractmethod + + +class DbService: + + def __init__(self) -> None: + super().__init__() + + @abstractmethod + def get_fact(self): + pass + + @abstractmethod + def get_multiple_facts(self, count=5): + pass diff --git a/model/text_db.py b/model/text_db.py new file mode 100644 index 0000000..2815796 --- /dev/null +++ b/model/text_db.py @@ -0,0 +1,26 @@ +import random + +from model.db_service import DbService +from util import common + + +class TextDb(DbService): + + def __init__(self) -> None: + super().__init__() + self.FACTS_LIST = None + + def load_facts_from_file(self): + facts_file = open(f"{common.get_project_root()}/resources/facts.txt", mode="r+") + self.FACTS_LIST = facts_file.readlines() + assert self.FACTS_LIST + + def get_fact(self): + if self.FACTS_LIST is None: + self.load_facts_from_file() + return random.choice(self.FACTS_LIST) + + def get_multiple_facts(self, count=5): + if self.FACTS_LIST is None: + self.load_facts_from_file() + return random.sample(self.FACTS_LIST, k=count) diff --git a/resources/facts_sample.txt b/resources/facts_sample.txt new file mode 100644 index 0000000..03ae83f --- /dev/null +++ b/resources/facts_sample.txt @@ -0,0 +1,11 @@ +Charles Darwin's personal pet tortoise didn't die until recently. +The average person will spend six months of their life waiting for red lights to turn green. +A bolt of lightning contains enough energy to toast 100,000 slices of bread. +President Lyndon B. Johnson owned a water-surfing car. +Cherophobia is the word for the irrational fear of being happy. +You can hear a blue whale's heartbeat from two miles away. +Nearly 30,000 rubber ducks were lost a sea in 1992 and are still being discovered today. +There's a Manhattan-specific ant. +The inventor of the frisbee was turned into a frisbee after he died. +There's a bridge exclusively for squirrels. +Subway footlongs aren't always a foot long. diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_text_db.py b/tests/test_text_db.py new file mode 100644 index 0000000..52c2b1a --- /dev/null +++ b/tests/test_text_db.py @@ -0,0 +1,15 @@ +from model.text_db import TextDb + + +class TestTestDb: + def test_get_fact(self): + text_db_service = TextDb() + random_fact = text_db_service.get_fact() + assert random_fact is not None + assert random_fact != "" + + def test_get_multiple_facts(self): + text_db_service = TextDb() + random_facts_list = text_db_service.get_multiple_facts() + assert random_facts_list is not None + assert len(random_facts_list) > 0 diff --git a/util/__init__.py b/util/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/util/api_keys.py b/util/api_keys.py new file mode 100644 index 0000000..3d3d03d --- /dev/null +++ b/util/api_keys.py @@ -0,0 +1,3 @@ +import os + +GROUPME = os.environ.get("GROUPME_KEY") diff --git a/util/common.py b/util/common.py new file mode 100644 index 0000000..593b45d --- /dev/null +++ b/util/common.py @@ -0,0 +1,6 @@ +from pathlib import Path + + +def get_project_root() -> Path: + """Returns project root folder.""" + return Path(__file__).parent.parent From b7bba2d263b2d3d6ea1047c35241cf507c227786 Mon Sep 17 00:00:00 2001 From: Alex Oladele Date: Wed, 29 Apr 2020 12:40:47 -0400 Subject: [PATCH 2/7] Ignore more files --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.gitignore b/.gitignore index 0033751..2327987 100644 --- a/.gitignore +++ b/.gitignore @@ -127,3 +127,7 @@ fabric.properties .idea/caches/build_file_checksums.ser resouces/facts.txt + +secrets.yaml + +resources/facts.txt From 0a35519a723c3a6934da90f2267f1220131545b2 Mon Sep 17 00:00:00 2001 From: Alex Oladele Date: Wed, 29 Apr 2020 20:48:25 -0400 Subject: [PATCH 3/7] 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") From 6bc9eb351bd7002accedd0145e2f17f6c65681da Mon Sep 17 00:00:00 2001 From: Alex Oladele Date: Wed, 29 Apr 2020 21:11:51 -0400 Subject: [PATCH 4/7] Add readme filer --- README.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..c0acceb --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +A simple random facts bot for Groupme using Python \ No newline at end of file From 9d0d6ccf8a7112db72966a85e49dc690d28011d8 Mon Sep 17 00:00:00 2001 From: Alex Oladele Date: Wed, 29 Apr 2020 22:14:46 -0400 Subject: [PATCH 5/7] Add AWS lambda lib --- .gitignore | 2 +- .idea/groupme-facts-bot.iml | 2 +- .idea/misc.xml | 2 +- config.yaml | 39 +++++++++++++++++++++++++++++++++++++ event.json | 4 ++++ requirements.txt | 3 ++- service.py | 8 ++++++++ 7 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 config.yaml create mode 100644 event.json create mode 100644 service.py diff --git a/.gitignore b/.gitignore index 2327987..a129050 100644 --- a/.gitignore +++ b/.gitignore @@ -128,6 +128,6 @@ fabric.properties resouces/facts.txt -secrets.yaml +secrets.local resources/facts.txt diff --git a/.idea/groupme-facts-bot.iml b/.idea/groupme-facts-bot.iml index d5733cf..e98566c 100644 --- a/.idea/groupme-facts-bot.iml +++ b/.idea/groupme-facts-bot.iml @@ -4,7 +4,7 @@ - + diff --git a/.idea/misc.xml b/.idea/misc.xml index b5f1628..4c9fb1f 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -3,5 +3,5 @@ - + \ No newline at end of file diff --git a/config.yaml b/config.yaml new file mode 100644 index 0000000..282d3a6 --- /dev/null +++ b/config.yaml @@ -0,0 +1,39 @@ +region: us-east-1 + +function_name: avan_facts_bot +handler: service.handler +description: Random Avan facts vis groupme +runtime: python3.8 +# role: lambda_basic_execution + +# S3 upload requires appropriate role with s3:PutObject permission +# (ex. basic_s3_upload), a destination bucket, and the key prefix +# bucket_name: 'example-bucket' +# s3_key_prefix: 'path/to/file/' + +# if access key and secret are left blank, boto will use the credentials +# defined in the [default] section of ~/.aws/credentials. +aws_access_key_id: ${AWS_TOKEN_ID} +aws_secret_access_key: ${AWS_TOKEN} + +# dist_directory: dist +# timeout: 15 +# memory_size: 512 +# concurrency: 500 +# + +# Experimental Environment variables +environment_variables: + BOT_NAME: ${BOT_NAME} + GROUPME_KEY: ${GROUPME_KEY} + +# If `tags` is uncommented then tags will be set at creation or update +# time. During an update all other tags will be removed except the tags +# listed here. +#tags: +# tag_1: foo +# tag_2: bar + +# Build options +build: + source_directories: lib # a comma delimited list of directories in your project root that contains source to package. diff --git a/event.json b/event.json new file mode 100644 index 0000000..f5ac7c2 --- /dev/null +++ b/event.json @@ -0,0 +1,4 @@ +{ + "pi": 3.14, + "e": 2.718 +} diff --git a/requirements.txt b/requirements.txt index 140019e..129e358 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ GroupyAPI -pytest \ No newline at end of file +pytest +python-lambda \ No newline at end of file diff --git a/service.py b/service.py new file mode 100644 index 0000000..f04dba3 --- /dev/null +++ b/service.py @@ -0,0 +1,8 @@ +# -*- coding: utf-8 -*- + + +def handler(event, context): + # Your code goes here! + e = event.get("e") + pi = event.get("pi") + return e + pi From c85d5cada4b7cee2af3b3fbaf3f6d489620870b2 Mon Sep 17 00:00:00 2001 From: Alex Oladele Date: Wed, 29 Apr 2020 23:14:13 -0400 Subject: [PATCH 6/7] Add lambda related fikes --- .idea/terminal.xml | 9 +++++++++ app/fact_bot.py | 10 ++++++---- config.yaml | 6 +++--- service.py | 5 ++--- 4 files changed, 20 insertions(+), 10 deletions(-) create mode 100644 .idea/terminal.xml diff --git a/.idea/terminal.xml b/.idea/terminal.xml new file mode 100644 index 0000000..a91d2d0 --- /dev/null +++ b/.idea/terminal.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/app/fact_bot.py b/app/fact_bot.py index e50108d..8b39308 100644 --- a/app/fact_bot.py +++ b/app/fact_bot.py @@ -1,16 +1,18 @@ from app import fact_service, groupme from util import constants -if __name__ == '__main__': + +def post_fact_to_group(): # 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) + + +if __name__ == '__main__': + post_fact_to_group() diff --git a/config.yaml b/config.yaml index 282d3a6..c54fc41 100644 --- a/config.yaml +++ b/config.yaml @@ -1,10 +1,10 @@ -region: us-east-1 +region: us-east-2 function_name: avan_facts_bot handler: service.handler -description: Random Avan facts vis groupme +description: Random Avan facts via groupme runtime: python3.8 -# role: lambda_basic_execution +#role: lambda_basic_execution # S3 upload requires appropriate role with s3:PutObject permission # (ex. basic_s3_upload), a destination bucket, and the key prefix diff --git a/service.py b/service.py index f04dba3..30c412c 100644 --- a/service.py +++ b/service.py @@ -1,8 +1,7 @@ # -*- coding: utf-8 -*- +from app import fact_bot def handler(event, context): # Your code goes here! - e = event.get("e") - pi = event.get("pi") - return e + pi + fact_bot.post_fact_to_group() From 7016926c3de9ee707bc806c9729350356e17420c Mon Sep 17 00:00:00 2001 From: Alex Oladele Date: Thu, 30 Apr 2020 22:25:20 -0400 Subject: [PATCH 7/7] Add DynamoDb support --- .gitignore | 4 ++- .idea/terminal.xml | 5 +++ __init__.py | 1 + app/__init__.py | 0 util/common.py => common.py | 0 config.yaml | 4 ++- constants.py | 7 ++++ model/db_service.py => db_service.py | 0 dynamo_db.py | 34 +++++++++++++++++++ app/fact_bot.py => fact_bot.py | 5 +-- app/fact_service.py => fact_service.py | 6 ++-- .../facts_sample.txt => facts_sample.txt | 0 app/groupme.py => groupme.py | 2 +- model/__init__.py | 3 -- requirements.txt | 1 + service.py | 2 +- test_dynamo_db.py | 15 ++++++++ tests/test_text_db.py => test_text_db.py | 4 +-- tests/__init__.py | 0 model/text_db.py => text_db.py | 7 ++-- util/__init__.py | 0 util/constants.py | 4 --- 22 files changed, 84 insertions(+), 20 deletions(-) create mode 100644 __init__.py delete mode 100644 app/__init__.py rename util/common.py => common.py (100%) create mode 100644 constants.py rename model/db_service.py => db_service.py (100%) create mode 100644 dynamo_db.py rename app/fact_bot.py => fact_bot.py (86%) rename app/fact_service.py => fact_service.py (68%) rename resources/facts_sample.txt => facts_sample.txt (100%) rename app/groupme.py => groupme.py (97%) delete mode 100644 model/__init__.py create mode 100644 test_dynamo_db.py rename tests/test_text_db.py => test_text_db.py (89%) delete mode 100644 tests/__init__.py rename model/text_db.py => text_db.py (79%) delete mode 100644 util/__init__.py delete mode 100644 util/constants.py diff --git a/.gitignore b/.gitignore index a129050..b76ad2b 100644 --- a/.gitignore +++ b/.gitignore @@ -130,4 +130,6 @@ resouces/facts.txt secrets.local -resources/facts.txt +facts.txt + +.idea/terminal.xml diff --git a/.idea/terminal.xml b/.idea/terminal.xml index a91d2d0..fbd11ad 100644 --- a/.idea/terminal.xml +++ b/.idea/terminal.xml @@ -4,6 +4,11 @@ + + + + + \ No newline at end of file diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..bcc746e --- /dev/null +++ b/__init__.py @@ -0,0 +1 @@ +import service \ No newline at end of file diff --git a/app/__init__.py b/app/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/util/common.py b/common.py similarity index 100% rename from util/common.py rename to common.py diff --git a/config.yaml b/config.yaml index c54fc41..efc85f8 100644 --- a/config.yaml +++ b/config.yaml @@ -3,7 +3,7 @@ region: us-east-2 function_name: avan_facts_bot handler: service.handler description: Random Avan facts via groupme -runtime: python3.8 +runtime: python3.7 #role: lambda_basic_execution # S3 upload requires appropriate role with s3:PutObject permission @@ -25,7 +25,9 @@ aws_secret_access_key: ${AWS_TOKEN} # Experimental Environment variables environment_variables: BOT_NAME: ${BOT_NAME} + DB_TABLE_NAME: ${DB_TABLE_NAME} GROUPME_KEY: ${GROUPME_KEY} + FACTS_PATH: ${FACTS_PATH} # If `tags` is uncommented then tags will be set at creation or update # time. During an update all other tags will be removed except the tags diff --git a/constants.py b/constants.py new file mode 100644 index 0000000..7bca529 --- /dev/null +++ b/constants.py @@ -0,0 +1,7 @@ +import os + +BOT_NAME = os.environ.get("BOT_NAME") +DB_TABLE_NAME = os.environ.get("DB_TABLE_NAME") +FACTS_PATH = os.environ.get("FACTS_PATH") +GROUPME = os.environ.get("GROUPME_KEY") + diff --git a/model/db_service.py b/db_service.py similarity index 100% rename from model/db_service.py rename to db_service.py diff --git a/dynamo_db.py b/dynamo_db.py new file mode 100644 index 0000000..ab19c9d --- /dev/null +++ b/dynamo_db.py @@ -0,0 +1,34 @@ +import random + +import boto3 + +import constants +from db_service import DbService +import common + + +class DynamoDb(DbService): + + def __init__(self) -> None: + super().__init__() + self.FACTS_LIST = [] + self.dynamodb = boto3.resource('dynamodb') + self.facts_table = self.dynamodb.Table(constants.DB_TABLE_NAME) + print(self.facts_table.creation_date_time) + + def load_facts_from_db(self): + # global facts_table + self.FACTS_LIST.clear() + resp = self.facts_table.scan() + self.FACTS_LIST = [i["fact"] for i in resp["Items"]] + assert self.FACTS_LIST + + def get_fact(self): + if self.FACTS_LIST is None or not self.FACTS_LIST: + self.load_facts_from_db() + return random.choice(self.FACTS_LIST) + + def get_multiple_facts(self, count=2): + if self.FACTS_LIST is None or not self.FACTS_LIST: + self.load_facts_from_db() + return random.sample(self.FACTS_LIST, k=count) diff --git a/app/fact_bot.py b/fact_bot.py similarity index 86% rename from app/fact_bot.py rename to fact_bot.py index 8b39308..cb5687b 100644 --- a/app/fact_bot.py +++ b/fact_bot.py @@ -1,5 +1,6 @@ -from app import fact_service, groupme -from util import constants +import fact_service +import groupme +import constants def post_fact_to_group(): diff --git a/app/fact_service.py b/fact_service.py similarity index 68% rename from app/fact_service.py rename to fact_service.py index da1df68..3319e50 100644 --- a/app/fact_service.py +++ b/fact_service.py @@ -1,9 +1,11 @@ -from model.text_db import TextDb +from dynamo_db import DynamoDb +from text_db import TextDb def get_random_fact(): ret_fact = None - db_service = TextDb() + # db_service = TextDb() + db_service = DynamoDb() ret_fact = db_service.get_fact() return ret_fact diff --git a/resources/facts_sample.txt b/facts_sample.txt similarity index 100% rename from resources/facts_sample.txt rename to facts_sample.txt diff --git a/app/groupme.py b/groupme.py similarity index 97% rename from app/groupme.py rename to groupme.py index 5226c8f..dbf18e6 100644 --- a/app/groupme.py +++ b/groupme.py @@ -1,6 +1,6 @@ from groupy import Client -from util import constants +import constants GROUPME_CLIENT: Client = None diff --git a/model/__init__.py b/model/__init__.py deleted file mode 100644 index b62f6ca..0000000 --- a/model/__init__.py +++ /dev/null @@ -1,3 +0,0 @@ -from model import db_service - -from model import text_db diff --git a/requirements.txt b/requirements.txt index 129e358..7c23132 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +boto3 GroupyAPI pytest python-lambda \ No newline at end of file diff --git a/service.py b/service.py index 30c412c..b800cf9 100644 --- a/service.py +++ b/service.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -from app import fact_bot +import fact_bot def handler(event, context): diff --git a/test_dynamo_db.py b/test_dynamo_db.py new file mode 100644 index 0000000..43fe319 --- /dev/null +++ b/test_dynamo_db.py @@ -0,0 +1,15 @@ +from dynamo_db import DynamoDb + + +class TestDynamoDb: + def test_get_fact(self): + text_db_service = DynamoDb() + random_fact = text_db_service.get_fact() + assert random_fact is not None + assert random_fact != "" + + def test_get_multiple_facts(self): + text_db_service = DynamoDb() + random_facts_list = text_db_service.get_multiple_facts() + assert random_facts_list is not None + assert len(random_facts_list) > 0 diff --git a/tests/test_text_db.py b/test_text_db.py similarity index 89% rename from tests/test_text_db.py rename to test_text_db.py index 52c2b1a..7744be7 100644 --- a/tests/test_text_db.py +++ b/test_text_db.py @@ -1,7 +1,7 @@ -from model.text_db import TextDb +from text_db import TextDb -class TestTestDb: +class TestTextDb: def test_get_fact(self): text_db_service = TextDb() random_fact = text_db_service.get_fact() diff --git a/tests/__init__.py b/tests/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/model/text_db.py b/text_db.py similarity index 79% rename from model/text_db.py rename to text_db.py index 2815796..4c3f1d2 100644 --- a/model/text_db.py +++ b/text_db.py @@ -1,7 +1,8 @@ import random -from model.db_service import DbService -from util import common +import constants +from db_service import DbService +import common class TextDb(DbService): @@ -11,7 +12,7 @@ class TextDb(DbService): self.FACTS_LIST = None def load_facts_from_file(self): - facts_file = open(f"{common.get_project_root()}/resources/facts.txt", mode="r+") + facts_file = open(f"{constants.FACTS_PATH}", mode="r") self.FACTS_LIST = facts_file.readlines() assert self.FACTS_LIST diff --git a/util/__init__.py b/util/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/util/constants.py b/util/constants.py deleted file mode 100644 index ff19829..0000000 --- a/util/constants.py +++ /dev/null @@ -1,4 +0,0 @@ -import os - -BOT_NAME = os.environ.get("BOT_NAME") -GROUPME = os.environ.get("GROUPME_KEY")