From the course: Microsoft Teams Bot Development

Write a GitHub client

From the course: Microsoft Teams Bot Development

Write a GitHub client

- [Instructor] Now if you have been following this course so far, you would see that the bot framework, or a bot in general, is nothing but a web service. As you can see here, we have a bot written up that says Hello World to whatever you say. Not very useful is it? So next I'm going to take this bot and make it a little bit smarter. If you're catching me in this module directly, what I'd like you to do, is set up this code on your machine first. Here's how you do that. Go into Exercise Files, and in chapter two, folder three, take the end project here, unzip it, and go ahead and run an npm install. I have already done that so let's dive straight into code. After an npm install you should see a node_modules folder in your project. Great, now let's go head and add some smarts into this bot. So one of the things that I found out very interesting is that GitHub actually has a REST API. So it's actually available on https://api.github.com and here I can execute some simple calls. For instance, I can say, slash search, slash users, so let's say I'm searching for users, and query is equal to and I can pass it any wildcard strings so I'll search for my name and it tells me that there are four people matching whatever search string I specified. So can I use the GitHub API and create a bot out of that? Let's try. So as you know, that the bot is nothing but just a web service. So the first thing I need to do is that I will write a simple JavaScript file, or what they call in the node a module, which will act as a client for all my GitHub requests. So github-client.js, and here I'll say var querystring is equal to require('querystring') these are built into node. var https is equal to require('https'), wonderful. And now I'll define my module, module exports is equal to and let's say that we want to expose a method that allows us to do a search. And we want to have another method that allows me to load the profile of a particular GitHub user. So I'm going to say loadProfile for a particular user. We'll fill this out in a moment. I'm going to have a generic function that executes a simple get call to GitHub. I'll call that function loadData, and that function calls api.github.com at whatever path we specify here. And then when we get data, we chunk the data together, as you can see on line 25, and when the response ends, as seen on line 26, we execute a callback and send the data back to the caller. There are many other ways to execute a REST call, and all of them should work, however, you can find this function along with other source code in the exercise files. So I'll call this function loadData, and that function calls api.github.com at whatever path that we specify here and when we get a response, we call the issue callback so this callback as you can guess is what we're going to chain all the way back. So now let's fill out executeSearch. So I'm going to say this.loadData and this calls as you saw was slash search slash users and query would be querystring.escape(query) and when that succeeds, let's go ahead and call callback, let's not worry about failures. And this.loadData, so another command that I have with which that I can load the profile of a user, and that is slash users slash querystring.escape(username) and yes, as usual, if that succeeds, we will call callback. So this finishes my GitHub client, now we can start using it.

Contents