crash reports are now sent via mail. documented config.toml.example

This commit is contained in:
b3yond 2018-01-04 11:02:42 +01:00
parent df32f3c614
commit 0f6fc60b5e
4 changed files with 88 additions and 40 deletions

View file

@ -1,28 +1,32 @@
[mapp] [mapp]
name = 'yourcity_ticketfrei' # :todo: where do you receive the app name? # The bot registers a mastodon app automatically to acquire OAuth keys.
name = 'yourcity_ticketfrei' # What you want the app to be called
[muser] [muser]
email = 'youremail@server.tld' # E-mail address of your Mastodon account email = 'youremail@server.tld' # E-mail address of your Mastodon account
password = 'yourpassword' # Password of your Mastodon account password = 'yourpassword' # Password of your Mastodon account
server = 'yourmastodoninstance' # Instance where you have your Mastodon account server = 'yourmastodoninstance' # Instance where you have your Mastodon account
[tapp] [tapp]
# You get those keys when you follow these steps:
# https://developer.twitter.com/en/docs/basics/authentication/guides/access-tokens
consumer_key = "OD0CLn6twBxHjN2DqMkKuSvli" consumer_key = "OD0CLn6twBxHjN2DqMkKuSvli"
consumer_secret = "XkvbViwjBWoWoJzIlseJLXmg2fqluq4HYqvwOwoSHGwxdTNi4l" consumer_secret = "XkvbViwjBWoWoJzIlseJLXmg2fqluq4HYqvwOwoSHGwxdTNi4l"
shutdown_contact_userid = 801098086005243904 [tuser]
shutdown_contact_screen_name = 'links_tech' # You get those keys when you follow these steps:
# https://developer.twitter.com/en/docs/basics/authentication/guides/access-tokens
[tuser] access_token_key = "876046057721008128-J35moxFXUvLb24MnaMVbVpqiEtxBlcc"
access_token_key = "876046057721008128-J35moxFXUvLb24MnaMVbVpqiEtxBlcc"
access_token_secret = "I7PQZMHuJDS5WslgUhqEeZbEWGhwLhmOetvwFoTn8YDKW" access_token_secret = "I7PQZMHuJDS5WslgUhqEeZbEWGhwLhmOetvwFoTn8YDKW"
[mail] [mail]
# This is the mail the bot uses to send emails.
mailserver = "smtp.riseup.net" mailserver = "smtp.riseup.net"
port = 587
user = "ticketfrei" user = "ticketfrei"
passphrase = "sup3rs3cur3" passphrase = "sup3rs3cur3"
# If you want to receive crash reports (so you can restart the bot
# when it breaks down), you should specify a contact email address:
#contact = "your_mail@riseup.net"
# [trigger] # [trigger]
@ -30,6 +34,7 @@ passphrase = "sup3rs3cur3"
# badlists are one badword per line. # badlists are one badword per line.
# a message musst match at least one regex in goodlist and contain none of the badwords. # a message musst match at least one regex in goodlist and contain none of the badwords.
# the variables mention the directory where the lists are located, not the filenames. # the variables mention the directory where the lists are located, not the filenames.
# These are the default folders. If you want to specify differents folders, uncomment
# goodlist_path = 'goodlists' # those lines and enter relative paths.
# blacklist_path = 'blacklists' #goodlist_path = 'goodlists'
#blacklist_path = 'blacklists'

View file

