 I'm glad to see everybody here this morning. This is Peter Preston. We're going to be talking about building a chat bot and going to go out of the business community. Thanks very much. Before I really get started, I sat through in a session yesterday where the instructor introduced the session and the title all themselves and then had to do it all over again for the video because I hadn't recorded yet, so I'll get the thumbs up from the guy. There it is. There's the thumbs up, so we can get started. My name is Nate Preston. I'm a developer evangelist with Cisco's dead-net organization. How many people have ever heard of Cisco before? Yeah, how many people actively use Cisco at something in an organization today? A phone? That's good enough. I agree with my idea. How many people use Cisco somewhere in an organization today? There's a few. And those that know and probably just don't know it, we're usually buried someplace in there. Think about Cisco when you think about developer experience, APIs and all of that. Anybody? Yeah, no, I know. Yeah, we're trying really hard. So that's actually what my job is. I work with dead-net, which is our developer ecosystem and program. If you're familiar with MSDN, Microsoft Developer Network, Amazon's obviously got their dead-net of Cisco. And Cisco's been around for many years, but we've really, only in the last handful, we've really adopted and understood the importance of the developer platform, because frankly, for a while, most people just wanted to buy our stuff, plug it in and make sure it did what it was supposed to do. But today, everything in technology is about being a platform, making people extend capabilities in technology, and nobody is happy with what you get out of the box anymore. And so, dead-net is how Cisco is out there providing all of the things that developers need to get started. And I have the wonderful job of being a part of the team that gets to do this type of stuff, developer outreach, evangelism, and people know what's out there. And so that's what we're going to spend some time today is talking through some of those. Let me get rid of this gum, which is a terrible thing to still have in my mouth because I've already started. So let's dive in. So the session today, build a chat pod with Chuck Norris, Python, and Docker. I put Chuck Norris in there purely for the seats. How many people came simply because Chuck Norris was in the title? Yeah, it's okay. Everybody there on purpose, right? I'm a professional presenter, but you'll learn a little bit about some of these other topics as we go through the day. All right, so let's try a little bit about me. So I call myself a technologist and a performer. So these are all shots of me in the last year doing what I love to do, which is talk about technology or just have a good time up on a stage someplace. So some of the core values I have, I actually stepped through a session yesterday. I was the first one that went through was like, how to help your manager be at their leader? I don't see him here. But he had a great job talking to me. What are your beliefs? Make sure people know what you believe. So these are mine. I think technology is amazing. Frankly, it changes everything in the world when it goes through. And secondly, technology should be fun. For a long time, I've been at Cisco, you can see my ears back here for a while, but I've been out talking to people on technology and I would just see grumpy faces everywhere. Nobody's going to be enjoying technology anymore. And so I personally believe it should be fun. If it's not, then something is wrong. And we should go after that. Your work should be enjoyed. I know not everybody can enjoy your job, but if you have an opportunity to enjoy what you're doing, gosh darn it, go for it. Try to enjoy what you're doing. And then learning should also be fun. I present and I do education by trade as part of my job. And I've seen too many people get up there and do the monotone, deliver a presentation, have an audience all looking at their laptops, doing nothing. If I was going to deliver content ever in my career, I want to be sure everybody is enjoying what they're after. So those are my core values. How I achieved where I'm at today, so why you should talk to me, is I actually graduated undergraduate in 2001 with a degree in psychology and theater, which promptly, yeah, which promptly turned into a couple of career jobs. I actually did professional theater for a little while. Musicals, rock concerts, that type of thing. And then that's a night job, if you didn't know. And so I started taking on computer college classes just to kill time during the day. I did coding, web design, development, like that. And I realized two things fairly quickly. One, hours are way better in technology. And two, the pay is also way better. And so I left and I started becoming an IT consultant in 2003. I moved on, kept with the schooling, got a master's in information systems with a focus on database development and engineering design. So I did a lot of web development, DBA stuff, stuff like that. And then I started, you'll notice I moved into infrastructure from development. I came out of school with a development background and then I purely realized very quickly that I am not a fan of Java. God, I hate Java development. And so I said, you know what? It's time to move on and go to infrastructure. I still like technology, I don't want to lose it all, but I'm just tired of Java. So I did infrastructure for a handful of years, which brought me to Cisco. That's where I came in. I came in as a network engineer, a system engineer, hoping people were their infrastructure pieces. And then cloud and data centers, development became really valuable to go out and talk with customers. In 2013, I discovered Python and I realized that development doesn't have to suck anymore. Java is not the only thing out there. You can write stuff and things that are entertaining. And then that brought me to 2016 in my dream job, which is what I do full time now, which is help people understand technology and have a good time doing it. I'm pretty excited so far. So nobody's left yet and you're like, what the hell is this? So what is our agenda for today? So we've got a good chunk of time, nearly two hours to go through this. So these are the bullet points we're going to hit. How many people have ever heard of Cisco Spark before? All right, so it's not everybody in the room, so I'm going to have to do a bit of an interest so you then know why. I knew Chuck Norris would bring people in. Cisco Spark might scare people away. So we'll talk real briefly about what Cisco Spark is and what it is and why you might want to take some of the skills away and use them in your real-life job after that. So this is the lab preparation in Pre-REx. Did anybody come today to a coding tutorial at Pi, Ohio and not bring a laptop? I've had it happen. I've had it happen. All right, so you all have laptops, and the first Pre-REx is handled. We're going to go through the anatomy of Sparkbot, which is actually the anatomy of any bot. I don't care whether you're writing it from Facebook or Slack or any of those other platforms. They all generally work the same way. So we'll go through the general architecture there. And then we'll dive into the actual piece. So we're going to look at the Cisco Sparkbot Python library. We'll see how it works, and then we'll use it to very rapidly stand up a functioning Sparkbot. Everybody in the room will get your own functional Sparkbot. And then we'll start to dissect it and add new capabilities as we go through. The one we're going to focus on is Chuck Norris via the API. My personal favorite API on the Internet is the Chuck Norris API. So we'll take a look at that and how we can implement it. Once our bot is working and we've gotten developed, they're running on our laptops. You can package that up as a docker image so that you can actually get portable if you wanted your bot to live on indefinitely. You can take your docker image, post it someplace on the Internet, make it available. And then at the end we're going to talk about how you add new bot commands. So we get through all of that in still time. I'm hoping that everybody will kind of go through and hand down and we'll do more coding and we'll see what extra things we can teach our bot to do as far as that goes. Everybody still excited? Anybody want to get up and leave so intro to Cisco Spark? So if you already know what Cisco Spark is, that's fine, go check Twitter to read about how things up there are talking about something. That's cool, but for those that don't, Cisco Spark, the industry's most advanced collaborations. I want the reverb on the echo to go through there. There really is just the next generation Cisco collaboration platform built in a modern fashion, it's web UI, it's all focused on the user and it offers these capabilities, messaging and calling. So you can hold virtual meetings, you can do messaging, which is a high part of what it uses and that's our focus for today is around bots, but it also does both voice and video calling. So it's kind of the full collaboration platform targeting mostly enterprise users that is Cisco's target audience. It's user centric and that's a little different than some of the other ones that are out there. It's not centered on a team, it's not centered on a particular event, it's all about the user. It's the platform that they can count and then they can join and participate in whatever they want to go for that. But what it was actually built to be more than anything else is this platform across any device. So whether you're on a mobile device from Apple or Android or some other manufacturer, you're in a room, you're out of phone on your desk, you've got a big whiteboard solution, it's a single platform, a single capability for Cisco to cover all these solutions. It's the API platform, it's the development platform which is really where it starts to shine. It's been built API first like any good software today should be so that you can integrate and you can add capabilities to it. So you can build it into your workflows, you can build it into all those capabilities that are there. There's plugins for box and office, we've got Zendesk, we've got Salesforce plugins, all of those types of capabilities already exist. But because it's an open platform that is free to join, you'll be able to get your account no, don't, don't, don't, don't. Oh, no. Alright, one down. I should have done the crowd selfie when I started it before you all came in. So, I get distracted really easily. So what's really nice about this is you can get an account for Cisco Spark for free, and you have full access to the developer API. You don't have to pay anything, you can jump right into it, which is one of the benefits when we get out there and go forward. So anybody can build an app or a bot in integration or whatever you're after. These are some of the ones that exist already today, and they break into kind of two areas. Integrations and bots. You want to build something into your application that is going to leverage either meetings, messaging, calling inside of your own area, or you want to build a bot that can interact and answer questions or just tell jokes. And then we've got the APIs and the SDK, so it makes it easy for you to use those technologies that are out there. So the biggest categories that people are building, integrations and bots are going to fall into these areas. Incident management, HR onboarding, general tasks, finance operations, content management, like the list could go on and why people can do these areas. Now we're going to take the simple step, right? You have to crawl before you walk, walk before you run, and so we're just going to build a very basic, interactive bot that will take some input and then reply with a funny clip is really kind of the point of what we're going to go after. But the skills we use today, or you learn in the class that you could use to integrate anything. As long as you've got an API and an idea, you can build a bot for that. We're also now covering kind of the high end of bot pieces today, artificial intelligence, natural language processing. Those are all topics worthy of their own. We're going to stick with kind of the basic framework for bot creation today, which is command structure. Look for a command, trigger all of that and do something. That's it. That's as close as I get to a marketing field today, but now everybody understands what it is, this tool that we're integrating with so you can go back and have a little bit of context that goes through. So lab prerequisites to go through on this. So this is PyOhio, so hopefully none of these are going to be too hard. So on your laptop, you need Python 3.6 preferred, 3.5 or 3.4 would probably work as well. Does anybody not have that installed yet today? All right, you've got, okay. If you've got Python 2, I've got some pieces you can go through on that, but I would say go to python.org, grab Python 3.6, that'll be the easiest. All the code samples will work exactly as expected. Docker for whatever your platform is. If you're a Mac user, docker for Mac, if you're a Windows user, docker for Windows, if you're a Linux user, just docker on those. So who needs to go and is frantically downloading docker now? We've got plenty of time, so don't over too much. I built it on. So hit docker.com, get docker, so you can go through on that. And then the way that bots work, whether it's Spark or anything else, is to go through the code, and that code is then responsive to requests from the Internet. Because the requests come from the Internet and your code is going to run on your laptop, we need a way to put your laptop on the Internet. And as great as OSU is, like all of our laptops are just publicly on the Internet and you join the Wi-Fi here, that's probably really a good thing. And so we use a tool called ngrok, which is an open source tool that builds a local secure tunnel from your laptop and you'll already have ngrok on your laptops. That's actually quite, that's way more than usual, not surprised at the audience here. If you don't have it yet, hit ngrok.com slash download. Grab the download for it, it's a really simple command line utility, you can run it in a temp directory, it doesn't need to be any fancy installation. Do you technically know you wouldn't, as long as when you start the Python application, you know the URL that your application has, you could just sub that URL then instead. Okay, good question. Oh yeah, the question was if our system is actually up on the Internet, we're connected to a remote development machine and it does exist on the Internet, do we need ngrok? And the answer is no, if you know how to, you're going to run your Python application, you know what the URL that's available at is available, and that's fine, you wouldn't need it. So we're going to throw those pieces that are there. All right, accounts. So you do need Cisco Spark accounts to do this, you're going to run them from your own account. So if you don't have one already, if you go to web.cisco-spark.com, you'll put your email address in, it will send you an email, it's like any other signup, it'll send you an email, you click a button, you set up an account, it takes usually less than three minutes to get that done. So go ahead and grab that on that. Also, if you can sign up for a DevNet account, while not technically needed for the session today, if you want to follow up, get code samples, run other learning labs, it's kind of good to get it out of the way. It's also a little benefit for me, right? Cisco lets me come and do these things, and I can actually prove that people listen to me, and I actually showed up, that's great. So if you go to this link, cs.co slash pi Ohio 17, you're then linking yourself that you were hearing about through, and I get credit, which everybody likes credit, it work, right? Well, I like some credit, so do me a favor. Hit the link, just hit register. If you already have a DevNet account, do it anyway because it'll still give me a little credit. There's also a bonus, so I've got a few shirts up here that we're going to give away as well as plenty of stickers. Shirts today are for good questions, are really an interesting bot creation. Although everybody can get a shirt, so if you do sign up and go through this, if you just tweet or spark me a message showing that you signed in, give us your address, we'll ship you a shirt. And these are actually really cool. I will show you my API, if you show me your shirt, very clever. Everybody likes one of those. So a huge swag, so you get from just signing up for DevNet today. Now the lab code, the lab code is up on GitHub, like most code these days will be found for things like this, hbreston slash lab, understore check norris, understore spark. You can get clone that, or if you don't want to go through the whole process of get clone, you can just do like a WGIT or a curl, or just hit this web address, it'll download the zip file from GitHub, you can unzip it, and that'll work fine for the lab as well. I'm going to leave this slide out for a little bit as people go through, like I said, I've built in plenty of time to make sure these ones go in so I'll make sure that it will do some pulls occasionally as we go through. I'm going to shrink it to the size a little bit so that I can do some of these to redirect myself. Can you still hear me, like, try to hover over the microphone? You guys, I'm fairly loud, so you can probably hear me without the mic, so it's more for the recording. That is a great question. So the question was do I need to download Cisco Spark? You can download the client if you want, but you can download the client available at web.syscospark.com which is what I'll do for most of the demo today. I'm just going to download the zip file, so when you go to web.syscospark.com it'll ask you, if you haven't done this already, it'll ask you to email if you have to just put it in. You don't have an account yet, it will set you up one which involves sending you an email to confirm and all that other kind of stuff, and then you can go ahead and log in. I've learned my lesson over asking questions and figuring out status, so rather than ask who's done it already, I'm going to say who still needs time with some of these results. We'll hover here for a little bit. While that's going on, are there any questions so far? We haven't done anything terribly exciting yet, so the question should be quite entertaining. Yes? I'm just trying to guess. But you had the registration? Yeah. So it probably works. If you just open up another tab and go to developer.syscospark.com and see if you may have to hit the login button and try to log in. When you log in and register, you can give out or any of the credentials. It's developer.syscospark.com Yeah, it's developer.syscospark.com will be the URL. DevNet.syscospark.com will get you there as well, but the official URL is developer.syscospark.com. Is that what it goes through? That's the problem with these registration. They change our registration flow a few times and I ran out of email addresses and registered new accounts with it, so it's probably an API for that. Anybody have any problems? What's wrong? The DevNet doesn't seem to go anywhere. Does it just get you a getting started page? How do you get excited to go and look in? You're looking. You're interested. Skip it. If that's not an issue, just skip it purely and we'll deal with that later if you've got time. The syscospark one is going to be important for the lab, so as long as that one works we're in good shape. I don't want to be the educator. I get to find issues and have somebody else solve them. You've got a good test base. Yeah, I know. We tested it out. It didn't work. How are we doing this? Who still needs time? All right, no hands. Next step, let's join the Spark Room for this lab. Now that you have a Spark account, you'll be able to join a Spark Room. Spark Rooms are similar to a Slack channel if you're familiar with those. If you go to this URL, I'll put your email address in. That's a great question. No, it's not. I have nothing to do with Apache Spark. This is a syscospark. It's a collaboration platform. It has nothing to do with big data, but I'm sure they're using some big data in there somewhere. Yeah, collaboration. Yeah, we did have a lot. I didn't name it. If you go to https.io slash and then it's a pound symbol of RKG. It's just random characters and it does end actually in a dash. That'll take you to a page where you can just put your Spark email address in and it will dump you into a room for this class. So this will help. So as I've got links or information during the lab, I can dump it into the Spark Room so that you can grab it and go through on there. So that's the big purpose of doing this bit here. Yeah, if you have a client, you don't have to use the web. You can certainly use the client as well. You won't be able to find a room publicly. So if you go to this URL, put your email address in that your Spark account is with. It will just put you in it and then that room will show up in your Spark. Once you're made a member. Do you just say no? There is indeed a dash at the end. Throw me off to the first house. Pre-requisites, the least interesting part of the lab. What important? Do you still have web dots? It should have put you in the room and then in the client. There's a web client. Once you're in there, it will show up in any of your clients. Whether it's the web page client, your laptop client, mobile phone client, it doesn't matter. Then you go back to the tab where you've got your web client. Then you will come up there. It will. All the web pages doing here is what this page is It generates a random URL that then will automatically add people to a room for you. So it's one of those pieces. It's kind of the equivalent of click this link to join it or be invited to do a Slack team. Same type of thing. Things have started. Excellent. Who is still trying to get into the Spark room? Is the URL not coming up for real? It did and it brought me to already in Cisco Spark space. Okay, then if you go to web.ciscospark.com you should see it in your client. So if I make this page wider the full client will have a list of rooms or spaces over on the side. You should see one that says Pie, Ohio 2017. Once in there you can participate in the excellent random chatter. 27, so far. Are we there? Anybody still need time? You're good. I'll even put the command in the Spark room so that you can just calculate if you want to join the Spark room. All right. So if you get cloned, I'll pull it down and go through it. So once you've cloned it down if you change into that directory that has a little code in you're going to see this set of files that are there. So let's talk through what we have and kind of how we're going to use them as we go through the lab. So these are not in the same order. These are in the order from you. Yes. One more time on that URL. Which one? To join? This is the Spark room. I just I put it in the Spark room. Make it easier. Yeah, absolutely. So in here, in the order that makes it easier for me to explain, so nodes.text.sample it's the file that we're going to use for scratch pattern in the lab. So everybody can just do cp, copy or if you're on a Windows machine or something else what are the equivalent on Windows is to make a copy of nodes.text.sample to just node.text. That'll be your copy. You'll put your own information in there on that side. So as we get closer to the end we're going to package our code up as a Docker image. That is a sample or the answer key for that part of it. So if anybody doesn't feel like typing a whole bunch you can just kind of switch to that. Similarly we have myBot, underscore zero, one, two and three. Those are step points in the lab that show the different styles. This is a Python Ohio session. So I'm going to actually encourage people to actually take the code where normally I just say grab a copy and paste but if you do get a little bit you can just find the answer keys in each of these solutions and as we go through the lab we'll say which code solution file we'll look at. Requirements.text, right Python people, those are the PIP requirements README is just the standard README and then the license for the file. So that's what you're going to see in there. Nothing too terribly shocking on it. And so if I go over here and I'm going to do my let's make that bigger so we can see it. Notes.sample to nodes.text and then I'm going to open up notes.text and when it opens it's very short. It's just a list of export statements for all of the details about our bot that we're going to need. We'll be filling in data into this file as we kind of collect it through the lab. Anybody getting needs some help gets stuck with where. What do you got? I'm already in Cisco spark states but my spark is not showing up in the program. It is not. Let's try the equivalent of a web application of unplugging your computer. If you log out of the web client, log back in. Let's try that. You can log out into the gear and then sign out. Yep. I'm going to join the space using the desktop application that I'm not going to figure out. So did you go to the URL dot the URL and put your address in? You'll still need to do that. The space exists and it will let you join but you do need to put it in. So if you go to that URL dot IO and then that's a bit in piece it'll let you join. Once you join using this URL you'll be able to find it in any client that you use whether it's a web client, mobile phone, desktop client. Thank you. Did you get it? Okay, excellent. So does everybody have their notes dot text file open? Anybody stuck with that? We good? Excellent. So the anatomy of a bot. So now we've got all the pre-writes done directly into the the piece that goes here. So you're all painfully aware of how hard technology is at this point. Let's sit back for a second and relax and just talk about how this works. And so the anatomy of how humans chat, right, we're all fairly familiar with this. We've got one person on one side they use whatever their platform of choice is they ask the question, hey, what is the meaning and then somebody replies back and says it's a 2PM. Really simple, easy and international. The point of a bot is usually to provide context. So in a bot we're simulating the human with code but kind of a more robust full-featured human. It's got all sorts of like other information that they're going to provide. So in this case, if we're asking a bot when is the meeting, your answer might come back hey, it's a 2PM, but it's with Hamlet discussing his friend Yorick's interest in joining the team. If you want to learn more about him, click here to see his LinkedIn profile. So you got the information that you're asked for but maybe other information that you could be interested in. And that's a lot of the ways why these bots are being created by companies is to try to provide that direct interaction, customer service, support that goes through, leveraging all the capabilities that are there. So the next bit is notification for humans. So if someone sends you a message how do you know? Most of us are using some sort of smartphone we get notification on our page the notification indicates us that we had an incoming message, maybe I want to read it and do something with it. Bots need the same thing. The bot is sitting out there, it needs to know some way that somebody sent it a message that it has to react to. The majority of the bots, including Cisco Spark operate using webhooks. Anybody heard the term webhook before? So you're probably handled. If you're not familiar with a webhook, what it is, it's just an API call or REST API call coming from one system to another. It's a notification mechanism. It's most often an HTTP post, a REST post request that provides an indication, a notification that something happened. So in this case, the general format of a notification will look something like this. It's going to come through an app ID, identify what application who is sending this message, where is it coming from? The data, right, so the data, the ID of the webhook. So in this case, it's the ID excuse me the idea of the message that came through, who sent it and this came from Ophelia. And it was what room ID? It kind of spark every interaction space, whether it's a one-on-one I'm talking with you, or it's a gigantic space with hundreds of people in it, every room has an identification number. And that's what this is. This is the ID that says, in this bit of data of how it says Ophelia sent a message to this room if you want to see the message go get it. This is the message's ID. And then it was an event, so the event, the kind of metadata about the webhook itself that it was created, and then the webhook ID is going to be listed there. This doesn't include the message. This is just a notification that says somebody sent you a message. Webhooks are used all over the internet. If you've ever done anything in continuous development in githubs, notifying Jenkins, or Drone, or TeamCity, that notification is done through a webhook. If you've ever gotten a notification in Facebook because something else happens someplace else in the world those are all webhooks that go through. They use basically the same way. And we're going to leverage this technology for our bot code so that when we send a message to our bot it knows to go get the message, read it, and process it. Questions? Spark Webhooks, as a development platform, has a REST API of its own, and has a full capability to create messages, rooms, accounts, teams, video calls, voice calls, like anything you can do, you can do through the API to some extent. Put the caveat at the end of that. But we have a full suite of APIs. Get, post, put, and delete so that you can manage webhooks through API calls. Now in a lot of cases you would have to go in and register your own webhooks, set them up. One of the values of the bot library that we created that we're going to take advantage of today is it actually does all this for us. It uses the Spark API inside of the Python object code to go ahead and register the Spark webhook, or the webhooks in Spark for us. We'll look at that once we have been created, but we don't actually have to go create them ourselves, but understanding of the hood, I'm always a huge fan of what's going on in the piece. So the Cisco SparkBot library, this is what we're going to use. It's actually a Python library that I built along with a couple other members of the development, a couple other members of the Cisco SE team to make this easier. Because we have a whole bunch of people at Cisco trying to build SparkBots for a whole bunch of things, and everybody was creating their own way to do it and spending a whole lot of time on all of the bits and pieces that had nothing to do with their actual projects. It had all to do with just how to stage or bootstrap with their bots going. So we wanted to make it easier for folks. And so to do that, we created the SparkBot library to quickly build SparkBots in Python. There are a handful of libraries as part of the SDK that Cisco offers for Spark. They're in JavaScript, Node, Go, you can do them in any language. Because it's based in a collaboration place, a lot of the initial development, the places that a lot of people are building these bots in is something Java related. Node, JavaScript native. As an engineer coming with a background in Python, Python works for me, I hate Java, I'm not a huge fan of JavaScript either, and so I'd rather do it this way as a lot of the engineers that we're teaching. It uses Flask as a web framework. We'll look at the code a little bit under the hood. Basically what we did is we took Flask, we extended it with all of the logic necessary for the bots that are in there. Under the hood it's using the Cisco Spark library, which is a lightweight client for Cisco Spark to interact with API, so you don't have to make native REST requests, it's just a Pythonic way to interact with Spark. That library is also available if you just want to do something other than bot creation. That's an open library you can grab. And then all that web hook setup non-sect is all handled automatically. Which means this is the code necessary to stand up a fully functioning bot. And actually there's code in here we don't need because what it's doing is just illustrating how we add new capabilities. So from Cisco Sparkbot we'll import Sparkbot. We then have to grab all of the details, like what is our Sparkbot's email address, what is its password, what is the web hook URL, like all that has to come from somewhere. We're going to use environment variables for that because it works really easy, it's a modern development technique to do that. So we're going to create a bunch of four environment variables that contain that information and we'll pull those into our Python code in the OS library. We then, in this case, are showing how we create a function to teach our bot to do something else. So skip that for a second, but this line of code it wraps, but it's really just one line of code is creating a new bot object from Cisco Sparkbot and then passing in all of the information that's being imported. We skip this one for a second and then bot.run. So functionally you can get a fully running Python bot up and running in Spark with two lines of code. Create your bot, run your bot. You want to teach it to do something new you write a function, define your function, have it do something and then you just add that command to your bot. That's the basic process. Now the code for Sparkbot actually came from an initial boilerplate for building bots that we created to help RSEs go through and it was about 300 lines of Python code you can start with that and then at the bottom of it you would do like this and this. And so we refactored, everybody refactoring before, write code the first time and realize how you wish you would have done it the second time and then redo it. And so we refactor all of that boilerplate code into the Sparkbot library to make it this much easier to go through on it. Questions? Alright and we'll get deep into this code as we go to build it. So the question was how do we build a bot used for internal but in you want to prevent it from being available externally? It's a great question. So because this is a cloud platform just like any of the other ones when you create your bot account it exists along with everybody else's account in the world but when you build your code and what to do when messages come through you can filter and say only respond if this came from users in my domain and you can just ignore the rest of them it's just part of the response. It's the same thing that goes right? You've got a Facebook account or a Twitter account yeah anybody can send you a message that you can ignore them, same equivalent here you can build your bot just to ignore people you don't care about. Good question. Other questions? So let's build a bot. We talked about what we're going to do now let's actually get started. So the first step is other than generally if you're building on the internet in general you can skip this step but the rest of us need to get our bot up on the internet. We'll do that first. So what we're going to use EndRock to do is to expose our bot to the internet. We don't want our bot running so what we're going to do is we're going to prep the tunnel from the internet to our laptop on the port that we're going to run our bot for and so in your terminal window I'm going to open a new one you can actually run EndRock in the background and that's fine if you know how to do that you're an EndRock power user feel free to go off script I'm going to show this in a new tab so that we can kind of reference back to it a few times so it's simple if you put EndRock someplace in your path then you should be able to run this command from EndRock if you haven't gone through that step just go to the directory that you put EndRock and run EndRock TTP 5000 and this will stand up and start our tunnel now the command EndRock the file or the command that we're going to run the application we're going to run Http we're building a tunnel for Http use EndRock supports other like just basic TCP UDP sockets you need an account for that it's a little for lots of good reasons but for Http you don't need anything fancy and then 5000 is the port on our laptop in our development environment that our bot is going to run on now we're based on Flask so Flask defaults to 5000 which is why we're using 5000 you can certainly change that to whatever you want if I have my tunnel up and functioning everybody got that next so I'm going to switch so I switch back to where my code is at this point and what I want to do what I want to do EndRock what was that a couple of lists on there EndRock that's okay don't fall behind EndRock space Http space 5000 I'll put it in the sparker what you'll see in the output from EndRock is you'll see 240 addresses you'll see one that starts with Http and one that starts with Https EndRock is nice it gives us a little bit of security in there so I want everybody to copy the URL for Https all the way to .io just copy that and then over in your notes.txt file that you opened paste that after sparkbot URL this little notes file this is going to be the information for our bot to go through so I've taken the Https address I've pasted it next to sparkbot URL you are welcome to use Http if you are less security inclined than I am they both work by script we are not passing anything super secure through it but if you are going to be doing some secure information in your bot gathering electrical property questions about your customers you certainly want Https anybody not get EndRock the tunnel up found the URL you good? yes question is it the forwarding URL? the one you want is the forwarding URL and the one that starts with Https or some random numbers and letters and then it will end in .engrock.io so let's create a new bot account and so what you created today or if you already had it was a Cisco spark account for you a place for someone to send you a message for you to send messages back from though you can certainly use your own account for this class you probably want to create a bot account something to represent that functionality so what we are going to do is create a bot account inside of the Cisco spark platform and so we will go to developer.syscospark.com put it into so developer.syscospark.com and then login I already logged in but up in the upper right corner there should be a button that says login if in your browser session you are already logged into web.syscospark.com you probably won't have to put your credentials in it will just grab it and do it so don't think it is black magic it is just the lovely nature of web like web protocols you would already have an account or just a shade symbol in the corner waiting for everybody to go through if you are ever experimenting with the Cisco spark API the way that the API is done is not through username and passwords for authentication it uses tokens kind of an OAuth type of a framework you can get your own personal token for making API calls or even development by just clicking on your avatar put you in the upper right corner and then you will see my access token now it is longer than the 12 or so characters here you can double click on it click the copy button it will grab your full token and then you can use that for the bits those tokens do automatically refresh building an entire code based on that not a good plan but if you are just doing some test bit using your own account so the question is how do you set up something under your own API without using the token refresh so if you are going to do a piece like that you normally do the same way you would do any web development which is kind of where we are headed you can see my apps and click the add button you can see I have two options to add applications versus go spark and integration in a bot we are going to do a bot and in a bot case what we do is we create kind of a sub account for ourselves which is another bot it lets you interact or there are some differences we will cover a bit of those as we go through but if you are building an integration you want to have your application leverage Cisco spark in the back end you build an integration which then goes through typical integration pieces you would set up an OAuth you get a client an ID in a secret and you just build it into a authentication framework so when someone logs into your application it uses the same refresh cycles make sure your tokens are cut that to date which brings us along so the next step is click on my apps and then click the plus button and then click create a bot we are going to create our bot in here so the bot account is going to do what we actually are interacting with that is what we are going to run our code that is the identity as we go through and so for the display name you can pick anything you want that you want to go through in this case it is just the display name the friendly name so spaces are ok all of those other business pieces I am going to call mine hyo hyo for the bot username this is the identity this is the equivalent of the username in the spark system you will see that it is going to be suffixed with an app sparkbot.io this name has to be unique across spark so don't use the same one I use the whole app is going to get weird because we are all going to be fighting for the same username the exact same time I am going to call mine py oh hyo show up and see if somebody else has already done it they have it the green chat tells me at least currently that name is good if somebody else steals it from me I will find you and then the way that the bots go it is required in the system I don't know why we have this required but it is there has to be an avatar icon that goes with it do you have a favorite avatar icon available on a URL that you want to use great if you don't I set one up for you to make this easier this is in the spark room in just a second so don't worry about typing it live so if you copy and paste that you will know that it works if you actually see the devnet logo show up underneath the icon if the logo doesn't show up you have to type it or I type it if you have a URL there is no upload button which is another thing that I filed it needs to be available on a URL it is highly recommended that it be a square image so it doesn't distort and it has to be at least like 512 by 512 if you have your own file you can use it if you want but that tends to slow down the process as we go hunting who will image it so I made one to make it easier and those are changeable after the fact so you can always change that once those are in place click the add bot button it will take just a second to process the add request if we scroll down what you will see is the my access token the my access token is the token for your bot this will be different than your personal access token because the access token in any API is kind of a combination of username and password it identifies this bot to spark so we don't want it to be the same thing as us we want it to be unique for our bot so copy that you can just use the copy button and we are going to paste that back in our notes.sample sparkbot token if you navigate away from this page you will never be able to see that token again because of security but you can always regenerate a new one when you regenerate a token it invalidates the old ones that are there so it's always a good idea when you generate tokens like this the same thing works if you've ever used an EWS token or an API you can only get it once after that you can only regenerate them okay so we captured that we now have our sparkbot token for the sparkbot now in our notes file grab the bot username this is the one that ends in at sparkbot.io in the notes file the last field we need is an app name the app name is used in a couple of places in the library but the fundamental one that's used is to create the name of the web book that's generated I tend to just name it the same thing as the username without the at sparkbot.io so before where do you have the first one, the sparkbot URL that was from ngrok so when we inspired up ngrok it's the s forwarding URL so we've got four pieces of information the sparkbot URL as I just mentioned it's coming from ngrok that's the forwarding address that's a tunnel it's a way that we can send webbooks to our application we have our token that's the authentication information for the API calls we have our username that's the at sparkbot.io component from the spark interface and then the sparkbot app name just a friendly name that we're going to use in a couple of places through the bot most notably how we register our web book four bits of information anybody still need time gatherings excellent so with those in place we've saved them we've made notes of them where we can use them but now we actually have to make them available inside of our terminal inside of our running environment so that we can use those in our code so there's two ways we can do that the hard way and the easy way I'm a huge fan of the easy way and so you can inside of your terminal you could type you could type export sparkbot URL or like over and over and over again but I was really kind of nice to everybody including myself and I created the notes.txt file we're restoring all the information and put it in the same format and so once you save it don't forget to save your notes.txt if you just type source notes.txt it will go ahead and inside of the running terminal just this instance if you close this terminal they go away and you have to resource it it makes four environment variables available we'll test that by saying echo doesn't spark so everybody can test it echo sparkbot URL you should get back your URL it just verifies the terminal of our environment question for what? set alright so you won't be able to do the fancy source bit you can give it another one or change it and it's set just replace export with set I actually provide the laptops for it so the windows question doesn't come did you use the set to set them? yeah and you should be able to echo the windows right? surround it with percent percent center right? percent instead of dollar sign on both ends how many other windows are there? okay so we'll fix these on the fly as we go through echo the set is right set check okay I put in this sparkbot and that should go through oh no we're losing more we already got to the check door we're almost there I promise that's okay good publicly shame the people leaving make it harder for all of you my windows users did that work? was it someone Kevin? can we import someone to help? hopefully we can plug through on that one on the actual board alright how about my my lamps and mac can we go shake the echo working? good we'll see do you need an update? yeah how do you import set yeah yeah you'll have to change this change it from export to set yeah is there a bullet to source windows in there? you create a file you want to source it just do it one at a time let's change it back just bring it okay man I need to do that it's one of those I should have tested this one I didn't think it would be this easy I do a lot of other ones I delivered this class like a dozen times most of the time I'm getting the laptops to go through exactly what's on them this is the first one I did I'm going to bring you the next one hopefully we'll get those a couple more so for those that are so we can make some progress we'll pause again we want to build our base code so to do this so what we want to do is we want to create a file called mybot.py this is going to be our bot code before we do that I got excited because we lost the environment variables so we've got our environment variables set up now we have to set up our actual python environment get ready for that I'm going to use a virtual environment if you've got virtual environment I highly recommend it I'm on that side so what I'm going to do is create a new virtual environment virtual environment I'm going to call it vnv and as I mentioned this lab doesn't necessarily absolutely require python 3.6 so my code samples are there python 3.5 or 3.4 would probably work python 2.7 does work but there's one spot where the code is different because the URL live changed and if anybody is doing this with 2.7 just raise your hand and get to that point and I'll show you but for me I'm going to create a python equals python 3.6 this is creating a new virtual environment does anybody not know what a virtual environment is I'm happy to explain it while I wait for the command to finish again not surprised here normally that's a question that people don't know alright so once that's in place then we will activate it source vnv activate and I will pip install hr so if you look at requirements.txt the only thing that's listed in there is the sparkbot library now that sparkbot library like most python libraries use I'm trying to get to where I'm at so these are the commands that are interesting right now is this part critical it doesn't have to be virtual environment it doesn't have to be virtual environment you will need to do the pip install hr though the pip install or requirements file just has the cisco sparkbot library that library then in turn uses the cisco spark api library to interact with the api from cisco spark and that library uses requests and a few other ones to make recalls which is why when you do the pip install you see a bunch of stuff install it's just like most cases where you do a pip install requirements might only have 2 or 3 you end up with dozens same thing here so if I do a pip freeze we'll see that I do indeed have cisco sparkbot it's currently version 5.5 v0 and a handful of other ones just a flash because I mentioned sparkbot is built to build on top of flash it also has a sparkbot api library requests and a few other kind of core cartography libraries that are needed for api calls so nothing too terribly unusual or fancy I'll put those commands directly in the sparkbot and you said it's fine if I use 3.5 right? Pardon? you said it's fine if I use 3.5 yes but I found 3.5 would work as well I put 3.6 because that's the current one I don't actually know what anybody would recommend in these days so these will work great on a window or a map or a Linux machine the Windows machine might be slightly different commands to do some of these bits and pieces but in these cases these commands are going to create a virtual environment looks like somebody put a check mark somewhere source the virtual environment Pippin's Follower requirements the touch command on Linux or Mac will create an empty file and then we're going to open that file you just sent those messages to the shop oh that's what it was going to use you got so many rooms now they're on this but the room is all yours so those will go through on the app and then what I end up with if I end up with an empty file my budget will be white so this is where our boss is going to be created I can hear it down the hallway alright so this is the base bot this is our first version of code we're going to go through so let's take a look at it and see how it's going to work as I've stated this code exists in the code directory is mybot-0.py if anybody wants to type it to practice your Python great you're going to see me as soon as I'm not talking about the slide go through and execute my copy command so that I get these files in there and so our bot we will import OS that lets us grab our environment variables that we set and from Cisco sparkbot import sparkbot so that we can create any bot objects these four lines make available inside of Python his names the equivalent of all of the environment variables so the way that we use it is os.getenv and then in parenthesis you put the name of the the name of the environment variable the OS library in Python if you're not familiar with it is the way that you interact with the operating system it's a handy library because that library in this code will work whether you're Linux Windows Mac it will go ahead and function and operate the same way here this wraps the four lines but it's one line of code we're creating a new object called bot based on sparkbot we're passing in the variables for our app name token URL and email and then lastly we're going to run our bot now that run command probably looks similar if you've ever done any flask development because we're built on top of flask that's actually the dot run command is inherited by the sparkbot object from flask directly and so we use the same command we're going to run it on 0.0.0 and it's going to run on port 5000 if you want to get frisky you can change that from 5000 to something else but then you're on your own to figure out all of the other steps you have to do to fix ngrock and all those other bits and pieces so I highly recommend stick with 5000 so that's the code and as I mentioned I'm going to cheat my bot 0 my bot.py and voila I have my code see how fast I typed that so this isn't even going to be the checknors what we're going to see here this will be the checknors doesn't type code he just thinks it what we're going to start here is the base bot so we'll go ahead and get our bot up and running on that piece anybody need more time to get the initial code that's fine feel free to type or copy and paste any questions while we wait it is so if you've cloned this down or your pencil and paper so once you're in there I will actually take these slides and put them in the github library or stick it in spark so you can follow along as far as that goes but all the answer keys at each step along the way are included in the code library yep absolutely if you want to look at it so you don't have to look up and down as you go through or you can cheat and practice command C or control C any questions on the code itself I usually don't go through that but again this is an actual python conference so if you've got questions on the python code this is fairly basic python code but if you're new to python and just getting started it can certainly kind of throw you off on seeing some of these bits and pieces we good anybody need more time still I have questions R yes and then command C should work there must be something on your laptop it's working what browser are you using that's the same one I'm on so you're doing it the same way I am command C yeah I don't know so the next step is let's go ahead and fire up and run our bot and see if it's working yeah question yeah if you look in the spark room you create a virtual environment activate it and then install the requirements pip install dash R and then if you just want to catch up real quick just grab the my bot underscore zero that's the exact same place we are in the lab okay perfect alright so let's fire up and run our bot and see if she's going so if I run python my bot dot py what I have I must yeah I've got one running somewhere here there it is I was testing my lab everybody if you ever teach something it's always good to test in advance I just never shut it down that's better alright so I now have the bot running on my laptop okay it's just as I've said several times it's built based on flask so the output will look the same we do see that we output the web hook ID if you want to go if you're debugging or looking for other information we'll look at some of that in a little bit but our bot is now running so now what I want to do is actually go and test and see if he's functioning as expected now we haven't put the Chuck Norris in there yet but we have the base kind of the boiler plate spark bot that's in there and so if I go to go to the web.sysco spark in here and then in the upper corner if you're not familiar with spark this isn't the most intuitive bit of the client but this little plus button is how you create a new chat and I'm going to create a chat with pie Ohio Chuck pie Ohio Chuck and I'm polite I'm going to ask I'm just going to say hello to pie Ohio Chuck and cross my fingers keeping them crossed nope there it goes the first one I think it's a way the web hooks are generated with the code sometimes you got to send it twice so I send it twice and we get back the default help message from the bot so he comes back and he says hello I understand the following commands so we didn't write code for this spark bot library has that's in there automatically now I'm interacting with my bot one-on-one now I mentioned there are a couple of differences between a bot account and spark and a regular account and one of them is the way that they interact and fire with web hooks so here inside of a one-on-one room I can just send him messages and I can test so if I send slash echo test it will work and he should echo back test and everybody should be doing the same thing on that now we can go in and we can add our bots to a room so I've seen someone trying to fire off some of the commands in here we actually have to put the bots in so in the upper right corner everybody should be able to do this you see the little grid of dots and you can say go into people and then add people I'm going to add my pie Ohio Chuck bot you are all welcome to do that and we can have them chat though it might get a bit noisy and hard to find them so for most of the testing I recommend doing it in a one-on-one we can do it in this way as well pie Ohio Chuck so I've added him the way that spark chat deals with this and it's for security and some logging pieces if you put a bot account into a room in order for the web hooks to fire you actually have to mention the bot in there so if I do at pie Ohio Chuck and then say help it will fire the web hook and go off on that the reason I have to mention him again is because this is a multi-person room it's considered a larger space and it's just functionally the way that the apis are written and the integrations were written was that in order for a bot to receive a web hook from a multi-party room they have to be mentioned in the piece you mention the at symbol and then you start typing their name and it'll filter through and hopefully as I went through I've seen bluebird where was mine there he is so here was mine so I did pie Ohio slash help okay if I don't do that if I don't successfully mention him if I just type pie Ohio Ohio Chuck I've got his name in there but you'll notice it's not highlighted it's because it hasn't been recognized that I've actually tagged him and it's just kind of the way that it goes through so in order in a large multi-person room for a bot to receive the web hooks they have to be explicitly mentioned now in a one-on-one room that goes away so if I just go back to my one-on-one room I can again just say help and they will go through and it will process successfully okay that's unique to a bot account if you were to create an account a full-fledged account and create your bot using that token and all the other bits and pieces they would get every web hook for every message that was ever sent in any room that was going through there because you can overwhelm your code like that question so did you use your auth token instead of your bot auth token if it is there's something responding on behalf you probably use instead of using the bot code you probably use the other one so to fix that go back into developers.cisco spark if you still have it open just make sure you grab your bots token again not the one that's in the upper left corner grab the bot put it into the notes file resource it and rerun the code and it should come up and start using for your bot okay so let's look at what's going on so when I send the messages if I go back to the if I go back to the ngrok window what you'll actually see here are the interactions that ngrok is getting so every time I send a message it's a web hook that's fired from spark spark is sending it to the ngrok url which is why I see all of these 200 ok's the post coming through and then what ngrok does is then forwards that to your code so if I look at my running code it's coming through here so I would say let's see where you're coming through do you see them on ngrok so in your what I would say do I output that I don't so inside of ngrok what I would say is kill your bot go back to your notes.txt go back to notes.txt and make sure the spark bot url is the forwarding address for ngrok so put that in kill your bot, resource the file to reset the environment variables rerun the bot and you should start seeing the messages come through how many people got their bots to respond alright a vast majority you don't have to I was just showing the piece on that if it's working one on one that's perfectly fine question not yet on python 2 everything at this point should work exactly the same but when I get to the point where there is a change I will mention it open up your mybot your mybot code doesn't look right okay any other questions yes in the status let me see where's your developer window can you scroll down let's scroll back up copy that copy the bot username I think you something went yeah try again yeah I think you had a dash instead of an underscore okay anybody else have a question yes so the question was the name that I'm chatting with the name or the email so this is kind of like a lot of account type systems everybody has several things that identify you so as far as spark is concerned the way that it's finding you is the email address so it's whatever your bot is at sparkbot.io right so what it will try to do though is just to chat with is the email if you put in the display name spark will go looking through trying to figure out who you're talking about and that can be hit or miss so I would say in that window put the sparkbot email and then hit the go chat and then you can send the message find it usually especially if you've got an account that's part of assist like a company it's great about finding directory before if you've never chatted with somebody when we're talking about these global bot accounts sometimes you have to put it any other questions alright so we've got our bot running and he's up and functional but we're not telling Chuck Norris jokes and I promise Chuck Norris jokes so let's go ahead and move on from there so the next step we did that so let's bring on Chuck Norris and so the way that we're going to do this is we're going to use as I mentioned before my favorite API which is the internet Chuck Norris database icndb.com it's an easy API it's actually very rest-like I use it for teaching APIs all the time because it goes through on that so in this case we're going to use it in that space so the first step we have to do is we have to create a function that knows how to go get a Chuck Norris joke and so we'll create our function def ChuckJoke underscore and then in parentheses message now what we're going to pass into the next step is we're going to use the message now what we're going to pass into this joke is the message that was sent to our bot in this case we're in this first version of the piece we're not actually doing anything with the message that was sent but the way that the sparkbot library is built it expects a function to kind of it's going to send the message at it so we define our function I'm using URLlib to make the REST API request I'm doing that because I wanted to limit what it takes to do for this piece I'm using some other piece that's here but in this example I'm using URLlib and then I'm importing JSON because the API call is going to send the data the joke back in JSON format everybody familiar with JSON? yep okay so then we create a response object URLlib.request.url open it's a really easy API it's just the host slash joke slash random give me a random joke back the API supports all sorts of other options with some other names so you could actually put yourself into the jokes like it's a really fun API to work with we're using a basic one here we're then going to JSON.loads the response and then grab value.joke out of the dictionary that's sent back from the JSON and interpreted and then we return the joke which is just the text of the joke that goes through on there this is the function code you'll notice Python 3 up there and it's because URLlib changed from 2 to 3 so I'm going to flip this over in mybot underscore 1.py is this code if you want to grab it I don't have it subbed out for the 2.7 but I do have the code here so if we look at what this code looked like in Python 2 I had to make these changes instead of importing URLlib or importing URLlib 2 and then the response is URLlib2.url open and so for anybody using Python 2 I'm going to leave this up because I don't actually have this code in the repo because I ported the code all the Python 3 actually just this past weekend for this class to go through on that everything else is the same it's just the URLlib library changed here I may actually in the future change it to request so that I don't have to have this bit that's different leave that for a second question we're not using it so this function doesn't actually use it but the sparkbot library when we go to add this to the bot we pass in the message so despite the fact we're not using it we have to have the function as to support it it's an intricacy of it in the future what we can do and I have no idea if we'll have time today or not what you can do is you can actually take the command to run this and then parse information from the message so if someone wanted to see really? if someone wanted to change out their name in Chuck Norris or pick a category we could grab that info out of the message alright so we got that so the next thing we have to do is so we create the function and then we have to teach our bot how to call it and so the next thing we're going to do is we're going to add bot.add command and then there's three parameters that add command needs needs to know the command that is going to listen so what command are we teaching we're going to teach our bot how to respond to slash chuck remember it already knows how to respond to help and slash echo the help message we're going to add a random Chuck Norris joke and then the function to call which is chuck underscore joke that's the function name that we created on the slide previous and so I'm going to again this code so the full code the full working code is stored as my bot underscore 2 so I'll grab that and put it into my code I'm going to kill my running bot copy my bot underscore 2.py to my bot.py and I get the full function that's here so what we added again we created a new function called chuck joke and we taught our bot how to tell a joke with the add command bit we're at about 30 minutes so at this point if you're executing the typing bravo go ahead and use the copy command at this point so we can get them all working so we've got our bot up and functioning using these bits and pieces any questions on the code that we've seen here so now that that's in place the next thing that we want to do is we want to actually restart our bot with the new code that's there so I already killed it yep question so the question was are commands arbitrary or does it have to be slash in a command it can be whatever you want it's just a string so the way that what the bot code is doing is it's going to add into the bot dictionary the list of commands that it knows slash chuck I could put till the chuck I could just put chuck I use the slash notation because it seems to be something that a lot of bots are using and sticking familiar but it could be whatever you want and then the string that's here is just the help message so remember it responds back with a help message so this is going to be what's input into the help message that's there so the question was if your bot is the only one in the channel or in this case a room or a space is the spark term for it's fine no no I get it it's you're not going to offend me at all so the spark has a concept a room is either a one-on-one a direct or it's a format with a lot so if you built a room that was of a multi-party room but the only people in it were you and your bot it would still behave like a multi-party and you would have to mention them but if it's a direct message then you don't have to mention them it actually wouldn't let you mention them so we've copied the code let me go ahead and run it so I rerun and when I go back to spark and find my bot if I rerun it slash help I should now see a third command I'm getting there look at that unknown response code it worked for somebody okay well that's a good sign so this is I got a note that they're actually doing spark cloud maintenance this weekend and I tested it this morning we were fine but it seems like now they've stopped killing my debug on the fly trace back what did they do to spark we were so close is my other check mark still working spark maintenance alright we're going to pretend it was miraculous I have a working I got a history for a working one so we'll go off script here a little bit and love the cloud and love live coding so what it should have done is the new help message would have looked like this we added a command call slash Chuck it gives us the help message that comes through here and then by executing the slash Chuck it would use the API to go get a Chuck Norris joke from the API it would then reply back in the spark room for it and it would go through on that hopefully the spark cloud will fix whatever the issue is here shortly and they will start working I'm going to plug ahead because this is actually the end of the live coding bits and we'll talk about the other bits so the question was after slash Chuck would you put a message in so if I put slash Chuck and then something in there that actually does get passed into the function based on the way that the bot works but our code didn't do anything with it so it wouldn't have any impact now we could we could parse the message and we could put slash Chuck category equals nerdy and then we could update the API call to go get nerdy jokes only like there's a lot you could do with it correct just the string everything other than the command is all passed in I think the way okay so we've got those up and functioning on that and that's a real bummer man and when I saw that email this morning they're like nobody should notice a problem the cloud nope all right that's a real bummer okay so let's let's continue along that's what it should have worked alright so we checked our feature we present so look under the covers where are we on time 20 minutes so let's look at a couple of bits and pieces so if I go to you just watch at this point I'm just going to do a bit of a demo on how some of these bits work so I clicked on the URL here which actually just opens up the n grock like command window on our laptops this would work for everybody what's nice here is we can actually see the webhooks that are coming through and so you can monitor those as you're troubleshooting them so these indicate the contents of the webhook that was going through and there's some flags you can turn on in the spark bot code where if we go through and if I went into my code and on the bot creation I can add and debug equals true and what the debug equals true statement does is in addition to the logging that the messages come through the spark bot the python code and instead of the stack tracing we're currently seeing we would actually see the message output the full json blobs everything coming back and forth from spark so for debugging pieces so that was built into the code to make it easier to debug and then if we go through actually I'm going to skip the looking inside the spark bot code because we won't have enough time for that but you're always you're welcome to go through and actually look at how the spark bot object was created on that if you're interested in how we wrapped up all of the we built on top of flask how we wrapped in functions to automatically create webhooks all that code's available you can go ahead and take a look at that on your own so the next piece right I said python docker in Chuck Nora so here's the docker bit that goes through and so let's take our code we're currently running it just live on our laptops that's great for testing but that sucks for packaging right packaging is all about how do we make them available and so still fairly modern we're still a fairly current way to do this because not everybody is fully adopted serverless architectures our microservices and containers and so let's look at how we would actually create a docker image to run our bot code and so if you go back to the command line and I will kill my non-working bot and touch docker file how many people have experience with docker you how many people have heard it but have never done anything with it before okay good so let's we'll take a look at how this works so just like before I have a cheat sheet for what the answer file is I'm going to go ahead and copy it now so we can look at it so dockerfile.sample so dockerfile.sample is the answer key and we're going I'm going to open it now and we'll talk about what's in there and how the docker file works so the way docker functions is that the whole point of a docker container or an LXC or Rocket or whatever container technology you're using is to take your code along with any dependencies your code needs and in Python our dependencies are things that you would pip install primarily we package them all up in a portable object that can then be deployed anywhere you want it and the way that you define and build a docker image which is the proper term for the template that you would then run containers for is using a docker file called dockerfile with a capital D you can call them something else but that adds undue complexity I wouldn't do it so dockerfile the way this goes it's a text file command structure base the first line is from like all good coders we steal from other people it's called borrowing or honoring those that came before and so I didn't want to build my own base container to run a Python application when other people came before me particularly the people at Python so the people at Python have have a base container a base image that I can build from they actually have a variety of them depending on what version of Python or what base operating system or what you're trying to do and so from Python 3.6-Lpine Lpine is a specific kind of flavor of Linux designed specifically for containers to make them super tiny Python 3.6 image all that image knows how to do is run Python 3.6 and then from that I build on top of it in layers so the first layer is maintainer I stamp it with my identity then I say expose port 5000 my code is going to run on port 5000 this container needs to expose it to the outside world then run a command make directory slash bot so inside of the container image I create a directory called my bot where I can put my code in my directory I add in the requirements file the Python requirements file and then I pip install them to bring my requirements in there by default the from image ran Python has pip for me they're at least nice enough to put that in there but it doesn't have anything else so I have to install my requirements with this line then I add my code my bot.py gets added in and then finally the command is how I instruct Docker container what do I want it to do what I want it to do is to run my code and so it runs the command python my bot.py the syntax here is kind of weird there are a couple of variations but this is I think currently the recommended way by Docker on how to do it is the way the command structure goes is their parameterize and it will take all of these and put a space between them so if I had to run Python my bot and then had a bunch of arguments I would just kind of string them out for that so this is our Docker file and it will go ahead and we are going to create an image based on that if you install Docker for whatever your platform is you can go ahead and follow along from here if you don't have Docker running successfully you can just watch that's cool too so I've got that piece in place what I want to do is actually I'm going backwards so having the Docker file are the instructions what I need to do is build an image based on those instructions and so what I use is the Docker build command dash t dash t is for tag I'm going to name my image on my laptop if you don't name it it will name it give you a unique identification those suck to work with so name them so my bot and then dot you actually have to end in dot dot represents the local directory and you are saying where to go find Docker file I'm running this from the same directory where the Docker file is if I was running this from someplace else I would do dot slash and then however just a full path on how to get there but just dot because my code in the Docker file is all right there I've run this before it went really quick for me if you're running it it may stall on step one the from because what it's doing is it's probably downloading the python 3.6 Elpine image from Docker hub as we talked about the Docker file we talked about each line along the way and I called those layers in here we can see the step indications for each of the layers as they go through so step one from our image step two create a new version of it labeled with the maintainer then expose it each step along the way is considered a new layer in the image and then at the final bottom it creates the full container that's there as you update code and rebuild your container what's one of the there's a lot of really good things about Docker from a container perspective but one of my favorites is it doesn't redo work it doesn't have to do there's a science and art behind designing your Docker files but in general the things that will change the most often you want to put latest because that is where it has to pick up rework so in this point I don't actually get to my code file till step eight of nine so if I don't change my requirements I don't update anything else I'm just updating the code every time I Docker build it can just rerun eight and nine eight and nine it's much faster on that side okay so I've built my container is anybody else building still still building or did it finish still building okay I'm going to go ahead and plug away it should finish pretty soon you still building too or is there a question from the Docker build command I'm not familiar with yeah it must be an issue okay so we've got that in place now the way that what we created was a Docker image a lot of people say you created a Docker container I'm usually not that pedantic about terminology but I think it's important to understand some of the bits of differences that are there a Docker image is a template if you're familiar with a VM world it's like a VM template and then you clone it to create a bunch of running VMs the Docker image is the template when you run an instance of it then you have a container as they go through on that so what we'll do now is we're going to run a Docker container based on our image we're going to do that with the Docker run command I am going to copy and paste the command into the spark room for the same reason we've done the other ones and we'll look at it as we run it we'll talk about what this command does to you so there's the command it's wrapping as you'll notice I'm using an end slash it's one long command with a bunch of parameters so let's talk about what they are as we go through so the question was where is the image stored? it's stored on your laptop all the Docker image really is is a file and references so when you do the Docker build on your laptop it's storing that image on your laptop if you wanted to make that image available up on Docker hub for other people to use there's Docker push and other methods for that I'm not covering that in the lab today I have no idea wherever Docker puts stuff and I don't have to know you probably don't either it's wherever in your file system so Docker run so the Docker run command that's up there it has a bunch of bits that are in there so Docker run dash dash name I want to run an instance of my container and call it my bot if I don't give it a name it will create one for me and those are actually kind of clever and fun to look at but in general it's easier just to give it your own these dash ease each indicate an environment variable so we had to create environment variables for the token the URL all of that information Docker needs is those as well and the way we do those is pass the way we set Docker environment variables inside of a running container is using the dash E on the Docker run and so what we're doing is we're creating inside of the Docker running container and a variable called spark bot URL and we're setting it equal to the same spark bot URL that's local in our terminal so we're just passing those into the Docker container for all four of those dash P is how we set a port so run the container if I want that container to serve something I need to map the port of the container to a port on the host that's this dash 5000 5000 dash D is for Damon it says run this container in the background rather than in the foreground and then finally what is the container image I want to run which is my bot it spits out a long identification that's the running container ID it's a unique container ID to indicate this container and she's running I have no idea where spark is working again but let's test it slash help didn't reply now the next step on this anyway was to look inside it so I have a running container on my machine when I ran the Python code like I could see the stack traces we can do the same thing with Docker by doing Docker logs my bot which is running so there's probably some outages on spark right so we'll pretend oh no it's still Docker logs you can follow them with dash f wow they are taking longer so it's really busted alright oh there they go spark apis I can't wait to dig into that there's all sorts of emails flowing around Cisco right now I guarantee it alright so we have our container running normally our bot would be responding back with witty chuck Norris jokes that I cross my finger every time that they're not too dirty which would result in an email to my boss and then a justification with HR I avoid that by having it busted so there's a plus sign so here's a couple of pieces so I just ran the Docker logs command that lets you get in and look at the standard out and standard error logs that are going through so Docker logs those for us if you're doing this in production there's all sorts of ways to kind of grab those pieces put them into a logging centralization system log slash a last assert all that stuff to visualize for development purposes just Docker logs let you look at them locally Docker PS right we've got the PS command in Linux just to look at the process is Docker PS will list you the different Docker containers that are currently running in their status and then Docker options for life cycle stop start and RM stop will do is clean of a stop of that running instance of the container as possible but it's still there you've probably all heard the purpose Docker containers are ephemeral you start one you delete it they go away they start up fresh but if it's true you do have the ability to start and stop a container and maintain it in its piece so if you just stop and start all of the logs everything that's there will stay the same if you Docker RM that deletes an instance of that container in which case if you then Docker run a new one now you have a new one on that so there's a little bit of kind of devil in the details when we talk about Docker containers so if I come over here and I Docker stop Docker stop will take a second because it's actually trying to stop it cleanly you can also Docker kill which is less clean but much faster and then if I do a Docker p s p s only by default shows running containers so I don't see any if I Docker p s dash a and then grep for my bot so I don't see all of the garbage history we'll see that it's there and it shows its current state is exited four minutes ago and I could Docker start to restart it up again if I wanted to start it back at its same state any questions on the Docker bit so we did run out a bit of time on here which is actually fine because spark is down anyway we should make this part of the mission really hard but if you wanted to go through and kind of practice later this week or someplace else you have the code you can always restart it it's your bot it's your account so this this lab doesn't end when when the piece is over you can always extend and use this technology but for some ideas one of the instructors Kenneth Lowe yesterday said new coders need ideas things to do so if you want to build on the skills that we talked about today and teach your bot to do something else anything with an API or that you can do in code is possible so some examples of things you may want to try I'm a big fan of having fun with technology especially when you're just getting started and learning so I've also built a cat fact bot because the internet is for nothing other than cats I think there's one that will tell cat facts which is also a good one to go through to practice another API see what's out there it's actually several that'll give you cat facts famous quotes weather details information about github repos there are APIs freely available for all of that information that you can replicate the same thing we did with Chuck Norris but for any of these other functionality some harder ideas build a hangman game right now you have to deal with state somebody starts a game it picks a word it remembers letters that go through great project to go through on here fun to do as well or a guessing game right 20 questions with a spark bot same type of idea lots of cool stuff you can do when you take this into your technology so when I deliver this with Cisco engineers or folks trying to do this I then go into how would you use this for real world I deal with a lot of network engineers right build a bot to go query the status of your firewalls build a bot to go query the health of your running systems and application the options are endless to go through on this and when you start building bots to do parts of your job to make it easier now we have chat ops and we've gotten even cooler than dev ops right cool alright so closing in review I'm still good right 20 20 after so closing review what did we do we learned what makes up a bot right so we learned the basic pieces doesn't matter if it's spark or slack or anything else we deployed a fully functional spark bot and it did work thank God that the spark cloud didn't crash like right at the beginning of the class so we all got them somewhat working we then packaged our bot as a Docker which still did work we were able to fire it up go through on that we looked at some of the basic Docker lifecycle commands we added a completely new feature which was the Chuck Norris pit you've got some ideas of other things that you can add from there as we go through so a couple of bits as we go through I'll stick around for some questions as we go oh what else did I have you can learn more right so if you go to developer dot Cisco spark dot com we were there already you can get documentation on the full apis learninglabs dot Cisco dot com has several labs about using spark building integrations using OAuth all of the other stuff you might want to do so you can get more information and then up on github under Cisco DevNet slash awesome Cisco spark are a ton of libraries things to make it easier all sorts of bits I will post these slides into the spark room that hopefully you're all a member of so that you can get access to those as well I'll also keep that spark room open probably indefinitely if you have other questions or have other bits and pieces to go through on there so feel free to use that if you want to get in touch with me that's the fastest I think I had my other contact info here yeah if you do have other questions if you're not an active spark user you can get me in other areas I am on Slack I use spark much more often so if you're looking for me spark or Twitter are easy ways to grab me you can follow me on github at slash H Preston I occasionally post something that's somewhat useful you can follow all the Cisco DevNet pieces that are here as well I will stick around as I'm packing up on that as I mentioned I have a few shirts up here if you register for a Cisco DevNet account send me a spark message or just tweet me showing it you're there we'll actually ship you your own because I only have like five of them up here of varying sizes so show me your API and I'll show you yours we got some cool there and because we all like stickers and development I've got a whole bunch of them here spark account some DevNet ones these are some of the new favorite ones API girl advertise those we've actually created a full suite of development superheroes led by our fearless captain cloud so I've got some decent swag that's up here as well that people can grab any final questions yes yeah the question was does spark host bots for you spark doesn't it's a platform you can host your bots wherever you want we have lots of bots hosted in AWS or Google there's a couple of projects if you go to Cisco pipeline dot IO it's kind of a skunk words project from one of the engineers at Cisco for a bot hosting platform but basically you can run it any place that's accessible on the internet we don't we decided that it didn't make sense for us to say run your bot here just run it anywhere you can't you want to question I will I will post the slides in the spark room so as soon as I'm done here I'll zip them up and just post them in the spark room okay alright the presentation will also be available on the pie Ohio website as well there okay yes other platforms other than Cisco spark I mean there's there's bot platforms out the yin yang these days if there's a messaging platform you can build a bot for it Facebook Messenger Slack obviously I'm drawing a blank on the messaging platforms right now but there's tons of them if there's a messaging platform you can probably build bots for it just pardon yeah so the advantage of the Cisco spark platform is Cisco spark was built like most of Cisco technology around the enterprise use case for collaboration and so integrating this with the rest of your collaboration technology voice video meeting services we've got full security baked in with key exchange so one of the big things the messaging platforms is I want to secure it but I want to own my own keys we support that rather than hosting keys for you we can do that too so there's a lot it was built from the design up to be secure and kind of targeting at the enterprises I probably have like one more question I'll keep taking questions but we'll have to turn off in just a minute so what's your question yep I will dump that I'll put all the URL and stuff information in that spark room you don't have to keep spark open forever but if you keep it open for the next couple of hours I'll dump some information in there so for some of those basics do we have what GIFI integration I don't know yes we probably I know people have written them for them if you go to spark depot or integrations there's tons of ones that are there yeah I'm sure there is I just there's been a ton all right we're starting to filter up on this thank you