# Creating a user consent application using the Python SDK ## Creating the application All of the code for this user consent app is contained within the `app.py` file. The completed code is displayed on the right panel. Copy and paste that code into your `app.py` file. The composite sections of code are dicussed below. ### Setting up the application The `app.py` file begins with the following code: ```python import sinch from flask import Flask, request import signal import sys app = Flask(__name__) sinch_client = sinch.SinchClient( key_id="your_key_id", key_secret="your_key_secret", project_id="your_project_id" ) sinch_client.configuration.sms_region = "your_region" group = sinch_client.sms.groups.create("Sinch Pirates") def signal_handler(signal, frame): delete_group_response = sinch_client.sms.groups.delete( group_id=group.id ) print(delete_group_response) sys.exit(0) signal.signal(signal.SIGINT, signal_handler) ``` br This code sets up everything your application needs to function. - It creates a Flask instance, and [initializes a Sinch SDK client](#initializing-the-client). This allows your application to use the functionality of the Python SDK to create groups and send messages. - It sets the `sms_region` to your region and creates a group. - Finally, the `signal_handler` function ensures that your group is deleted when you terminate your Flask app. Note: While we need to create and delete a group for this demonstrative tutorial, it's worth noting that in production, recipients would probably be subscribing to, or unsubscribing from, a prexisting group. #### Initializing the client The snippet above includes the code required to initialize the Sinch SDK client. To start using the SDK, you need to initialize the main client class with your credentials from your Sinch [dashboard](https://dashboard.sinch.com). ```python from sinch import SinchClient sinch_client = SinchClient(key_id="key_id", key_secret="key_secret", project_id="YOUR_project_id") ``` Note For testing purposes on your local environment it's fine to use hardcoded values, but before deploying to production we strongly recommend using environment variables to store the credentials, as in the following example: ```python import os from sinch import SinchClient sinch_client = SinchClient( key_id=os.getenv("KEY_ID"), key_secret=os.getenv("KEY_SECRET"), project_id=os.getenv("PROJECT_ID") ) ``` ### Receiving incoming messages Lets review the code that gives your application the ability to receive incoming messages: ```python @app.route('/', methods=['POST']) def result(): inbound = request.get_json() numbers = sinch_client.sms.groups.get_group_phone_numbers( group_id=group.id ).phone_numbers if all(key in inbound for key in ["body", "to", "from"]): from_number = inbound["from"] to_number = inbound["to"] inbound_message = inbound["body"] auto_reply = "" ``` br This is similar to the code in the [Receive an SMS Message](/docs/sms/getting-started/python/receive-sms-sdk/) guide. Its basic job is to listen for an inbound message and respond with an action. ### Adding and removing users Whereas the *Receive Message* application simply returned a reply containing the inbound message, this application is going to add/remove users from the group you created. Review the following code: ```python if from_number not in numbers and inbound_message == "SUBSCRIBE": sinch_client.sms.groups.update(add=[from_number], group_id=group.id) auto_reply = ("Congratulations! You are now subscribed to " +group.name+". Text STOP to leave this group.") elif from_number in numbers and inbound_message == "STOP": sinch_client.sms.groups.update(remove=[from_number], group_id=group.id) auto_reply = ("We're sorry to see you go. You can always rejoin " +group.name+' by texting SUBSCRIBE to ' + str(to_number)) else: auto_reply = ("Thanks for your interest." " If you want to subscribe to this group," " text SUBSCRIBE to {to_number}") ``` br This lets your application respond intelligently to join/leave requests. The logic is designed to cover the following basic contingencies: - If a user is *not* already a group member and wants to join, the application looks for the **SUBSCRIBE** keyword. It then uses the SDK update method to add them to the group and assigns a confirmation message to the variable `auto_reply`. - When a user *is* already a group member and wants to leave, the application looks for the **STOP** keyword. It then uses the SDK update method to remove them from the group and assigns a confirmation message to the variable `auto_reply`. - If a user is not a group member but doesn't know the keyword to join, the application sends a message thanking them and telling them the right keyword. ### Sending confirmation messages The final code is for sending the confirmation messages assigned to `auto_reply`. ```python sinch_client.sms.batches.send( body=auto_reply, delivery_report="none", to=[from_number], from_=to_number ) ``` This sends the `auto_reply` confirmation using the SDK send method. Again, it's pretty similar to how the *Receive Message* application worked.