Browse Source

Add auto responder for follows

master 20180409.2
KemoNine 3 years ago
parent
commit
6ea904fdc7
  1. 35
      botstreamlisteners.py
  2. 16
      configs/example_follow_auto_respond_bot.service
  3. 25
      configs/example_follow_auto_respond_config.yaml
  4. 16
      toot_bot.py

35
botstreamlisteners.py

@ -3,6 +3,7 @@
"""Different stream listener implementations used by bots."""
import copy
import pprint
import sqlite3
from toot import toot
@ -94,6 +95,7 @@ CREATE TABLE IF NOT EXISTS notification_cache (
self.cursor.execute('select max(toot_id) from toot_cache;')
last_seen_toot_id = self.cursor.fetchone()[0]
if not last_seen_toot_id:
last_seen_toot_id = 0
first_page = mastodon.timeline_public(since_id=last_seen_toot_id)
@ -177,8 +179,11 @@ CREATE TABLE IF NOT EXISTS notification_cache (
return
# Cache toot
self.cursor.execute('insert into toot_cache values (?)', (toot_id, ))
self.conn.commit()
try:
self.cursor.execute('insert into toot_cache values (?)', (toot_id, ))
self.conn.commit()
except:
pass
self.process_toot(status)
def on_notification(self, notification):
@ -296,6 +301,32 @@ class AutoRespondBot(PublicTimelineBot):
self.cursor.execute('insert into toot_cache values (?)', (autoresponse_id, ))
self.conn.commit()
class NewFollowerAutoRespondBot(PublicTimelineBot):
"""User timeline bot that auto responds to new followers"""
def setup_cache(self):
"""No extra logic necessary"""
pass
def process_notification(self, notification):
"""Method that handles notifications."""
if not notification['type'] == 'follow':
return
# If auto-responses to followers should be limited to local accounts
if 'local_only' in self.bot_config['config'] and '@' in notification['account']['acct']:
return
if not notification['account']['id'] == self.account_id:
# Send autoresponse toot
autoresponse = toot(self.bot_config, username=notification['account']['username'])
autoresponse_id = autoresponse['id']
# Cache newly sent toot to prevent any looping
try:
self.cursor.execute('insert into toot_cache values (?)', (autoresponse_id, ))
self.conn.commit()
except:
pass
class FollowBot(PublicTimelineBot):
"""Public timeline bot that follows newly seen users"""

16
configs/example_follow_auto_respond_bot.service

@ -0,0 +1,16 @@
# drop at /etc/systemd/system/mastodon_follow_autorespond_bot.service
[Unit]
Description=mastodon-follow-autorespond-bot
After=network.target
[Service]
Type=simple
User=mastodon
WorkingDirectory=/home/mastodon/bot
ExecStart=/usr/bin/python3 /home/mastodon/bot/toot_bot.py --config /home/mastodon/bot/follow_autorespond_bot.yaml followautorespond
TimeoutSec=15
Restart=always
[Install]
WantedBy=multi-user.target

25
configs/example_follow_auto_respond_config.yaml

@ -0,0 +1,25 @@
# Setup general config for the bot (registered name, instance url, credential cache)
# This section is REQUIRED
config:
app_name: autorespond to new followers
api_base_url: https://instance.com
client_cred_file: /home/mastodon/bot/client_cred.secret
user_cred_file: /home/mastodon/bot/user_cred.secret
local_only: True #OMIT for non-local too
# Setup the cache file used for ensuring users aren't welcomed more than once
stream:
cache_file: /home/mastodon/bot/follow_autorespond_cache.db
# Setup the toots that will be sent
# These static toots will ALWAYS be sent
# This section is REQUIRED
toot:
visibility: public # One of: public, unlisted, private, or direct
cw_text: Hello @{0.username}!
toot_text: |
I'm your friendly autoresponding toot bot
Hi there! :sunglasses:
{0.mentions}

16
toot_bot.py

@ -15,7 +15,7 @@ import feedparser
from mastodon import Mastodon
from ruamel.yaml import YAML
from botstreamlisteners import WelcomeBot, AutoRespondBot, FollowBot
from botstreamlisteners import WelcomeBot, AutoRespondBot, FollowBot, NewFollowerAutoRespondBot
from toot import toot
@ -171,6 +171,15 @@ def follow(config):
mastodon.user_stream(FollowBot(config))
def followautorespond(config):
"""Auto respond to new followers"""
mastodon = Mastodon(
client_id=config['config']['client_cred_file'],
access_token=config['config']['user_cred_file'],
api_base_url=config['config']['api_base_url']
)
mastodon.user_stream(NewFollowerAutoRespondBot(config))
def autorespond(config):
"""Auto respond to @'s to the configured account"""
mastodon = Mastodon(
@ -212,6 +221,7 @@ def handle_command_line():
subparsers.add_parser('rss', help='cross post articles from an rss feed')
subparsers.add_parser('welcome', help='Run the welcome bot (service)')
subparsers.add_parser('autorespond', help='Run the auto respond bot (service)')
subparsers.add_parser('followautorespond', help='Run the new follower auto respond bot (service)')
subparsers.add_parser('follow', help='Run the follow bot (service) - UNSUPPORTED DON\'T RUN !!!!!!!!!!!!')
# Parse CLI arguments
@ -273,6 +283,10 @@ def handle_command_line():
if args.command == 'welcome':
welcome(config)
# Deal with new follower command
if args.command == 'followautorespond':
followautorespond(config)
# Deal with autorespond command
if args.command == 'autorespond':
autorespond(config)

Loading…
Cancel
Save