From 9027b7da3332604dd1c7d4934f764ce1ccfecb54 Mon Sep 17 00:00:00 2001 From: Parker M Date: Tue, 19 Sep 2023 20:19:55 -0500 Subject: [PATCH] Add Twilio Support --- .dockerignore | 3 ++- app/create_message.py | 9 +++++++++ app/initialize_variables.py | 10 +++++++++- app/messagearr.py | 22 ++++++++++++---------- docker-compose.yaml | 13 +++++++++++++ requirements.txt | 3 ++- 6 files changed, 47 insertions(+), 13 deletions(-) create mode 100644 docker-compose.yaml diff --git a/.dockerignore b/.dockerignore index c34d28b..752b920 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,2 +1,3 @@ __pycache__ -.DS_Store \ No newline at end of file +.DS_Store +docker-compose.yaml \ No newline at end of file diff --git a/app/create_message.py b/app/create_message.py index 56989e9..f51c867 100644 --- a/app/create_message.py +++ b/app/create_message.py @@ -1,4 +1,5 @@ import telnyx +from twilio.rest import Client from initialize_variables import * @@ -10,3 +11,11 @@ def create_message(number, message): to=number, text=message ) + + if sms_service == 'twilio': + client = Client(twilio_account_sid, twilio_auth_token) + client.messages.create( + body=message, + from_=api_number, + to=number + ) diff --git a/app/initialize_variables.py b/app/initialize_variables.py index 33e2642..5450036 100644 --- a/app/initialize_variables.py +++ b/app/initialize_variables.py @@ -2,7 +2,7 @@ import os import yaml import requests -supported_sms_services = ['telnyx'] +supported_sms_services = ['telnyx', 'twilio'] sms_service = str(os.environ['SMS_SERVICE']).lower() radarr_host_url = str(os.environ['RADARR_HOST_URL']) @@ -33,6 +33,10 @@ try: if sms_service == 'telnyx': telnyx_api_key = str(file['telnyx_api_key']) + if sms_service == 'twilio': + twilio_account_sid = str(file['twilio_account_sid']) + twilio_auth_token = str(file['twilio_auth_token']) + value_not_set = False except: print('One or more values are not set or not set correctly within the config.yaml file. Please edit the file or refer to the docs for more information.') @@ -64,6 +68,10 @@ if value_not_set: if sms_service == 'telnyx': f.write("telnyx_api_key:\n") + if sms_service == 'twilio': + f.write("twilio_account_sid:\n") + f.write("twilio_auth_token:\n") + f.write("\n\n# INFORMATION: There should be NO trailing spaced after you enter a value,\n# this will cause errors.\n# There should be one space after the colon though (e.g. quality_profile_id: 1)\n# Check docs for information on each value.") exit() diff --git a/app/messagearr.py b/app/messagearr.py index 107c12d..3b7d7be 100644 --- a/app/messagearr.py +++ b/app/messagearr.py @@ -47,29 +47,31 @@ and then run the command if it is valid. @app.route('/incoming', methods=['POST']) def incoming(): # Get the data and define the from_number (number that sent the message) - data = flask.request.get_json() - if sms_service == 'telnyx': - from_number = data['data']['payload']['from']['phone_number'] + from_number = flask.request.get_json()['data']['payload']['from']['phone_number'] + message = str(flask.request.get_json()['data']['payload']['text']) + + if sms_service == 'twilio': + from_number = flask.request.form['From'] + message = str(flask.request.form['Body']) # Make sure the number is a valid_sender, this stops random people from # adding movies to the library if from_number not in valid_senders: return 'OK' # If the message starts with /request, that means the user is trying to add a movie - unparsed = str(data['data']['payload']['text']) - if unparsed.startswith('/request'): + if message.startswith('/request'): # If the user has already run the /request command, delete the entry # from the temp_movie_ids dict so that they can run the command again if from_number in temp_movie_ids.keys(): del temp_movie_ids[from_number] # If the user did not include a movie title, alert them to do so # Just check to make sure that the length of the message is greater than 9 - if len(unparsed) <= 9: + if len(message) <= 9: create_message(from_number, "Please include the movie title after the /request command.\nEX: /request The Dark Knight") return 'OK' - incoming_message = str(data['data']['payload']['text']).split(' ', 1)[1] + incoming_message = message.split(' ', 1)[1] movie_request = incoming_message.replace(' ', '%20') # Send a request to the radarr API to get the movie info response = requests.get(f'{radarr_host_url}/api/v3/movie/lookup?term={movie_request}', headers=headers) @@ -100,7 +102,7 @@ def incoming(): # Elif the user responded with a variation of 1, 2, or 3 # This means they are replying to the previous prompt, so now we need to # add their movie choice to radarr for download - elif str(data['data']['payload']['text']).strip() in numbers_responses.keys(): + elif message.strip() in numbers_responses.keys(): # If there is no entry for the user in the temp_movie_ids dict, that means # they have not yet run the /request command, so alert them to do so. if from_number not in temp_movie_ids.keys(): @@ -116,7 +118,7 @@ def incoming(): # Otherwise, all checks have been completed, so alert the user of the # start of the process create_message(from_number, "Just a moment while I add your movie to the library...") - movie_number = numbers_responses[str(data['data']['payload']['text']).strip()] + movie_number = numbers_responses[message.strip()] try: tmdb_id = temp_movie_ids[from_number]['ids'][movie_number - 1] except IndexError: @@ -156,7 +158,7 @@ def incoming(): del temp_movie_ids[from_number] return 'OK' - elif str(data['data']['payload']['text']).strip() == '/status': + elif message.strip() == '/status': # This returns a list of ALL movies being downloaded, but not all of them were # requested by the user, so we need to filter out the ones that were not requested response = requests.get(f'{radarr_host_url}/api/v3/queue/', headers=headers) diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..8b75675 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,13 @@ +version: '3.3' +services: + messagearr: + ports: + - '4545:4545' + environment: + - TZ=America/Chicago # OPTIONAL: Default is UTC + - RADARR_HOST_URL=http://127.0.0.1:7878 # Change to your radarr host + - RADARR_API_KEY=apikeyhere # Found by navigating to Settings > General + - SMS_SERVICE= # Currently only supporting Telnyx and Twilio + volumes: + - /local/file/path:/data + image: packetparker/messagearr:latest \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 70c6fdf..0901419 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,4 +6,5 @@ APScheduler==3.10.4 hypercorn==0.14.4 asyncio==3.4.3 -telnyx==2.0.0 \ No newline at end of file +telnyx==2.0.0 +twilio==8.8.0 \ No newline at end of file