Skip to content

Commit

Permalink
feat: add twilio message
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfromyeg committed Jan 7, 2024
1 parent 87f8dd9 commit bf023d5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
15 changes: 10 additions & 5 deletions bereal/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from .bereal import memories
from .images import create_images, cleanup_images
from .videos import build_slideshow
from .utils import Mode, REDIS_HOST, REDIS_PORT, year2dates
from .utils import Mode, REDIS_HOST, REDIS_PORT, TRUE_HOST, year2dates
from .send import sms
from .logger import logger


Expand All @@ -23,9 +24,11 @@ def make_celery(app_name=__name__, broker=f"redis://{REDIS_HOST}:{REDIS_PORT}/0"


@bcelery.task(time_limit=1200)
def make_video(token: str, phone: str, year: str, song_path: str, mode: Mode) -> str:
def make_video(token: str, bereal_token: str, phone: str, year: str, song_path: str, mode: Mode) -> str:
"""
Creating a video takes about ~15 min. This is a work-in-progress!
TODO(michaelfromyeg): handle errors more gracefully; better logging.
"""
logger.info("Starting make_video task; first, downloading images...")

Expand All @@ -35,10 +38,9 @@ def make_video(token: str, phone: str, year: str, song_path: str, mode: Mode) ->
if not result:
raise Exception("Could not generate memories; try again later")

short_token = token[:10]
video_file = f"{short_token}-{phone}-{year}.mp4"
short_bereal_token = bereal_token[:10]
video_file = f"{short_bereal_token}-{phone}-{year}.mp4"

# TODO(michaelfromyeg): implement better error handling, everywhere
logger.info("Creating images for %s...", video_file)
try:
image_folder = create_images(phone, year)
Expand All @@ -55,6 +57,9 @@ def make_video(token: str, phone: str, year: str, song_path: str, mode: Mode) ->
gc.collect()
raise e

video_url = f"{TRUE_HOST}/video/{video_file}?phone={phone}&berealToken={bereal_token}"
sms(f"+{phone}", video_url)

logger.info("Cleaning up images")
try:
cleanup_images(phone, year)
Expand Down
25 changes: 25 additions & 0 deletions bereal/send.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""
Send messages to the user.
For now, only phone. Eventually, consider e-mail.
"""
from .utils import TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN, TWILIO_PHONE_NUMBER
from .logger import logger

from twilio.rest import Client


def sms(phone: str, link: str) -> None:
"""
Send a link to the user's phone number.
"""
try:
client = Client(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)

message_body = f"Here is the link to your BeReal Wrapped!\n{link}"
message = client.messages.create(body=message_body, from_=TWILIO_PHONE_NUMBER, to=phone)

logger.info("Sent message to %s: %s", phone, message.sid)
except Exception as e:
logger.error("Failed to send SMS: %s", e)
pass
2 changes: 1 addition & 1 deletion bereal/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def create_video() -> tuple[Response, int]:
logger.debug("Queueing video task...")

# TODO(michaelfromyeg): replace token with bereal_token
task = make_video.delay(token, phone, year, song_path, mode)
task = make_video.delay(token, bereal_token, phone, year, song_path, mode)

return jsonify({"taskId": task.id}), 202

Expand Down
9 changes: 9 additions & 0 deletions bereal/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
if SECRET_KEY == "SECRET_KEY":
raise ValueError("SECRET_KEY environment variable not set or non-unique")

TWILIO_PHONE_NUMBER = os.getenv("TWILIO_PHONE_NUMBER")
TWILIO_AUTH_TOKEN = os.getenv("TWILIO_AUTH_TOKEN")
TWILIO_ACCOUNT_SID = os.getenv("TWILIO_ACCOUNT_SID")

if TWILIO_PHONE_NUMBER is None or TWILIO_AUTH_TOKEN is None or TWILIO_ACCOUNT_SID is None:
raise ValueError("TWILIO environment variables not set")

# Global constants
BASE_URL = "https://berealapi.fly.dev"

Expand Down Expand Up @@ -85,6 +92,8 @@ def str2mode(s: str | None) -> Mode:
PORT: str | None = os.getenv("PORT") or config.get("bereal", "port", fallback="5000")
PORT = int(PORT) if PORT is not None else None

TRUE_HOST = f"http://{HOST}:{PORT}" if FLASK_ENV == "development" else "https://api.bereal.michaeldemar.co"

REDIS_HOST: str | None = os.getenv("REDIS_HOST") or config.get("bereal", "redis_host", fallback="redis")
REDIS_PORT: str | None = os.getenv("REDIS_PORT") or config.get("bereal", "redis_port", fallback="6379")
REDIS_PORT = int(REDIS_PORT) if REDIS_PORT is not None else None
Expand Down

0 comments on commit bf023d5

Please sign in to comment.