Skip to content

Commit

Permalink
send welcome email when subscribing
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas.bernhard committed Nov 16, 2024
1 parent 4954704 commit ba30d60
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 3 deletions.
7 changes: 7 additions & 0 deletions infrastructure/api/lambda.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ resource "aws_lambda_function" "api" {
memory_size = 512

role = aws_iam_role.api.arn

environment {
variables = {
GMAIL_SENDER_ADDRESS = var.gmail_sender_address
GMAIL_SENDER_PASSWORD = var.gmail_sender_password
}
}
}

resource "aws_iam_role" "api" {
Expand Down
8 changes: 8 additions & 0 deletions infrastructure/api/vars.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,12 @@ variable "dynamodb_email_arn" {

variable "dynamodb_polls_arn" {
type = string
}

variable "gmail_sender_address" {
type = string
}

variable "gmail_sender_password" {
type = string
}
2 changes: 2 additions & 0 deletions infrastructure/modules.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ module "api" {
source = "./api"
dynamodb_email_arn = aws_dynamodb_table.emails.arn
dynamodb_polls_arn = aws_dynamodb_table.polls.arn
gmail_sender_address = jsondecode(data.aws_secretsmanager_secret_version.gmail_credentials.secret_string)["GMAIL_SENDER_ADDRESS"]
gmail_sender_password = jsondecode(data.aws_secretsmanager_secret_version.gmail_credentials.secret_string)["GMAIL_SENDER_PASSWORD"]
}
17 changes: 17 additions & 0 deletions schafkopf/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from fastapi import FastAPI

from schafkopf.api.models import SubscribeRequest, SubscribeResponse, PollResponse, SubscribeCountResponse
from schafkopf.core import gmail, bitpoll
from schafkopf.core.dynamodb import email_table, poll_table

app = FastAPI(
Expand All @@ -17,6 +18,22 @@ def subscribe_to_schafkopf_rounds(req: SubscribeRequest) -> SubscribeResponse:
import boto3
dynamodb = boto3.resource("dynamodb")
email_table.add(dynamodb, req.to_email_item())

poll = poll_table.load(dynamodb)
poll_id = bitpoll.get_website_from_poll_id(poll.running_poll_id)
if poll.poll_is_running():
print("Send invite email with poll link")
gmail.send_welcome_with_running_bitpoll(
receiver=req.email,
bitpoll_link=poll_id,
)
else:
print('Send invite email with next date')
gmail.send_welcome_with_meeting_invitation(
receiver=req.email,
start=poll.next_schafkopf_event,
bitpoll_link=poll_id
)
return SubscribeResponse(email=req.email)


Expand Down
31 changes: 28 additions & 3 deletions schafkopf/core/gmail.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ def send_bitpoll_invitation(receivers: List[str], bitpoll_link: str):
)


def send_welcome_with_running_bitpoll(receiver: str, bitpoll_link: str):
html = load_html_template(f"{env.BASE_PATH}/templates/welcome_with_poll_running.html")
html = html.replace("YOUR_BITPOLL_LINK_HERE", bitpoll_link)

send(
receivers=[receiver],
subject="Welcome to our Schafkopf Round",
body=html
)

def send_schafkopf_meeting_invitation(receivers: List[str], start: datetime, bitpoll_link: str):
html = load_html_template(f"{env.BASE_PATH}/templates/schafkopf_scheduled.html")
html = html.replace("SCHEDULED_DATE_PLACEHOLDER", format_datetime(start))
Expand All @@ -32,7 +42,22 @@ def send_schafkopf_meeting_invitation(receivers: List[str], start: datetime, bit
body=html,
attachment=create_calendar_entry(
summary="[at] Schafkopfen",
start=start, end=start.replace(hour=23, minute=0),
start=start,
)
)

def send_welcome_with_meeting_invitation(receiver: str, start: datetime, bitpoll_link: str):
html = load_html_template(f"{env.BASE_PATH}/templates/welcome_with_event_scheduled.html")
html = html.replace("SCHEDULED_DATE_PLACEHOLDER", format_datetime(start))
html = html.replace("YOUR_BITPOLL_LINK_HERE", bitpoll_link)

send(
receivers=[receiver],
subject="Welcome to our Schafkopf Round",
body=html,
attachment=create_calendar_entry(
summary="[at] Schafkopfen",
start=start
)
)

Expand Down Expand Up @@ -67,15 +92,15 @@ def send(
smtpserver.close()


def create_calendar_entry(start: datetime, end: datetime, summary: str) -> MIMEBase:
def create_calendar_entry(start: datetime, summary: str) -> MIMEBase:
ics_content = f"""BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Schafkopf Scheduler//[at] Schafkopf
BEGIN:VEVENT
UID:{env.get_gmail_sender_address()}
DTSTAMP:{datetime.now().strftime('%Y%m%dT%H%M%SZ')}
DTSTART:{start.strftime('%Y%m%dT%H%M%SZ')}
DTEND:{end.strftime('%Y%m%dT%H%M%SZ')}
DTEND:{start.replace(hour=23, minute=0).strftime('%Y%m%dT%H%M%SZ')}
SUMMARY:[at] Schafkopfen
DESCRIPTION:{summary}
END:VEVENT
Expand Down
55 changes: 55 additions & 0 deletions templates/welcome_with_event_scheduled.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Welcome to our Schafkopf-Round!</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f7f7f7;
color: #333;
margin: 0;
padding: 20px;
}
h1 {
color: #fc6805;
}
.button {
display: inline-block;
padding: 10px 20px;
font-size: 16px;
color: white;
background-color: #fc6805;
text-decoration: none;
border-radius: 5px;
}
.footer {
margin-top: 20px;
font-size: 12px;
color: #777;
}
</style>
</head>
<body>
<h1>🃏Welcome to our round🃏</h1>
<p>We are very happy to have you</p>
<p>Our bot will create new polls every two weeks and monitor it. </p>
<p>Once a day with the most participants (minimum 4) has been found, it will send out an invitation to everyone</p>
<p>Keep in mind that the bot is currently not able to find and book a restaurant, so please take care of it yourself</p>
<br/>
<p>You can always look up the current state on our website</p>
<p><a href="https://schafkopf.lukas-bernhard.de/">https://schafkopf.lukas-bernhard.de/</a></p>
<br/>
<p>Feel free to join our next scheduled event at </p>
<h2><strong>SCHEDULED_DATE_PLACEHOLDER!</strong></h2>
<br/>
<p>Cheers,</p>
<p>Your Favorite Schafkopf Organizer</p>
<br/>
<div class="footer">
<p>Host: <a href="https://schafkopf.lukas-bernhard.de/">https://schafkopf.lukas-bernhard.de/</a></p>
<p><a href="https://schafkopf.lukas-bernhard.de/unsubscribe?email=RECEIVER_EMAIL">Unsubscribe</a></p>
</div>
</body>
</html>
55 changes: 55 additions & 0 deletions templates/welcome_with_poll_running.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Welcome to our Schafkopf-Round!</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f7f7f7;
color: #333;
margin: 0;
padding: 20px;
}
h1 {
color: #fc6805;
}
.button {
display: inline-block;
padding: 10px 20px;
font-size: 16px;
color: white;
background-color: #fc6805;
text-decoration: none;
border-radius: 5px;
}
.footer {
margin-top: 20px;
font-size: 12px;
color: #777;
}
</style>
</head>
<body>
<h1>🃏Welcome to our round🃏</h1>
<p>We are very happy to have you</p>
<p>Our bot will create new polls every two weeks and monitor it. </p>
<p>Once a day with the most participants (minimum 4) has been found, it will send out an invitation to everyone</p>
<p>Keep in mind that the bot is currently not able to find and book a restaurant, so please take care of it yourself</p>
<br/>
<p>You can always look up the current state on our website</p>
<p><a href="https://schafkopf.lukas-bernhard.de/">https://schafkopf.lukas-bernhard.de/</a></p>
<br/>
<p>Currently, we have a poll running, so </p>
<p><a href="YOUR_BITPOLL_LINK_HERE" class="button">Vote Now!</a></p>
<br/>
<p>Cheers,</p>
<p>Your Favorite Schafkopf Organizer</p>
<br/>
<div class="footer">
<p>Host: <a href="https://schafkopf.lukas-bernhard.de/">https://schafkopf.lukas-bernhard.de/</a></p>
<p><a href="https://schafkopf.lukas-bernhard.de/unsubscribe?email=RECEIVER_EMAIL">Unsubscribe</a></p>
</div>
</body>
</html>

0 comments on commit ba30d60

Please sign in to comment.