added some more steps for the blog post

This commit is contained in:
2017-10-25 17:05:11 -04:00
parent 25b373a818
commit 4262b13969
10 changed files with 140 additions and 269 deletions

59
basic_sitter_bot.py Normal file
View File

@@ -0,0 +1,59 @@
import os
from typing import Tuple
from flask import Flask, request
from twilio.rest import Client as TwilioClient
from twilio.twiml.messaging_response import MessagingResponse
twilio_client = TwilioClient(os.getenv('TWILIO_SID'), os.getenv('TWILIO_TOKEN'))
app = Flask(__name__)
app.config.from_object(__name__)
sitters = {}
@app.route('/bot', methods=['POST'])
def bot():
from_ = request.values.get('From')
body = request.values.get('Body').lower()
response = 'I wasn\'t sure what to do with your input. '
if has_phone_num(body):
try:
sitter_name, sitter_num = add_sitter(body)
except (AssertionError, ValueError):
response = 'Sorry, did you mean to add a sitter? Please try again.'
else:
response = f'Okay, I added {sitter_name.title()} to sitters, with phone # {sitter_num}. '
print(sitters)
resp = MessagingResponse()
resp.message(response)
return str(resp)
def has_phone_num(string):
return len([char for char in string if char.isnumeric()]) == 10
def add_sitter(body: str) -> Tuple[str, str]:
name, *num_parts = body.split(' ')
num_only = ''.join(char
for num in num_parts
for char in num if char.isnumeric())
lowercase_name = name.lower()
sitter = sitters.get(lowercase_name)
assert len(num_only) == 10
sitters[lowercase_name] = f'+1{num_only}'
return name, sitters[lowercase_name]
if __name__ == '__main__':
app.run(debug=True, port=8000)