-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.py
85 lines (70 loc) · 2.64 KB
/
storage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import os
import json
import requests
import urllib.parse
# load env variables
from dotenv import load_dotenv
load_dotenv(override=True)
# Get Airtable parameters from environment variables
base_key = os.environ.get('AIRTABLE_BASE_KEY')
table_name = os.environ.get('AIRTABLE_TABLE_NAME')
access_token = os.environ.get('AIRTABLE_PERSONAL_ACCESS_TOKEN')
def store_message(from_number, to_number, message_body, message_type, twilio_message_sid):
# Define the URL to send the message details to
url = f"https://api.airtable.com/v0/{base_key}/{table_name}"
# Define the headers and data for the request
headers = {
"Authorization": f"Bearer {access_token}",
"Content-Type": "application/json"
}
data = {
"fields": {
"from_number": from_number,
"to_number": to_number,
"message_body": message_body,
"message_type": message_type,
"twilio_message_sid": twilio_message_sid
}
}
# Make the post request
response = requests.post(url, headers=headers, data=json.dumps(data))
return response.json()
def store_user(user_number, first_message):
# Define the URL to send the user details to
url = f"https://api.airtable.com/v0/{base_key}/Users"
# Define the headers and data for the request
headers = {
"Authorization": f"Bearer {access_token}",
"Content-Type": "application/json"
}
data = {
"fields": {
"user_number": user_number,
"first_message": first_message
}
}
# Make the post request
response = requests.post(url, headers=headers, data=json.dumps(data))
return response.json()
def remove_user(user_number):
# Define the filter formula to find the record_id for this user
filter_formula = f"(user_number='{user_number}')"
# URL-encode the filter formula
filter_formula = urllib.parse.quote(filter_formula)
# Define the URL to make the request to the database
url = f"https://api.airtable.com/v0/{base_key}/Users?filterByFormula={filter_formula}"
# Define the headers for the request
headers = {
"Authorization": f"Bearer {access_token}"
}
# Make the GET request for the data
response = requests.get(url, headers=headers)
record = response.json()
record_id = record['id']
# Define the URL to delete the record
url = f"https://api.airtable.com/v0/{base_key}/users/{record_id}"
# Define the headers and data for the request
headers = { "Authorization": f"Bearer {access_token}" }
# Make the delete request
response = requests.delete(url, headers=headers)
return response.status_code