Skip to content

Commit

Permalink
optional custom message and optional keyword filtering (#22)
Browse files Browse the repository at this point in the history
* custom message and keyword

* remove junk

* bug fix
  • Loading branch information
NNTin authored Sep 14, 2018
1 parent 6ab7362 commit 2af85f1
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
8 changes: 8 additions & 0 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@
"INCLUDE_USER_REPLY": {
"description": "Include replies FROM your tracked twitter user to other twitter users",
"value": "True"
},
"CUSTOM_MESSAGE": {
"description": "OPTIONAL: Custom message. Useful for pinging @everyone, a role or individual discord members (syntax: <@ROLE_OR_MEMBER_ID>)",
"required": false
},
"KEYWORDS": {
"description": "OPTIONAL: Will only post tweets with certain keywords. Comma separate keyword sets. Separate a keyword set with +. Example: world+hello, dota--> hello guys! ✘, what in the world ✘, my first tweet: hello world ✔, let's play some dota! ✔",
"required": false
}
}
}
13 changes: 8 additions & 5 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from dataIO import fileIO
import os

# todo: add mongoDB support

false_strings = ["false", "False", "f", "F", "0", ""]
false_strings = ["false", "False", "f", "F", "0", "", "n", "N", "no", "No", "NO", "FALSE"]

if fileIO("data.json", "check"):
data_json = fileIO("data.json", "load")
Expand All @@ -20,9 +18,14 @@
"IncludeReplyToUser": False if os.environ["INCLUDE_REPLY_TO_USER"] in false_strings else True,
"IncludeRetweet": False if os.environ["INCLUDE_RETWEET"] in false_strings else True,
"IncludeUserReply": False if os.environ["INCLUDE_USER_REPLY"] in false_strings else True,
"webhook_urls": os.environ.get("WEBHOOK_URL", []).replace(" ", "").split(","),
"twitter_ids": os.environ.get("TWITTER_ID", []).replace(" ", "").split(",")
"webhook_urls": os.environ["WEBHOOK_URL"].replace(" ", "").split(","),
"twitter_ids": os.environ["TWITTER_ID"].replace(" ", "").split(","),
"custom_message": os.environ.get("CUSTOM_MESSAGE", None)
}
],
"twitter_ids": []
}
keyword_sets = os.environ.get("KEYWORDS", None)
if keyword_sets:
keyword_sets = [keyword_set.split("+") for keyword_set in keyword_sets.replace(" ", "").split(",")]
data_json["Discord"]["keyword_sets"] = keyword_sets
28 changes: 28 additions & 0 deletions data_example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"Discord" : [
{
"custom_message": "Pinging <@1234567890>",
"keyword_sets": [
["dota"],
["hello", "world"],
["Global", "Offensive"]
],
"IncludeReplyToUser" : false,
"IncludeRetweet" : false,
"IncludeUserReply" : false,
"twitter_ids" : [
""
],
"webhook_urls" : [
"https://ptb.discordapp.com/api/webhooks/1234567890/XXXXXXXXXXXXXXXXXXXXXXXXXX"
]
}
],
"Twitter" : {
"access_token" : "X-X",
"access_token_secret" : "X",
"consumer_key" : "X",
"consumer_secret" : "X"
},
"twitter_ids" : []
}
14 changes: 13 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ def _on_status(self, status):
else:
text = data['text']

if "keyword_sets" in data_discord and data_discord["keyword_sets"] is not None:
for keyword_set in data_discord["keyword_sets"]:
keyword_present = [keyword.lower() in text.lower() for keyword in keyword_set]
keyword_set_present = all(keyword_present)
if keyword_set_present:
break
if not keyword_set_present:
break

for url in data['entities']['urls']:
if url['expanded_url'] is None:
continue
Expand Down Expand Up @@ -159,7 +168,10 @@ def _on_status(self, status):
if match:
webhook = Webhook.partial(match.group("id"), match.group("token"), adapter=RequestsWebhookAdapter())
try:
webhook.send(embed=embed)
if "custom_message" in data_discord and data_discord["custom_message"] is not None:
webhook.send(embed=embed, content=data_discord["custom_message"])
else:
webhook.send(embed=embed)
except discord.errors.HTTPException as error:
print('---------Error---------')
print('discord.errors.HTTPException')
Expand Down

0 comments on commit 2af85f1

Please sign in to comment.