Python-powered Chat Apps with Twilio and SendGrid Transcripts
Chapter: Messaging and workflows with Twilio Studio
Lecture: Integrating external APIs

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


Talk Python's Mastodon Michael Kennedy's Mastodon