Python-powered Chat Apps with Twilio and SendGrid Transcripts
Chapter: Sending outbound text messages
Lecture: Sending the cake ready message with Twilio

Login or purchase this course to watch this video and the rest of the course contents.
0:00 As long as we live within the constraints of our sandbox, it's actually pretty easy to send out a message. So who we're going to send it to?
0:08 We'll say to_number=order.user.phone And it has a user and the user as a phone. Yeah, that's perfect. And we have from_number.
0:20 And right now that needs to be what we have in our twilio sandbox, which is "whatsapp: and then that sandbox number. And then we need the message.
0:31 Remember, it has to be of this form. The order status is such and such code, right? Let me drop this in. So we get it just right,
0:39 and I'll even put a link here up to where you can learn about these. So your cake orders status code is ready for pickup,
0:48 right? I think that will work just fine. Then how do we know what to do? Well, we're just going to go and send a message to create a message.
0:59 I want to go over here and say the 'to' is the 'to_number' 'from_' 'from_number' and the body is the message body.
1:09 It's probably better to think of this as a response And then we can check
1:14 if response has an error(if resp.error_message:) message or Error Code will raise an exception and not send a
1:21 WhatsApp message, and we'll just pass along that error message. Otherwise everything will have worked. So I think that will do it.
1:32 Unless we've made any mistakes along the way, I think we should give it a try. Let's go out and run it.
1:40 Now Over here in WhatsApp, I've already just a moment ago created another order. That's my order 20. And what I want to do is go over to
1:50 the admin section and tell Michael here that his cake and a cake does he have
1:55 something with bacon, cream cheese and so on, that his cake is cream cheese. Bacon cake. That sounds kind of weird, that that's ready for pickup.
2:04 So first of all, let's refresh it here. Okay, here's Michael, and this is the order we just placed. If we fulfill it, fingers crossed.
2:11 What's going to happen? Oh no. Our user has become detached. That's not good. I think we forgot a sub query Join thing going on here.
2:20 Let's just fix that real quick. You'll find order by id. We're creating a session.
2:25 We're getting the order and then we're closing because we're leaving this with block. We're closing the session and we can't do that joint.
2:31 So let's go over here and I'll just say as we're doing these things, we'll do an option. (subqueryload (Order.user)).
2:42 Alright, with our user not detached. Let's go and try to fulfill this order which will grab the user information and send
2:50 them that WhatsApp message. Here we go. Alright that worked. And check that out. Your cakes. Order Status code is ready for pickup.
2:59 Fantastic. So over here in WhatsApp, you can see that we have now received this message initiated by our website.
3:06 When we interacted with the admin section to reach out to this user and let them know a while ago you place this order for this cake and guess what?
3:14 Here it is. It's ready to go. We had a better message. Template. We could give them more information.
3:20 Like your small cream cheese. Bacon cake is ready for pickup. Make sure you get it within the next hour at such and such and so on
3:27 Right, We could do a better message, but within this template. Constraint. This is fantastic. How cool is that?
3:34 We've got the entire round trip of the app that we've been building working. They come to our website, fire up the WhatsApp link and say,
3:42 Hey, I'm looking for a cake through chat and I'd like to order something so great we go through that back and forth,
3:48 we use Twilio studio and then finally went twilio studio got all the information it called
3:53 an API on our flask application which put it into this admin section as unfulfilled as open. And then finally now we hit,
4:01 fulfill it sends that message over through WhatsApp, back to the client and lets them know that thing you ordered a while ago.
4:09 It's ready. Come get your delicious cake. Fantastic.


Talk Python's Mastodon Michael Kennedy's Mastodon