Python-powered chat apps with Twilio and SendGrid Transcripts
Chapter: Messaging and workflows with Twilio Studio
Lecture: Integrating external APIs
0:00 Well, we've come a long ways.
0:01 We've got our workflow triggering, doing some validation,
0:05 interacting with the user, if they get things right or if they get things wrong.
0:10 What we've asked them is, what size of cake would they like?
0:13 But there's a lot of other information that we need to ask them.
0:15 What flavor, what frosting do you want?
0:18 Toppings and so on. Then,
0:19 once we figured out what they want,
0:21 we got to decide on the price.
0:23 You know, maybe bacon topping is more expensive than vanilla or Sprinkles or something like
0:28 that. Instead of trying to bake all that stuff hard coded in here,
0:31 we're going to use an API endpoint,
0:34 and we already have one in place for years over
0:39 cloudcitycakecompany.azurewebsites.net /api/flavors. We have a HCP JSON API.
0:45 If we just look at the raw data,
0:47 this is what we got. We can look at it a slightly nicer like this
0:51 and notice we're getting three arrays back within this object, flavor, topping and frosting.
0:57 And for the flavor, we have red velvet and chocolate and vanilla.
1:01 What we want to do is call this API within our workflow at twilio
1:06 have it saved those three different pieces the three arrays,
1:10 these three variables to three variables, we're going to call flavor topping and frosting using liquid
1:15 We're going to access those again later throughout our flow.
1:19 That way, if we take an item of our menu,
1:21 it'll disappear from that interaction. If we add some more items or running a special
1:25 they'll appear automatically. So what we're gonna do is want to copy that
1:29 URL, right up there about,
1:32 cloudcitycakecompany.azurewebsites.net/api/flavours and we're going to get rid
1:38 of this. Hey, great,
1:40 That was a good choice. And instead,
1:43 if they say the right size,
1:44 we're going to go and get some data,
1:46 Ask them further questions with that data,
1:48 we got so down here at the bottom,
1:51 we're going to go and make an http request.
1:53 Now things are getting a little crowded,
1:55 as you can see here. So let's click this button to make it more full
1:58 screen and we can zoom in a little.
2:01 We're not going to be looping back as much so,
2:03 zooming in is good And over here try to line these things up so they
2:09 feel good. And if the condition matches small,
2:13 medium or large, we're now going to I'll call this get menu over here.
2:17 We're gonna go get the menu and we need a request.
2:20 URL, It's going to be that, we hit save.
2:26 Now we're going to get this data back,
2:27 and it's going to live in it's somewhat complicated form and this object as one whole
2:33 thing over here. But we want to parse it apart and do a little bit
2:37 of work on that data. So what we're going to do after this is going
2:41 to go back and say, Set a variable And I'll just call this menu or
2:50 items or something along those lines,
2:52 I'll go with menu and we need to add a variable.
2:55 And this is where we're going to use liquid template.
2:58 So we're gonna give it a name.
2:59 And if we flip over here again over here working,
3:01 copy it. So we're gonna have flavor just like this.
3:05 And the way that we get it is with liquid.
3:07 So we come over and say
3:10 widgets.GetMenu.parsed. We get a JSON response.
3:15 It's parsed into JSON. That's that whole document.
3:19 And so what do we want?
3:20 We want .flavor. So when you add another one, this time we want topping
3:30 and the value will be GetMenu.parsed like that. Really nice to get some
3:37 auto complete there, and it should just be topping, my flavor topping.
3:43 We're gonna need one more. We're frosting.
3:47 I'm just copying to make sure I get the spelling exactly right,
3:49 so I'll be frosting GetMenu parsed,
3:55 go back, got frosting. Great.
4:00 Now let's just do a quick little echo here to see how things are coming along
4:03 You had temporal test, and we're just gonna echo back what we set in
4:10 these variables. So we'll say menu,
4:13 we just say the variable names we go back here, and maybe it's also worthwhile to
4:23 echo this to see what comes out.
4:30 And finally, we're going to need to connect that, published those changes back to WhatsApp
4:37 to see if we got it right.
4:41 More cake please!, to get some feeling like a lot of cake close to
4:45 go get a large, Oh, it looks like we made a mistake.
4:49 Let's go fix that, over here where we're pulling out the values,
4:53 it has to be widget.menu like this.
4:59 So somehow I, I lost my way when I was trying to get the auto complete
5:04 together. So it's just widget.menu topping.
5:10 All right, now we're ready to try again.
5:14 Republish, still more cake. We're gonna be full of cake by the time this courses over
5:20 medium got to tone this down a little perfect.
5:25 So there's what we got back from the API our Flavors are red velvet
5:28 chocolate, vanilla, carrot,
5:30 rainbow, velvet, chocolate, vanilla,
5:34 carrot Rainbow is an interesting word.
5:35 So is cheese chocolate, vanilla,
5:37 maple. So something's going on.
5:39 Obviously we're just getting that data back directly and just dropping it in place.
5:43 We're going to need to do a little bit more work when we're caption or variable
5:46 or at least when we're displaying it.
5:48 We'll do it when we capture it just once,
5:50 but you do our display it with liquid templates to make it a little bit more
5:54 human readable, not just computer data.
5:57 So the final thing where we're going over here to set our variables let's go and
6:01 edit these, yourselves a little more room.
6:04 Remember what I showed you? That we can work with grabbing variables through liquid.
6:09 But we can also further process them with these filters,
6:13 and the one that we want to use is join.
6:15 The way you apply a filter to some piece of data in this case is going
6:23 we say. Pipe (|) the filter name Colon(:).
6:26 And then the arguments and the arguments we want is to join each element and put
6:30 a comma(,) and a space between it.
6:33 So take the string, all the elements and stringify it by putting together
6:36 with the string in between all of them.
6:38 That way we don't get chocolate
6:39 Vanilla carrotcake is all one giant piece of text.
6:43 So we're going to do this to all three of our variables.
6:48 Save that one. We're gonna do it to the Topping,
6:52 and we're gonna do it to the frosting.
6:56 And then we can put that back a little bit.
6:58 Publish it, about a little more cake because after the large and the medium,
7:03 I'm quite full. So I'll go for a small this time.
7:06 Perfect. This is what we're hoping for.
7:09 The flavors that we have available on our menu are red velvet,
7:12 chocolate, vanilla, carrot, rainbow and we have cream cheese,
7:16 chocolate vanilla and maple frosting.
7:18 This is not exactly how we're gonna interact with our user.
7:21 We're going to ask them. What flavor do you want one of these?
7:24 What frosting do you want? One of those in a nice exchange.
7:27 And we could even do validation that it's valid what they've said each time along the
7:31 way. But just for testing.
7:33 It's nice every now and then to stop, an echo back what you think you're working
7:37 with. So it's really easy for us to see.
7:39 Yeah, we did get that data from the API.
7:41 And our filter our joint filter did split it into a string like we
7:45 would expect. Now check this out.
7:47 We're getting this data from the
7:48 API, and this means a whole world of possibilities.
7:51 We can make changes through some management app or some backend,
7:55 and all of our twilio interactions are just picking up those changes instantly and continuing to work in the best way they can.