2018-10-08 21:11:51 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
import mastodon
|
2018-10-14 02:58:58 -04:00
|
|
|
import os, random, re, json
|
2019-01-11 07:47:42 -05:00
|
|
|
import functions
|
2018-10-08 21:11:51 -04:00
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
|
2018-10-14 02:58:58 -04:00
|
|
|
cfg = json.load(open('config.json', 'r'))
|
|
|
|
|
2018-10-08 21:11:51 -04:00
|
|
|
client = mastodon.Mastodon(
|
2018-10-25 21:35:14 -04:00
|
|
|
client_id=cfg['client']['id'],
|
|
|
|
client_secret=cfg['client']['secret'],
|
|
|
|
access_token=cfg['secret'],
|
|
|
|
api_base_url=cfg['site'])
|
2018-10-08 21:11:51 -04:00
|
|
|
|
|
|
|
def extract_toot(toot):
|
2019-01-11 07:56:35 -05:00
|
|
|
text = functions.extract_toot(toot)
|
|
|
|
text = re.sub(r"^@[^@]+@[^ ]+\s*", r"", text) #remove the initial mention
|
|
|
|
text = text.lower() #treat text as lowercase for easier keyword matching (if this bot uses it)
|
2018-10-08 21:11:51 -04:00
|
|
|
return text
|
|
|
|
|
|
|
|
class ReplyListener(mastodon.StreamListener):
|
2019-01-11 07:56:35 -05:00
|
|
|
def on_notification(self, notification): #listen for notifications
|
|
|
|
if notification['type'] == 'mention': #if we're mentioned:
|
|
|
|
acct = "@" + notification['account']['acct'] #get the account's @
|
2018-10-08 21:11:51 -04:00
|
|
|
post_id = notification['status']['id']
|
|
|
|
mention = extract_toot(notification['status']['content'])
|
2019-01-11 07:56:35 -05:00
|
|
|
toot = functions.make_toot(True)['toot'] #generate a toot
|
|
|
|
toot = acct + " " + toot #prepend the @
|
|
|
|
print(acct + " says " + mention) #logging
|
2018-10-27 09:25:46 -04:00
|
|
|
visibility = notification['status']['visibility']
|
|
|
|
if visibility == "public":
|
|
|
|
visibility = "unlisted"
|
2019-01-11 07:56:35 -05:00
|
|
|
client.status_post(toot, post_id, visibility=visibility, spoiler_text = cfg['cw']) #send toost
|
|
|
|
print("replied with " + toot) #logging
|
2018-10-08 21:11:51 -04:00
|
|
|
|
|
|
|
rl = ReplyListener()
|
2019-01-11 07:56:35 -05:00
|
|
|
client.stream_user(rl) #go!
|