Skip to content

Instantly share code, notes, and snippets.

@BryceStevenWilley
Created August 29, 2021 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BryceStevenWilley/45570af2138b744e8e28ec0fd7421345 to your computer and use it in GitHub Desktop.
Save BryceStevenWilley/45570af2138b744e8e28ec0fd7421345 to your computer and use it in GitHub Desktop.
Murder Mystery Party Bot Emailer
from __future__ import print_function
import pickle
import os.path
import base64
import json
import datetime
import random
from urllib.error import HTTPError
from email.mime.text import MIMEText
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
# https://realpython.com/python-send-email/#getting-started
# https://developers.google.com/gmail/api/guides/sending
# https://developers.google.com/gmail/api/quickstart/python
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/gmail.send']
# You need to have a `data.json` file that's a JSON object of Names to their emails.
def login():
"""
Makes the gmail client object to use
"""
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
return build('gmail', 'v1', credentials=creds)
def create_message(sender, to, subject, message_text):
message = MIMEText(message_text)
message['to'] = to
message['from'] = sender
message['subject'] = subject
return {'raw': base64.urlsafe_b64encode(message.as_bytes()).decode()}
def send_message(service, user_id, message):
try:
message = (service.users().messages().send(userId=user_id, body=message).execute())
print('Message Id: {}'.format(message['id']))
return message
except HTTPError as error:
print('An error occurred: {}'.format(error))
def main():
with open('data.json', 'r') as f:
to_send = json.load(f)
guilty_list = ['guilty'] + (len(to_send) - 1) * ['innocent']
random.shuffle(guilty_list)
sending_results = [{'name': d[0], 'status': g, 'email': d[1]}
for d, g in zip(to_send.items(), guilty_list)]
service = login()
from_email = os.getenv('FROM_EMAIL')
message_content = """
Hi {to_name},
Put whatever here. You will be {status} of murdering the victim.
PS: this was sent by a bot, no one will know who is guilty except the guilty party themselves!"""
for d in sending_results:
m = create_message(from_email, d['email'],
'Virtual Murder Mystery Party Role',
message_content.format(to_name=d['name'], status=d['status']))
sent_message = send_message(service, 'me', m)
with open('record_{}.json'.format(datetime.datetime.now()), 'w') as out_f:
json.dump(sending_results, out_f)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment