-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathinbox_handler.py
More file actions
76 lines (59 loc) · 1.97 KB
/
inbox_handler.py
File metadata and controls
76 lines (59 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""Responds to comments fom the inbox"""
import logging
import re
import praw
import prawcore
import reddit_instantiator
import my_i18n as i18n
NEGATIVE_PHRASES = [
'bad bot',
'delete this',
'no',
]
POSITIVE_PHRASES = [
'good bot',
]
def respond_to_comment(comment):
if comment.type == 'username_mention':
pass
elif comment.type == 'comment_reply':
handle_comment_reply(comment)
def handle_comment_reply(comment):
# author = None
# if comment.distinguished == 'moderator':
# author = 'moderator'
# else:
# author = f'/u/{comment.author.name}'
# logging.info(f'Received inbox comment by {author}: {comment.body}')
reddit = reddit_instantiator.get_reddit_instance()
if not isinstance(comment, praw.models.Comment):
return
sentiment = check_sentiment(comment.body)
try:
if sentiment == 'positive':
respond_to_positive_sentiment(comment)
elif sentiment == 'negative':
respond_to_negative_sentiment(comment)
if sentiment is not None:
reddit.inbox.mark_read([comment])
except prawcore.exceptions.Forbidden as e:
if e.response.reason == 'Forbidden':
logging.info(f'Got 403 Forbidden error, probably because the bot is banned in {comment.subreddit_name_prefixed}')
reddit.inbox.mark_read([comment])
else:
raise
def check_sentiment(text):
cleaned_text = re.sub(r'[^\w\s]', '', text, flags=re.S).strip().lower()
if cleaned_text in NEGATIVE_PHRASES:
return 'negative'
elif cleaned_text in POSITIVE_PHRASES:
return 'positive'
return None
def respond_to_positive_sentiment(comment):
# comment.reply('🤖🌹')
pass
def respond_to_negative_sentiment(comment):
# source_subreddit = comment.subreddit.display_name
# text = i18n.get_translated_string('RESPOND_TO_NEGATIVE_SENTIMENT', subreddit=source_subreddit)
# return comment.reply(text)
pass