我目前有一个reddit机器人,它从子reddit接收关键字,然后向我发送Slack通知。你知道吗
向我发送通知的当前代码是
for kw in keywords:
if kw.lower() in comment.body.lower(): # case insensitive check
already_alerted_submissions.append(comment.submission.id)
msg = '[Keyword *{0}* detected](http://www.reddit.com{1})'.format(
kw, comment.permalink)
slack_data = {'text': msg, 'mrkdwn': True}
因此,它目前正在从
keywords = ['camera', 'nikon', 'canon', 'campus'] # case insensitive
我想知道是否有可能有两个单独的关键字列表中的文件,如以下
keywords = ['camera', 'nikon', 'canon', 'campus'] # case insensitive
keywords_color = ['red', 'blue', 'green', 'black'] # case insensitive
如果在线程中检测到“camera”这个词,它就会像当前一样发布消息。你知道吗
但如果在同一条评论中,它同时从两条评论中检测到一个关键字 关键字和关键字颜色
它可以在slack中发布相同的消息,但另一行类似于“Color was detected”
因此,在上面的示例中,slack中的消息如下所示。你知道吗
1. [Keyword *camera* detected]
(http://www.reddit.com/r/camera/comments/9yg8mt/goodcameras
I just got a great CAMERA today, it is awesome
或者,如果它同时检测到“关键字”和“关键字颜色”,它将如下所示
2. 1. [Keyword *camera* detected]
(http://www.reddit.com/r/camera/comments/9yg8mt/goodcameras
I just got a great CAMERA today, it is a RED one and its awesome
**Colour was detected**
这可能吗?任何帮助都将不胜感激!你知道吗
文件的完整纸条如下:
def main():
alerted_comments = get_list_from_pickle('alerted_comments.pickle')
try:
for comment in comment_stream:
if comment.id in alerted_comments:
continue
if comment.author: # if comment author hasn't deleted
if comment.author.name in ignore_users:
continue
for kw in keywords:
if kw.lower() in comment.body.lower(): # case insensitive check
alerted_comments.append(comment.id)
while len(alerted_comments) > 100:
del alerted_comments[0]
with open('alerted_comments.pickle', 'wb') as fp:
pickle.dump(alerted_comments, fp)
for kw in keywords:
if kw.lower() in comment.body.lower(): # case insensitive check
alerted_comments.append(comment.submission.id)
msg = '[Keyword *{0}* detected](http://www.reddit.com{1})'.format(
kw, comment.permalink)
slack_data = {'text': msg, 'mrkdwn': True}
response = requests.post('https://hooks.slack.com/services/BE72P09A9/xxxxxxxxx78',
data=json.dumps(slack_data), headers={'Content-Type': 'application/json'})
if response.status_code != 200:
raise ValueError('Request to slack returned an error %s, the response is:\n%s' % (
response.status_code, response.text))
except Exception as e:
print('There was an error: ' + str(e))
sleep(60) # wait for 60 seconds before restarting
main()
if __name__ == '__main__':
main()
这是未经测试的,但给你:
如果你有任何问题,请告诉我。你知道吗
相关问题 更多 >
编程相关推荐