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