Skip to content

Commit

Permalink
implement more checks and vastly improve heroku button process
Browse files Browse the repository at this point in the history
  • Loading branch information
zachhardesty7 committed Jan 21, 2019
1 parent 57367d9 commit 7504ab7
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 60 deletions.
2 changes: 1 addition & 1 deletion README.md
@@ -1,7 +1,7 @@
# groupme-notifier

send email notifications for any given trigger words in a given GroupMe discussion
very very naive deploy button, more tutorial coming soon
button deploy works but requires a multitude of tokens. fields are well described and include links.

[![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy)

Expand Down
31 changes: 18 additions & 13 deletions app.json
Expand Up @@ -3,35 +3,40 @@
"description": "send email notifications for any given trigger words in a given GroupMe discussion",
"website": "https://zachhardesty.com",
"repository": "https://github.com/zachhardesty7/groupme-notifier",
"buildpacks": [
{
"url": "heroku/python"
}
],
"env": {
"GROUPME_TOKEN": {
"description": "secret key for accessing groupme api"
"description": "secret key for accessing groupme api, visit here: https://dev.groupme.com/applications/new, fill in fields with junk info and 'Callback URL' with 'localhost.' you want the access token on the next page."
},
"GROUPME_GROUP_NAMES": {
"description": "comma deliminated list of target groupme group names to display for more informative emails",
"description": "comma deliminated list (no spaces) of target groupme group names to display for more informative emails. leave blank unless you know what you're doing.",
"required": false
},
"GROUPME_GROUP_IDS": {
"description": "comma deliminated list of target groupme group IDs",
"description": "comma deliminated list of target groupme group IDs. will list out possible options on first run.",
"required": false
},
"HEROKU_ACCESS_TOKEN": {
"description": "secret key for accessing heroku api"
"description": "secret key for accessing heroku api. I think it can be found here: https://dashboard.heroku.com/account toward the bottom, named API key."
},
"LOCAL_TIMEZONE": {
"description": "timezone",
"value": "US/Central"
},
"LAST_MESSAGE_IDS": {
"description": "comma deliminated list of the most recent message ID for each target groupme group",
"description": "comma deliminated list of the most recent message ID for each target groupme group. leave blank unless you know what you're doing.",
"required": false
},
"USE_HEROKU_HOSTING": "true",
"HEROKU_APP_ID": {
"description": "id for heroku api"
"description": "id for heroku api. leave blank until the app is created."
},
"KEYWORDS": {
"description": "GOOD STUFF: a comma deliminated list of target keywords to trigger a notification",
"description": "IMPORTANT: a comma deliminated list of target keywords to trigger a notification",
"required": false
},
"IGNORED_USERS": {
Expand All @@ -42,23 +47,23 @@
"description": "your name"
},
"EMAIL_TO_ADDRESS": {
"description": "your email address"
"description": "your receiving email address"
},
"EMAIL_FROM_ADDRESS": {
"description": "sending email address"
"description": "sending email address, default: use gmail login email"
},
"EMAIL_HOST_URL": {
"description": "sending email host url, default gmail",
"description": "sending email host url, default: use gmail",
"value": "smtp.gmail.com"
},
"EMAIL_HOST_USERNAME": {
"description": "sending email host username, default gmail login email"
"description": "sending email host username, default: use gmail login email"
},
"EMAIL_HOST_PASSWORD": {
"description": "sending email host password, default gmail login password"
"description": "sending email host password, default: use gmail login password"
},
"EMAIL_HOST_PORT": {
"description": "sending email host port, default gmail",
"description": "sending email host port, defaults: use gmail host port",
"value": 465
}
}
Expand Down
95 changes: 52 additions & 43 deletions groupMeNotifier.py
Expand Up @@ -14,6 +14,9 @@
# for timezone conversions
from pytz import timezone

# list out ID's of groups the user is in
from printGroupIDs import getIDs

# change for slightly more verbose logging
DEBUG = False

Expand Down Expand Up @@ -70,49 +73,51 @@
def main():
allMessages = []

for i, groupID in enumerate(GROUPME_GROUP_IDS):
group = {}
# list_all fails more than list().autopage()
# retrieving group by ID doesn't allow "omit" param
# without "omit" large groups overflow json request max size
for g in CLIENT.groups.list(omit="memberships").autopage():
if g.id == groupID:
group = g

if not group:
print('WARNING: skipping invalid groupID: %s' % groupID)
else:
last = LAST_MESSAGE_IDS[i]
if last == 0:
last = initializeLastID(group, i)
newMessages = group.messages.list_since(last).autopage()

# generators cannot be indexed
# update env var with most recent message id
for message in newMessages:
LAST_MESSAGE_IDS[i] = message.id
break

# add group name to individual message data
# not incl by default in Groupy wrapper
for message in newMessages:
message.group = group.name
allMessages.append(message)

matches = filterMessages(allMessages)

if not matches:
print('INFO: no new matches')
elif DEBUG:
print('\nINFO: MATCHED MESSAGES:')
for message in matches:
print(message)
if not GROUPME_GROUP_IDS:
getIDs()
else:
emailBody = buildEmail(matches)
sendEmail(emailBody, len(matches))
print('INFO: email sent with %i matches' % len(matches))
for i, groupID in enumerate(GROUPME_GROUP_IDS):
group = {}
# list_all fails more than list().autopage()
# retrieving group by ID doesn't allow "omit" param
# without "omit" large groups overflow json request max size
for g in CLIENT.groups.list(omit="memberships").autopage():
if g.id == groupID:
group = g

if not group:
print('WARNING: skipping invalid groupID: %s' % groupID)
else:
if not LAST_MESSAGE_IDS or LAST_MESSAGE_IDS[i] == 0:
LAST_MESSAGE_IDS[i] = initializeLastID(group, i)
newMessages = group.messages.list_since(last).autopage()

# generators cannot be indexed
# update env var with most recent message id
for message in newMessages:
LAST_MESSAGE_IDS[i] = message.id
break

# add group name to individual message data
# not incl by default in Groupy wrapper
for message in newMessages:
message.group = group.name
allMessages.append(message)

matches = filterMessages(allMessages)

if not matches:
print('INFO: no new matches')
elif DEBUG:
print('\nINFO: MATCHED MESSAGES:')
for message in matches:
print(message)
else:
emailBody = buildEmail(matches)
sendEmail(emailBody, len(matches))
print('INFO: email sent with %i matches' % len(matches))

updateLastSeenMessage() # will restart program on heroku
updateLastSeenMessage() # will restart program on heroku


def buildEmail(messages):
Expand Down Expand Up @@ -151,8 +156,12 @@ def sendEmail(emailBody, numMessages):

def filterMessages(messages):
matches = []
keywords = KEYWORDS.split(',')
users = IGNORED_USERS.split(',')
keywords = []
users = []
if KEYWORDS:
keywords = KEYWORDS.split(',')
if IGNORED_USERS:
users = IGNORED_USERS.split(',')

for message in messages:
if message.text is not None:
Expand Down
6 changes: 3 additions & 3 deletions printGroupIDs.py
Expand Up @@ -26,10 +26,10 @@
raise Exception('***all necessary global config not defined***')


def main():
def getIDs():
for group in CLIENT.groups.list(omit="memberships").autopage():
print(group.name + ' (' + group.id + ')')


if __name__ == '__main__':
main()
if __name__ == '__getIDs__':
getIDs()

0 comments on commit 7504ab7

Please sign in to comment.