Merge branch 'develop'

This commit is contained in:
2020-05-01 00:17:36 -04:00
26 changed files with 332 additions and 1 deletions

6
.gitignore vendored
View File

@@ -127,3 +127,9 @@ fabric.properties
.idea/caches/build_file_checksums.ser
resouces/facts.txt
secrets.local
facts.txt
.idea/terminal.xml

8
.idea/.gitignore generated vendored Normal file
View File

@@ -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/

1
.idea/.name generated Normal file
View File

@@ -0,0 +1 @@
groupme-facts-bot

13
.idea/groupme-facts-bot.iml generated Normal file
View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="jdk" jdkName="Python 3.7 (groupme-facts-bot)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="TestRunnerService">
<option name="PROJECT_TEST_RUNNER" value="pytest" />
</component>
</module>

View File

@@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

7
.idea/misc.xml generated Normal file
View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.7 (groupme-facts-bot)" project-jdk-type="Python SDK" />
</project>

8
.idea/modules.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/groupme-facts-bot.iml" filepath="$PROJECT_DIR$/.idea/groupme-facts-bot.iml" />
</modules>
</component>
</project>

14
.idea/terminal.xml generated Normal file
View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="TerminalProjectOptionsProvider">
<envs>
<env key="AWS_TOKEN_ID" value="AKIAIIITA3BAG6LBBZ7A" />
<env key="AWS_TOKEN" value="OkJJ/LubZea6KbicszllpUKf6QvawCzOGxMJAaR5" />
<env key="BOT_NAME" value="Rando Facts Test" />
<env key="GROUPME_KEY" value="5ad6d5506c5e0138d774127c6b6f91ba" />
<env key="AWS_ACCESS_KEY_ID" value="AKIAIIITA3BAG6LBBZ7A" />
<env key="AWS_SECRET_KEY" value="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" />
<env key="FACTS_PATH" value="/var/task/facts.txt" />
</envs>
</component>
</project>

6
.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

1
README.md Normal file
View File

@@ -0,0 +1 @@
A simple random facts bot for Groupme using Python

1
__init__.py Normal file
View File

@@ -0,0 +1 @@
import service

6
common.py Normal file
View File

@@ -0,0 +1,6 @@
from pathlib import Path
def get_project_root() -> Path:
"""Returns project root folder."""
return Path(__file__).parent.parent

41
config.yaml Normal file
View File

@@ -0,0 +1,41 @@
region: us-east-2
function_name: avan_facts_bot
handler: service.handler
description: Random Avan facts via groupme
runtime: python3.7
#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}
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
# 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.

7
constants.py Normal file
View File

@@ -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")

15
db_service.py Normal file
View File

@@ -0,0 +1,15 @@
from abc import abstractmethod
class DbService:
def __init__(self) -> None:
super().__init__()
@abstractmethod
def get_fact(self):
raise NotImplementedError
@abstractmethod
def get_multiple_facts(self, count=5):
raise NotImplementedError

34
dynamo_db.py Normal file
View File

@@ -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)

4
event.json Normal file
View File

@@ -0,0 +1,4 @@
{
"pi": 3.14,
"e": 2.718
}

19
fact_bot.py Normal file
View File

@@ -0,0 +1,19 @@
import fact_service
import groupme
import constants
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()

17
fact_service.py Normal file
View File

@@ -0,0 +1,17 @@
from dynamo_db import DynamoDb
from text_db import TextDb
def get_random_fact():
ret_fact = None
# db_service = TextDb()
db_service = DynamoDb()
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

11
facts_sample.txt Normal file
View File

@@ -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.

40
groupme.py Normal file
View File

@@ -0,0 +1,40 @@
from groupy import Client
import constants
GROUPME_CLIENT: Client = None
def init_client():
global GROUPME_CLIENT
GROUPME_CLIENT = Client.from_token(constants.GROUPME)
def get_client():
if GROUPME_CLIENT is None:
init_client()
return GROUPME_CLIENT
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

View File

@@ -1,2 +1,4 @@
boto3
GroupyAPI
pytest
pytest
python-lambda

7
service.py Normal file
View File

@@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-
import fact_bot
def handler(event, context):
# Your code goes here!
fact_bot.post_fact_to_group()

15
test_dynamo_db.py Normal file
View File

@@ -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

15
test_text_db.py Normal file
View File

@@ -0,0 +1,15 @@
from text_db import TextDb
class TestTextDb:
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

27
text_db.py Normal file
View File

@@ -0,0 +1,27 @@
import random
import constants
from db_service import DbService
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"{constants.FACTS_PATH}", 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)