ticketfrei3/kibicara/platforms/email/bot.py

43 lines
1.5 KiB
Python
Raw Normal View History

2020-07-05 21:49:32 +00:00
# Copyright (C) 2020 by Maike <maike@systemli.org>
#
# SPDX-License-Identifier: 0BSD
from kibicara.platforms.email.model import EmailSubscribers, Email
from kibicara.platformapi import Censor, Spawner
from kibicara.email import send_email
from kibicara.config import config
from kibicara.webapi.admin import to_token
2020-07-05 21:49:32 +00:00
class EmailBot(Censor):
def __init__(self, email_model):
super().__init__(email_model.hood)
self.model = email_model
self.messages = []
async def run(self):
""" Loop which waits for new messages and sends emails to all subscribers. """
2020-07-05 21:49:32 +00:00
while True:
message = await self.receive()
for subscriber in EmailSubscribers(hood=self.hood.id):
payload = {
'email': subscriber.email,
'hood': self.hood.id,
}
token = to_token(email=subscriber.email, hood=self.hood.id)
2020-07-06 17:14:12 +00:00
unsubscribe_link = (
config['root_url']
+ 'api/'
+ self.model.id
+ '/email/unsubscribe/'
+ token
2020-07-06 17:14:12 +00:00
)
message.text += (
"\n\n--\nIf you want to stop receiving these mails, "
"follow this link: " + unsubscribe_link
)
send_email(subscriber.email, "Kibicara " + self.hood.name, body=message.text)
2020-07-05 21:49:32 +00:00
spawner = Spawner(Email, EmailBot)