Ejemplo n.º 1
0
def user_await_vendor_conf(command, order_id, from_number):
	command = clean_sms(command.lower())
	resp = twilio.twiml.Response()
	msg = None

	query = "SELECT vendor_conf FROM orders WHERE id = %s"
	vals = (order_id,)
	result = db_lib.pull_db(query, vals)
	if result is None or len(result) == 0: # if we couldn't find the entry, restart
		return initial_sms(command, from_number)

	last_cookie = 'user awaiting vendor confirmation'
	if int(result[0][0]) == 0:
		if command == "c":
			query = "SELECT o.description, o.price, v.phone FROM orders as o, vendors as v WHERE o.id = %s"
			vals = (order_id,)
			order = db_lib.pull_db(query, vals)[0]
			body = "User cancelled order of " + order[0] + " for " + '${:,.2f}'.format(float(order[1])) + "."
			client.messages.create(to="+14848001773", from_=TWILIO_PHONE, body=body) # sends message to vendor

			query = "DELETE FROM orders WHERE id = %s"
			vals = (order_id,)
			db_lib.push_db(query, vals)
			msg = "Order cancelled"
			last_cookie = 'raw'
		else:
			msg = "Invalid command. Waiting for vendor. Send 'c' to cancel order."
	else:
		return initial_sms(command, from_number)

	resp.message(msg)
	resp = make_response(str(resp))
	resp.set_cookie('last', last_cookie)
	return resp
Ejemplo n.º 2
0
def vendor_conf(command, order_id):
	command = clean_sms(command.lower())
	resp = twilio.twiml.Response()
	msg = None

	if command == "y":
		query = "UPDATE orders SET vendor_conf = 1 WHERE id = %s"
		vals = (order_id,)
		db_lib.push_db(query, vals)
		msg = "Order confirmed, venmo charge pushed!"
		query = "SELECT phone, price FROM orders WHERE id = %s"
		vals = (order_id,)
		result = db_lib.pull_db(query, vals)
		venmo_data = venmo.charge_or_pay(result[0][0], -1 * float(result[0][1]), str(order_id))
		db_lib.update_order_venmo_charge(order_id);
		# do something with venmo_data? TODO
		body = "Order confirmed by vendor! Please accept the venmo charge to continue."
		client.messages.create(to=result[0][0], from_=TWILIO_PHONE, body=body) # sends message to user
	elif command == "n":

		query = "SELECT phone FROM orders WHERE id = %s"
		vals = (order_id,)
		result = db_lib.pull_db(query, vals)
		body = "Order rejected by vendor..."
		client.messages.create(to=result[0][0], from_=TWILIO_PHONE, body=body) # sends message to user

		query = "DELETE FROM orders WHERE id = %s"
		vals = (order_id,)
		db_lib.push_db(query, vals)
		msg = "Order rejected"
	else:
		msg = "Unrecognized command. Send y or n."

	resp.message(msg)
	resp = make_response(str(resp))
	return resp
Ejemplo n.º 3
0
def await_user_conf(command, order_id):
	command = clean_sms(command.lower())
	resp = twilio.twiml.Response()
	msg = None

	last_cookie = "awaiting user confirmation"
	if command == "y":
		query = "UPDATE orders SET user_conf = 1 WHERE id = %s"
		vals = (order_id,)
		db_lib.push_db(query, vals)

		# vendor handling...
		query = "SELECT o.description, o.price, v.phone FROM orders as o, vendors as v WHERE o.id = %s"
		vals = (order_id,)
		order = db_lib.pull_db(query, vals)[0]
		if order is None or len(order) == 0:
			query = "DELETE FROM orders WHERE id = %s"
			vals = (order_id,)
			db_lib.push_db(query, vals)
			msg = "Failed to contact vendor. Transaction cancelled"
			last_cookie = 'raw'
		else:
			body = "Received order of " + order[0] + " for " + '${:,.2f}'.format(float(order[1])) + ". Accept? y/n"
			client.messages.create(to="+14848001773", from_=TWILIO_PHONE, body=body) # sends message to vendor
			msg = "Order forwarded to vendor for confirmation!"
			last_cookie = 'user awaiting vendor confirmation'

	elif command == "n":
		query = "DELETE FROM orders WHERE id = %s"
		vals = (order_id,)
		db_lib.push_db(query, vals)
		msg = "Order cancelled"
		last_cookie = "raw"
	else:
		msg = "Unrecognized command. Send y or n."

	resp.message(msg)
	resp = make_response(str(resp))
	resp.set_cookie('last', last_cookie)
	return resp