Automation: Building a Reservation/Waiting List Text-Bot For My Weekly Poker Games

  • Twilio: Messaging service that allows you to send texts via python
  • Flask: web server framework for receiving text messages and handling responses.
  • Iphone: I used the new shortcuts feature in my iphone to trigger commands on the Twilio server
pip install twilio
easy_install twilio
#Imports
from twilio.rest import Client
import csv
from flask import Flask, request, redirect
from twilio import twiml
from twilio.twiml.messaging_response import Message, MessagingResponse
# Your Account SID from twilio.com/console
account_sid = "YourSID"
# Your Auth Token from twilio.com/console
auth_token = "Your Auth Token"

client = Client(account_sid, auth_token)
def sendMsg(msg,number):
message = client.messages.create(
to=number,
from_="Twilio Number You Registered",
body=msg)

print(message.sid)
sendMsg("Test Message","Your Number")
app = Flask('PokerMessenger')

@app.route("/", methods=['GET', 'POST'])
def sms_reply():
# Get the message the user sent our Twilio number
body = request.values.get('Body', None)
number = request.values.get('From',None)
print("Message Recieved From")
print(number)
"""Respond to incoming calls with a simple text message."""
# Start our TwiML response
resp = MessagingResponse()


# Add a message
resp.message(handleMsg(body,number))

return str(resp)


if __name__ == "__main__":
app.run(debug=True)
##Handles Responses and adjusts the invite List accordingly
def handleMsg(msg,number):
if "/addGuest" in msg:
addContact(msg)
elif msg == "/Send:92115":
inviteAllContacts()
elif msg == "/requestWList":
sendWaitingList()
elif msg == "/requestCList":
if number == Admin:
sendGuestList()
elif msg == "Confirm" or msg == "confirm":
if isTableFull(maxTable):
waitingList.append(number)
outputGuests()
return "You have been added to the waiting list. There are " + str(len(waitingList) - 1) + " people ahead of you"
else:
handleConfirmation(number)
outputGuests()
return "Your seat has been reserved. Text 'No' to undo."
elif msg == "No" or msg == "no":
handleCancellation(number)
outputGuests()
return "Thank you for responding. Hope to see you next time!"
else:
return "Error: Invalid Response"
###Prints the confirmed and waiting List
def outputGuests():
print("Confirmed List:")
print(confirmedList)
print("Waiting List:")
print(waitingList)

##Sends an invite message to all contacts
def inviteAllContacts():
for k in NumberToName.keys():
msg = "You have been invited to Pran's Poker Game this Thursday 7:00PM at 43 Westland Avenue." \
"Reply: 'Confirm' to reserve your seat, or Reply: 'No' if you won't make it."
sendMsg(msg,k)

##

##Sends a message to a number provided it is included on the contact list
def sendMsg(msg,number):
if NumberToName.has_key(number):
message = client.messages.create(
to=number,
from_="+19543629753",
body=msg)

print(message.sid)
else:
print("Number not included in contact List")
##

##Sends the admin the attendee list
def sendGuestList():
sendMsg(confirmedList,Admin)
##

##Sends admin the waiting List
def sendWaitingList():
sendMsg(confirmedList,Admin)
##

##checks if the table is full
def isTableFull(max):
return len(confirmedList) >= max
####

##Removes atendee from list and adds first from waiting list to confirmed
def handleCancellation(number):
print("Cancelling Number ")
print(number)
outputGuests()
if containsNumber(number,confirmedList):
removegrepl(number,confirmedList)
if len(waitingList) > 0:
sendMsg("You have been taken off the Waiting List! Reply: 'Confirm' to reserve your seat!",waitingList.pop(0))

##If the attendee is not yet in the confirmed list, add him
def handleConfirmation(number):
if not containsNumber(number,confirmedList):
confirmedList.append(number)

##Checks in a thorough manner if the list contains the suppliedNumber
def containsNumber(number,list):
t = False
for i in list:
if number in i:
return not t;

###Removes a string that contains the supplied string froma list
def removegrepl(number, list):
for i in list:
if number in i:
list.remove(i)
maxTable = 1
waitingList = []
confirmedList = ["MyPhoneNumber"]
NumberToName = {'MyPhoneNumber' : "Pranav Ahluwalia"}
Admin = "MyPhoneNumber"

##Loads all names and numbers from guests.csv into dictionary
def loadContacts():
sheet = csv.reader('guests.csv')
count = 0
for row in sheet:
if count > 1:
NumberToName[row[2]] = row[0] + " " + row[1]
##
##Sends an invite message to all contacts
def inviteAllContacts():
for k in NumberToName.keys():
msg = "You have been invited to Pran's Poker Game this Thursday 7:00PM at ______" \
"Reply: 'Confirm' to reserve your seat, or Reply: 'No' if you won't make it."
sendMsg(msg,k)

##
  • Handling a confirmation
##Removes atendee from list and adds first from waiting list to ##confirmed
def handleCancellation(number):
print("Cancelling Number ")
print(number)
outputGuests()
if containsNumber(number,confirmedList):
removegrepl(number,confirmedList)
if len(waitingList) > 0:
sendMsg("You have been taken off the Waiting List! Reply: 'Confirm' to reserve your seat!",waitingList.pop(0))

##If the attendee is not yet in the confirmed list, add him
def handleConfirmation(number):
if not containsNumber(number,confirmedList):
confirmedList.append(number)

##Checks in a thorough manner if the list contains the ##suppliedNumber
def containsNumber(number,list):
t = False
for i in list:
if number in i:
return not t;

###Removes a string that contains the supplied string from a list
def removegrepl(number, list):
for i in list:
if number in i:
list.remove(i)
Shortcut that sends command to add a contact to the Guest List

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store