@ -8,14 +8,17 @@ import re
import time import time
import trigger import trigger
import logger import logger
import sendmail
import traceback
import sys
class RetootBot(object): class RetootBot(object):
def __init__(self, config, filter, logger): def __init__(self, config, filter, logger):
self.config = config self.config = config
self.filter = filter self.filter = filter
self.register() self.client_id = self.register()
self.login() self.m = self.login()
# load state # load state
try: try:
@ -24,31 +27,40 @@ class RetootBot(object):
except IOError: except IOError:
self.seen_toots = set() self.seen_toots = set()
# intialize shutdown contact
try:
self.no_shutdown_contact = False
self.contact = self.config['mail']['contact']
except KeyError:
self.no_shutdown_contact = True
self.logger = logger self.logger = logger
def register(self): def register(self):
self.client_id = os.path.join( client_id = os.path.join(
'appkeys', 'appkeys',
self.config['mapp']['name'] + self.config['mapp']['name'] +
'@' + self.config['muser']['server'] '@' + self.config['muser']['server']
) )
if not os.path.isfile(self.client_id): if not os.path.isfile(client_id):
mastodon.Mastodon.create_app( mastodon.Mastodon.create_app(
self.config['mapp']['name'], self.config['mapp']['name'],
api_base_url=self.config['muser']['server'], api_base_url=self.config['muser']['server'],
to_file=self.client_id to_file=client_id
) )
return self.client_id
def login(self): def login(self):
self.m = mastodon.Mastodon( m = mastodon.Mastodon(
client_id=self.client_id, client_id=self.client_id,
api_base_url=self.config['muser']['server'] api_base_url=self.config['muser']['server']
) )
self.m.log_in( m.log_in(
self.config['muser']['email'], self.config['muser']['email'],
self.config['muser']['password'] self.config['muser']['password']
) )
return m
def retoot(self, toots=()): def retoot(self, toots=()):
# toot external provided messages # toot external provided messages
@ -83,17 +95,44 @@ class RetootBot(object):
# return mentions for mirroring # return mentions for mirroring
return retoots return retoots
def shutdown(self, tb):
""" If something breaks, it shuts down the bot and messages the owner.
"""
logmessage = "Shit went wrong, closing down.\n" + tb + "\n\n"
if self.no_shutdown_contact:
self.logger.log(logmessage)
return
logmessage = logmessage + " Sending message to " + self.contact
self.logger.log(logmessage)
# feature yet only in RetweetBot:
# self.save_last_mention()
try:
mailer = sendmail.Mailer(self.config)
mailer.send(tb, self.contact, "Ticketfrei Crash Report")
except:
# traceback.print_exc()
self.logger.log(traceback.extract_tb(sys.exc_info()[2]))
print()
if __name__ == '__main__': if __name__ == '__main__':
# read config in TOML format (https://github.com/toml-lang/toml#toml) # read config in TOML format (https://github.com/toml-lang/toml#toml)
with open('config.toml') as configfile: with open('config.toml') as configfile:
config = toml.load(configfile) config = toml.load(configfile)
filter = trigger.Trigger(config) trigger = trigger.Trigger(config)
logger = logger.Logger() logger = logger.Logger()
bot = RetootBot(config, filter, logger) bot = RetootBot(config, trigger, logger)
while True: try:
bot.retoot() while True:
time.sleep(1) bot.retoot()
time.sleep(1)
except KeyboardInterrupt:
print("Good bye! Remember to restart the bot.")
except:
tb = traceback.extract_tb(sys.exc_info()[2])
bot.logger.log(tb)
print()
bot.shutdown(tb)

View file

@ -8,6 +8,7 @@ import trigger
from time import sleep from time import sleep
import traceback import traceback
import logger import logger
import sendmail
class RetweetBot(object): class RetweetBot(object):
@ -46,8 +47,7 @@ class RetweetBot(object):
# intialize shutdown contact # intialize shutdown contact
try: try:
self.no_shutdown_contact = False self.no_shutdown_contact = False
self.screen_name = \ self.contact = self.config['mail']['contact']
self.config['tapp']['shutdown_contact_screen_name']
except KeyError: except KeyError:
self.no_shutdown_contact = True self.no_shutdown_contact = True
@ -55,9 +55,8 @@ class RetweetBot(object):
self.last_mention = self.get_history(self.historypath) self.last_mention = self.get_history(self.historypath)
self.trigger = trigger self.trigger = trigger
self.waitcounter = 0 self.waitcounter = 0
self.logger = logger
self.logger = logger
def get_api_keys(self): def get_api_keys(self):
""" """
@ -217,18 +216,21 @@ class RetweetBot(object):
# Return Retweets for tooting on mastodon # Return Retweets for tooting on mastodon
return mastodon return mastodon
def shutdown(self): def shutdown(self, tb):
""" If something breaks, it shuts down the bot and messages the owner. """ If something breaks, it shuts down the bot and messages the owner.
:param tb: (string) traceback
""" """
logmessage = "Shit went wrong, closing down." logmessage = "Shit went wrong, closing down.\n" + tb + "\n\n"
if self.screen_name:
logmessage = logmessage + " Sending message to " + self.screen_name
self.logger.log(logmessage)
if self.no_shutdown_contact: if self.no_shutdown_contact:
self.logger.log(logmessage)
return return
logmessage = logmessage + " Sending message to " + self.contact
self.logger.log(logmessage)
self.save_last_mention() self.save_last_mention()
try: try:
self.api.send_direct_message(self.screen_name, "Help! I broke down. restart me pls :$") mailer = sendmail.Mailer(self.config)
mailer.send(tb, self.contact, "Ticketfrei Crash Report")
except: except:
# traceback.print_exc() # traceback.print_exc()
self.logger.log(traceback.extract_tb(sys.exc_info()[2])) self.logger.log(traceback.extract_tb(sys.exc_info()[2]))
@ -251,6 +253,7 @@ if __name__ == "__main__":
except KeyboardInterrupt: except KeyboardInterrupt:
print("Good bye! Remember to restart the bot.") print("Good bye! Remember to restart the bot.")
except: except:
bot.logger.log(traceback.extract_tb(sys.exc_info()[2])) tb = traceback.extract_tb(sys.exc_info()[2])
bot.logger.log(tb)
print() print()
bot.shutdown() bot.shutdown(tb)

View file

@ -35,5 +35,6 @@ if __name__ == '__main__':
except KeyboardInterrupt: except KeyboardInterrupt:
print("Good bye. Remember to restart the bot!") print("Good bye. Remember to restart the bot!")
except: except:
tbot.logger(traceback.extract_tb(sys.exc_info()[2])) tb = traceback.extract_tb(sys.exc_info()[2])
tbot.shutdown() tbot.logger.log(tb)
tbot.shutdown(tb)