<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Kissa Eric on Medium]]></title>
        <description><![CDATA[Stories by Kissa Eric on Medium]]></description>
        <link>https://medium.com/@eallan02?source=rss-1391b7967620------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*wXo25XBTuTZdWq5JA2S8AA.jpeg</url>
            <title>Stories by Kissa Eric on Medium</title>
            <link>https://medium.com/@eallan02?source=rss-1391b7967620------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 10:41:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@eallan02/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Building a Messenger Movie Recommendations Chatbot in 20 Minutes Or Less]]></title>
            <link>https://blog.chatbotslife.com/building-a-messenger-movie-recommendations-chatbot-in-20-minutes-or-less-d0f06ad06d4b?source=rss-1391b7967620------2</link>
            <guid isPermaLink="false">https://medium.com/p/d0f06ad06d4b</guid>
            <category><![CDATA[bots]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[chatbots]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[messenger-bots]]></category>
            <dc:creator><![CDATA[Kissa Eric]]></dc:creator>
            <pubDate>Sun, 31 Dec 2017 09:39:15 GMT</pubDate>
            <atom:updated>2018-01-03T22:45:35.618Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ia_bsKy04f2VJAAD05FODA.jpeg" /></figure><p>You can find the code to for this tutorial <a href="https://github.com/Eroo127/movieBot">here</a>.</p><p>Bots present a lot of possibilities for developers to create smart applications to interact with and offer services directly to users. <a href="https://chatbotslife.com/">Chatbots</a> in particular can be used as customer service agents, automated booking applications, to interact with users before handing off to human agents and so forth.</p><p><strong>INTRODUCTION</strong></p><p>For this tutorial, we will be building a simple <a href="https://chatbotslife.com/chatbots-the-rise-of-conversational-ui-8a59078e2f95">Facebook Messenger chatbot</a> to interact with the user and recommend movies based on the genre chosen. We will be using the <a href="https://github.com/Charca/bootbot">BootBot</a> framework and <a href="https://github.com/Charca/bootbot-cli">BootBot CLI</a> to handle the integration with Facebook’s Graph API, set up the webhooks and handle contexts.</p><p>BootBot is a very simple yet powerful framework for creating messenger chatbots and the CLI handles all the complexity of interacting with Facebook messenger, tunneling and setting up the development environment.</p><p>We will be using the <a href="https://www.themoviedb.org/">TMDb</a> API to get the movie recommendations that we will be passing into our chatbot.</p><h3>Top 3 Bot Tutorials</h3><blockquote><a href="https://chatbotslife.com/building-a-chatbot-with-watson-9f286bd6e3d1">1. Building a ChatBot with Watson</a></blockquote><blockquote><a href="https://chatbotslife.com/which-are-the-best-on-site-chatbot-frameworks-3dbf5157fb57">2. Which are the best chatbot frameworks?</a></blockquote><blockquote><a href="https://chatbotslife.com/chatbot-using-django-rest-framework-api-ai-slack-part-1-3-69c7e38b7b1e">3. Chatbot using django rest framework + api.ai + slack</a></blockquote><h3>1. Register on TMDb and get an API key</h3><p>We need to first and foremost sign up on <a href="https://www.themoviedb.org/"><strong>themoviedb</strong></a><strong> </strong>to gain authorization to use the API.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lQYsVkHXKqGcRzKuSOKWuw.png" /></figure><p>After signing up, log in to the site and locate the settings page by clicking on the letter icon button that should have the initial for your first name. This icon will be on the top right hand side of your screen and should activate a dropdown where you will find the “<strong>settings</strong>” link. While on the settings page, click on the “<strong>API</strong>” link on the left side menu and register for the API key that we will use later.</p><h3>2. Set up Facebook App</h3><p>For anyone who has built a messenger chatbot before, this part is pretty much the same. We are going to create the page that the user will interact with the chatbot through and then connect it to our Facebook app.</p><p><strong>Create a Facebook page</strong></p><p>To create a Facebook page, click on the arrow icon on your homepage top right corner and that will show the dropdown menu with the “create page” link. When you get to the create page section, you choose the appropriate category for your product and give it an awesome name.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PjLbQiMDGFtsld6s2A_fOg.png" /></figure><p><strong>Create a Facebook app</strong></p><p>In order to integrate our bot with our Facebook page, we will need to set up a Facebook app. This can be done by going to <a href="https://developers.facebook.com/apps">https://developers.facebook.com/apps</a> and clicking the <strong>+ Add New App</strong> button. This will launch a modal where you can give it any name you want.</p><p>Clicking the <strong>Create App ID</strong> button take you to the dashboard for your new app which should look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AL36IZVODBzgopDywBsYpA.png" /></figure><p>When we create our bot using the BootBot CLI, we will need two things i.e. the <strong>App Secret</strong> and the Facebook <strong>Page Access Token</strong>. The App Secret, as you can see, can be got by clicking the show button on the dashboard. You might be required to re-enter your Facebook password but after that your app secret will be revealed. Copy and save it somewhere safe.</p><p>To get the page token, we need to add messenger as a product. We do this by clicking the <strong>+Add Product</strong> button at the bottom of the sidebar and choosing messenger from the options. This will take you to the messenger app dashboard screen and scroll down to the token generation section. Click on the <strong>Select a Page</strong> dropdown and choose the page we created in the earlier. This will then result in the generation of a token that you should copy and paste where you saved the App Secret from earlier.</p><h3>3. Create the bot</h3><p>We now have our Facebook App Secret and Page Access Token, and are ready to create our bot using the BootBot Framework and BootBot CLI. I recommend the CLI because it helps save us a lot of time in setting up the development environment.</p><p><strong>Setup the bot</strong></p><p>To get started, install the CLI by typing the following command into your terminal.</p><pre>npm install bootbot-cli -g</pre><p>Then create the new project by running the “<strong>bootbot new”</strong> command like:</p><pre>bootbot new movieBot</pre><p>It will prompt you for the Facebook App Secret and Page Access Token that we saved earlier so just copy and paste them in where required.</p><p>You now have a bot and are ready to take the world by storm. However, you still need to enter your folder and install the dependencies in order to be able to use it.</p><pre>cd movieBot</pre><pre>npm install</pre><p>You can now start the bot. This will create a local tunnel to connect your chatbot to the internet without the need for an actual web server and it will give us our <strong>Webhook URL</strong> and <strong>Verify token</strong> that we will use to connect our local bot to the Facebook app we created earlier.</p><pre>bootbot start</pre><p><strong>Configure the bot to your Facebook App</strong></p><p>Using the<strong> Webhook URL</strong> and <strong>Verify token</strong> that we have just received from BootBot, we return to our Facebook app messenger product page.</p><p>Scroll down to the webhooks section and click the <strong>Setup Webhook</strong> button. This will launch a modal that will prompt you for the Webhook URL and Verify token as well as ask you to select the events you want your webhook to receive. I usually check all the boxes just to be on the safe side because I might want to add functionality to it later.</p><p>Click the A<strong>dd Webhook</strong> button to submit. We then subscribe to our bot by selecting the dropdown next to the new webhook, choosing the page we created earlier, and then clicking the <strong>subscribe</strong> button.</p><p>Everything is now ready and you can test it out by visiting your Facebook page and messaging your bot. It should be echoing everything you send.</p><h3>4. Create your movie recommendations system</h3><p>This is how our index.js file currently looks:</p><pre>&#39;use strict&#39;;</pre><pre>const BootBot = require(&#39;bootbot&#39;);</pre><pre>const config = require(&#39;config&#39;);</pre><pre>const bot = new BootBot({</pre><pre>accessToken: config.get(&#39;accessToken&#39;),</pre><pre>verifyToken: config.get(&#39;verifyToken&#39;),</pre><pre>appSecret: config.get(&#39;appSecret&#39;)</pre><pre>});</pre><pre>/**</pre><pre>* Demo handler to echo back whatever the user says.</pre><pre>* Feel free to delete this handler and start hacking!</pre><pre>*/</pre><pre>bot.on(&#39;message&#39;, (payload, chat) =&gt; {</pre><pre>const text = payload.message.text;</pre><pre>chat.say(`Echo: ${text}`);</pre><pre>});</pre><pre>bot.start(config.get(&#39;botPort&#39;));</pre><p>The BootBot and Config libraries have been required and we have a new bot that has all the details that we submitted into the CLI. There is also the “<strong>bot.on(‘message’)</strong>” event handler that is set up to echo the users text. Finally, we have the “<strong>bot.start()</strong>” call that starts the chatbot’s server.</p><p>Delete the whole “<strong>bot.on(‘message’)</strong>” event handler so that we can begin to add our own logic.</p><p><strong>Create Greeting Message</strong></p><p>When a user messages a page for the first time, they usually send out a “hello” or “Hi” greeting, so we need to create an event handler to listen for these greetings and respond with a message that details what the bot does and how they should interact with it.</p><pre>//welcome message triggered by user greeting</pre><pre>bot.hear([&#39;hi&#39;, &#39;hello&#39;, &#39;hey&#39;], (payload, chat) =&gt; {</pre><pre>chat.say(&#39;Hello, Type Genre then add the movie genre of the recommendation you want. &#39;);</pre><pre>});</pre><p>We use the “<strong>chat.say()</strong>” call to communicate our greeting message and prompt the user for a movie genre that we will use to get a recommendation.</p><p><strong>Add our API information and get the genreID’s</strong></p><p>We need to store the API details we got from TMDb into variables that we will use to make a call and fetch the movie recommendations.</p><pre>var apiKey=’&lt;&lt;Your Api Key&gt;&gt;’;</pre><p>We then pass our API key into our request.</p><pre>var movieRecommendation = ‘https://api.themoviedb.org/3/discover/movie?api_key=&#39; + apiKey + &#39;&amp;language=en-US&amp;sort_by=popularity.desc&amp;include_adult=false&amp;include_video=false&amp;page=1&amp;with_genres=&#39;;</pre><p>Take note of the “<strong>with_genres=</strong>” endpoint. This particular endpoint requires us to pass in a genreID in order to return the movies belonging to that particular genre. Therefore, we need to know the corresponding genreID’s for all the available genres. Sounds complicated right? But it really isn’t. TMDB has a “<strong>genre</strong>” API that lists for us all the genres and their corresponding genreID’s. Copy and paste the following link into your Postman and don’t forget to include your API Key where it is required. Make a get request and you should be given the list of all the genres and genreID’s in the TMDb database.</p><pre>https://api.themoviedb.org/3/genre/movie/list?api_key=&lt;&lt;api_key&gt;&gt;&amp;language=en-US</pre><p><strong>Adding our Movie recommendations logic</strong></p><p>We are going to use “<strong>node-fetch</strong>” to make our API request. Therefore, we have to start by importing the library into our bot.</p><pre>const fetch = require(‘node-fetch’);</pre><p>We now have all we need to create an event handler to receive the users requested genre, match it to a particular genreID, make the request, receive a movie and output it to the user. To do this, we have to listen for the message where the user specifies a genre and this requires a regular expression that listens for the text after a user has stated “<strong>genre</strong>” i.e. “<strong>genre comedy</strong>”. We then store the value “comedy” that we will match to a genreID. We convert the entire sting to lowercase in order to avoid errors.</p><pre>//Reg expression that listens for the genre</pre><pre>bot.hear(/genre (.*)/i, (payload, chat, data) =&gt; {<br></pre><pre>//Stringify the users response</pre><pre>const query =  String(data.match[1]);</pre><pre>//convert the response to lowercase to prevent request errors</pre><pre>const res = query.toLowerCase();</pre><pre>});</pre><p>We then run the string through a switch statement to check whether the user entered a valid genre and to match it to a particular genreID. I chose to use a switch statement because it is the easiest for beginners to understand, however, you can use object literals to achieve the same result.</p><pre>let id;<br></pre><pre>//switch statement to match the genre stated to the genre ID</pre><pre>switch(res) {</pre><pre>case &quot;comedy&quot;:</pre><pre>id = 35;</pre><pre>break;<br></pre><pre>case &quot;action&quot;:</pre><pre>id = 28;</pre><pre>break;<br></pre><pre>case &quot;adventure&quot;:</pre><pre>id = 12;</pre><pre>break;<br></pre><pre>case &quot;animation&quot;:</pre><pre>id = 16;</pre><pre>break;<br></pre><pre>case &quot;crime&quot;:</pre><pre>id = 80;</pre><pre>break;<br></pre><pre>case &quot;documentary&quot;:</pre><pre>id = 99;</pre><pre>break;<br></pre><pre>case &quot;drama&quot;:</pre><pre>id = 18;</pre><pre>break;<br></pre><pre>case &quot;family&quot;:</pre><pre>id = 10751;</pre><pre>break;<br></pre><pre>case &quot;fantasy&quot;:</pre><pre>id = 14;</pre><pre>break;<br></pre><pre>case &quot;history&quot;:</pre><pre>id = 36;</pre><pre>break;<br></pre><pre>case &quot;horror&quot;:</pre><pre>id = 27;</pre><pre>break;<br></pre><pre>case &quot;music&quot;:</pre><pre>id = 10402;</pre><pre>break;<br></pre><pre>case &quot;mystery&quot;:</pre><pre>id = 9648;</pre><pre>break;<br></pre><pre>case &quot;romance&quot;:</pre><pre>id = 10749;</pre><pre>break;<br></pre><pre>case &quot;science fiction&quot;:</pre><pre>id = 878;</pre><pre>break;<br></pre><pre>case &quot;tv movie&quot;:</pre><pre>id = 10770;</pre><pre>break;<br></pre><pre>case &quot;thriller&quot;:</pre><pre>id = 53;</pre><pre>break;<br></pre><pre>case &quot;war&quot;:</pre><pre>id = 10752;</pre><pre>break;<br></pre><pre>case &quot;western&quot;:</pre><pre>id = 37;</pre><pre>break;<br></pre><pre>default:</pre><pre>chat.say(&#39;That is not a genre.&#39;);</pre><pre>}</pre><p>Before we can make the API request, we have to check whether we were able to match the string to a genreID. We can do this using a simple if statement. Now, using node-fetch, we make our API request and pass the genre id into our “with_genres” parameter. We then store the response in an object and pass the “<strong>original_title</strong>”, “<strong>overview</strong>” and “<strong>poster_path</strong>” into our Facebook messenger’s generic template which is sent back to the user.</p><pre>//Conditional to check if the genre matched any existing genre ID</pre><pre>if(id!=undefined){</pre><pre>//make the API request and store the response.</pre><pre>fetch(movieRecommendation+id)</pre><pre>.then(res =&gt; res.json())</pre><pre>.then(json =&gt;{</pre><pre>//sends the movie title, overview and image in a messenger generic //template</pre><pre>chat.sendGenericTemplate([{</pre><pre>title: json.results[0].original_title,</pre><pre>subtitle: json.results[0].overview,</pre><pre>image_url:&#39;http://image.tmdb.org/t/p/w500&#39;+json.results[0].poster_path</pre><pre>}]);</pre><pre>});<br></pre><pre>//declares that the bot is searching for the recommendation<br>chat.say(&#39;searching for recommendations... &#39;);</pre><pre>console.log(id);</pre><pre>}</pre><p>We are now done with our basic movie recommendations chatbot and your code should now look like this:</p><pre>&#39;use strict&#39;;</pre><pre>const BootBot = require(&#39;bootbot&#39;);</pre><pre>const config = require(&#39;config&#39;);</pre><pre>const fetch = require(&#39;node-fetch&#39;);</pre><pre>var apiKey=&#39;Your Api Key&#39;; //TMDB api key</pre><pre>var movieRecommendation = &#39;https://api.themoviedb.org/3/discover/movie?api_key=&#39;+apiKey+&#39;&amp;language=en-US&amp;sort_by=popularity.desc&amp;include_adult=false&amp;include_video=false&amp;page=1&amp;with_genres=&#39;; //API request to get the recommendation</pre><pre>//New bot with our facebook app details</pre><pre>const bot = new BootBot({</pre><pre>accessToken: config.get(&#39;accessToken&#39;),</pre><pre>verifyToken: config.get(&#39;verifyToken&#39;),</pre><pre>appSecret: config.get(&#39;appSecret&#39;)</pre><pre>});</pre><pre>//welcome message triggered by user greeting</pre><pre>bot.hear([&#39;hi&#39;, &#39;hello&#39;, &#39;hey&#39;], (payload, chat) =&gt; {</pre><pre>chat.say(&#39;Hello, Type Genre then add the movie genre of the recommendation you want. &#39;);</pre><pre>});</pre><pre>//Reg expression that listens for the genre</pre><pre>bot.hear(/genre (.*)/i, (payload, chat, data) =&gt; {<br></pre><pre>//Stringify the users response</pre><pre>const query =  String(data.match[1]);</pre><pre>//convert the response to lowercase to prevent request errors</pre><pre>const res = query.toLowerCase();</pre><pre>let id;</pre><pre><br>//switch statement to match the genre stated to the genre ID</pre><pre>switch(res) {</pre><pre>case &quot;comedy&quot;:</pre><pre>id = 35;</pre><pre>break;</pre><pre>case &quot;action&quot;:</pre><pre>id = 28;</pre><pre>break;</pre><pre>case &quot;adventure&quot;:</pre><pre>id = 12;</pre><pre>break;</pre><pre>case &quot;animation&quot;:</pre><pre>id = 16;</pre><pre>break;</pre><pre>case &quot;crime&quot;:</pre><pre>id = 80;</pre><pre>break;</pre><pre>case &quot;documentary&quot;:</pre><pre>id = 99;</pre><pre>break;</pre><pre>case &quot;drama&quot;:</pre><pre>id = 18;</pre><pre>break;</pre><pre>case &quot;family&quot;:</pre><pre>id = 10751;</pre><pre>break;</pre><pre>case &quot;fantasy&quot;:</pre><pre>id = 14;</pre><pre>break;</pre><pre>case &quot;history&quot;:</pre><pre>id = 36;</pre><pre>break;</pre><pre>case &quot;horror&quot;:</pre><pre>id = 27;</pre><pre>break;</pre><pre>case &quot;music&quot;:</pre><pre>id = 10402;</pre><pre>break;</pre><pre>case &quot;mystery&quot;:</pre><pre>id = 9648;</pre><pre>break;</pre><pre>case &quot;romance&quot;:</pre><pre>id = 10749;</pre><pre>break;</pre><pre>case &quot;science fiction&quot;:</pre><pre>id = 878;</pre><pre>break;</pre><pre>case &quot;tv movie&quot;:</pre><pre>id = 10770;</pre><pre>break;</pre><pre>case &quot;thriller&quot;:</pre><pre>id = 53;</pre><pre>break;</pre><pre>case &quot;war&quot;:</pre><pre>id = 10752;</pre><pre>break;</pre><pre>case &quot;western&quot;:</pre><pre>id = 37;</pre><pre>break;</pre><pre>default:</pre><pre>chat.say(&#39;That is not a genre.&#39;);</pre><pre>}</pre><pre>//Conditional to check if the genre matched any existing genre ID</pre><pre>if(id!=undefined){</pre><pre>//make the API request and store the response.</pre><pre>fetch(movieRecommendation+id)</pre><pre>.then(res =&gt; res.json())</pre><pre>.then(json =&gt;{</pre><pre>//sends the movie title, overview and image in a facebook messenger //generic template</pre><pre>chat.sendGenericTemplate([{</pre><pre>title: json.results[0].original_title,</pre><pre>subtitle: json.results[0].overview,</pre><pre>image_url:&#39;http://image.tmdb.org/t/p/w500&#39;+json.results[0].poster_path</pre><pre>}]);</pre><pre>});<br></pre><pre>//declares that the bot is searching for the recommendation</pre><pre>chat.say(&#39;searching for recommendations... &#39;); </pre><pre>console.log(id);</pre><pre>}</pre><pre>});</pre><pre>//Start the bot server</pre><pre>bot.start(config.get(&#39;botPort&#39;));</pre><p><strong>Test the bot</strong></p><p>Go to your Facebook messenger and send a “hello”, “hi”, “hey” message to the page and the chatbot will respond with a greeting and prompt you for a genre of your choice. Type “genre ” followed by the genre of your choice e.g. comedy, action, fantasy and so forth and the chatbot should reply with a generic message with the movie poster, movie title and a bit of the overview.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/681/1*N5Czt4pCaW4MXRxsj9zb7Q.png" /></figure><h3>Conclusion</h3><p>Every once in a while I find unique tools and libraries that cut down on my development time and improve the quality of my final product. BootBot is one of these tools. It handles the time consuming integration issues thus allowing you time to focus more on the conversational logic of the chatbot.</p><p>From this very basic example, we have learnt how to setup the chatbot with the BootBot CLI, add our own event handlers and responses, make API calls, send messages using messengers generic message template and so forth. You can take this project further by using your imagination and the TMDb API to add more features to familiarize yourself with BootBot and the Messenger platform such as integrating lists, adding a movie plot summaries and so forth. I also made it simple so that you could learn more complex techniques by refractoring the code to improve it’s quality.</p><p>I hope you liked this article. Clap, Share or Comment if you found it useful or educational in any way.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2F3e59ef%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2F3e59ef&amp;image=http%3A%2F%2Fapi.screenshotlayer.com%2Fapi%2Fcapture%3Faccess_key%3Dfe59908dad3baab69ffab249a2224b03%26viewport%3D1024x612%26width%3D1000%26url%3Dhttps%253A%252F%252Fupscri.be%252F3e59ef%253Fscreenshot&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/7078d8ad19192c4c53d3bf199468e4ab/href">https://medium.com/media/7078d8ad19192c4c53d3bf199468e4ab/href</a></iframe><figure><a href="https://chatbotslife.com/bot-communities-mastermind-group-d2dae9876709#.53x0py6ou"><img alt="" src="https://cdn-images-1.medium.com/max/255/1*6XUspT9JOSq0w0Fi35HIaA.png" /></a></figure><figure><a href="https://m.me/ChatbotsLife"><img alt="" src="https://cdn-images-1.medium.com/max/255/1*c1LDMH5vbnIz9rmAka8Hwg.png" /></a></figure><figure><a href="https://chatbotslife.com/how-to-get-a-free-chatbot-b1fb9dfe109#.z9dtp2sy0"><img alt="" src="https://cdn-images-1.medium.com/max/255/1*D0Jf3dI6ZThtqcfwDYY7mg.png" /></a></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d0f06ad06d4b" width="1" height="1" alt=""><hr><p><a href="https://blog.chatbotslife.com/building-a-messenger-movie-recommendations-chatbot-in-20-minutes-or-less-d0f06ad06d4b">Building a Messenger Movie Recommendations Chatbot in 20 Minutes Or Less</a> was originally published in <a href="https://blog.chatbotslife.com/">Chatbots Life</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[INTEGRATING ANGULAR 4 AND ELECTRON IN 7 MINUTES OR LESS]]></title>
            <link>https://medium.com/@eallan02/integrating-angular-4-and-electron-in-7-minutes-or-less-667ccecf8c2?source=rss-1391b7967620------2</link>
            <guid isPermaLink="false">https://medium.com/p/667ccecf8c2</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[electron]]></category>
            <category><![CDATA[desktop-app]]></category>
            <category><![CDATA[angular-4]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Kissa Eric]]></dc:creator>
            <pubDate>Sun, 17 Dec 2017 20:15:11 GMT</pubDate>
            <atom:updated>2017-12-30T12:55:21.995Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/494/1*gqxdUGXls3PTOEC51mSsOQ.jpeg" /></figure><p>Find the code for this application <a href="https://github.com/Eroo127/angular-electron-starter">here</a>.</p><p>A lot of organisations have traded more traditional desktop applications for fancier web applications and this has been in part due to their ease of development, aesthetic and most importantly multi-platform capabilities. With web apps, a user isn’t restricted to a particular operating system like Windows or Mac because all he needs to do is fire up his browser, type in the URL and start working.</p><p>Electron has taken this dynamic and spun it on its head by engineering a framework for developing desktop applications that utilize the powerful web technologies most developers are familiar with, like JavaScript, HTML and CSS to build hybrid applications that can be packaged to run on Mac, Windows, Linux and any other operating systems from a single code base.</p><p>Angular is one of the most popular JavaScript/ Typescript frameworks in the world for building modular components that are used to build powerful, fast and secure single page web applications. Angular is my choice for this tutorial as it best exhibits the power of web technologies and can be seamlessly integrated with electron to develop amazing hybrid desktop applications.</p><h3><strong>Introduction</strong></h3><p>Today, we will be developing a simple boilerplate that integrates angular 4 and electron which you can use as your starter for any projects you might want to do in the future.</p><p>First and foremost, check to see that you are running the latest LTS version of Node on your computer. This is because older versions of node and npm seem to have a challenge with installing electron on windows 8 and 10. Run the following command to check your current node version.</p><pre>node –v</pre><p>Versions over 7.0.0 seem to be okay but the latest LTS version is most recommended. If you do not have node then you can download it at their site <a href="https://nodejs.org/en/">here</a>.</p><p>Next, if you do not have the Angular CLI, install it globally on your computer by running the following command in your terminal.</p><pre>npm install -g @angular/cli</pre><p>You are now ready to begin developing.</p><h3>1. Generate a new Angular App</h3><p>Type the following commands into your terminal to generate a new angular project.</p><pre>ng new electron-angular-starter</pre><p>Then, enter the angular project directory with the following command.</p><pre>cd electron-angular-starter</pre><h3>2. Install electron</h3><p>While inside the angular project directory, install electron and save it to your package.json file.</p><pre>npm install electron --save</pre><h3>3. Update the index.html file</h3><p>In the root folder of the newly generated electron-angular-starter project, open the index.html file and update the base href to avoid any interference with electron.</p><pre>&lt;base href = ’’./’’&gt;</pre><h3>4. Configure Electron</h3><p>While still in the root folder of your project, create a new file called main.js. This is the core file for your electron project and it defines how your desktop application will act and function.</p><p>Set up the “createWindow” function to determine the properties of the application window. This function controls features like size, color, fonts and so on.</p><p>When an angular app is built, the files are stored in a “dist” folder within your application root folder and, the index.html file in that folder is what we will be pointing to in your new main.js file. Try not to confuse it with the index.html file in your “src” folder. This will be the body of your electron desktop app.</p><pre>const electron = require(&#39;electron&#39;); //require electron</pre><pre>const { app, BrowserWindow } = electron;//import these</pre><pre>let window;</pre><pre>function createWindow () {</pre><pre>// Create the new browser window.</pre><pre>window = new BrowserWindow({</pre><pre>width: 800,</pre><pre>height: 700,</pre><pre>backgroundColor: &#39;#ffffff&#39;,</pre><pre>});</pre><pre>//set the URL to the angular build folder index.html file</pre><pre>window.loadURL(`file://${__dirname}/dist/index.html`)</pre><pre>// What happens when the window closes.</pre><pre>window.on(&#39;closed&#39;, function () {</pre><pre>window = null</pre><pre>});</pre><pre>}</pre><pre>// Create window when electron is initialized</pre><pre>app.on(&#39;ready&#39;, createWindow)</pre><pre>// Close process.</pre><pre>app.on(&#39;window-all-closed&#39;, function () {</pre><pre>if (process.platform !== &#39;win32&#39;) {</pre><pre>app.quit()</pre><pre>}</pre><pre>});</pre><p>Ensure to use the close process specific to your operating system</p><pre>(process.platform !== ‘win32’) //for windows<br>(process.platform !== ‘darwin’) // for macOS</pre><p>Update the package.json file to show the new main.js file. I usually put it before the “scripts” object.</p><pre>“main”: “main.js”</pre><h3>5. Build the angular app</h3><p>Building the angular application will create the “dist” folder that we mentioned in our main.js file. To do this, run the following command in your terminal:</p><pre>ng build</pre><p>You can test out the desktop app now to see if it works. Running the following command will open the electron desktop application window and you should be able to see the angular start page.</p><pre>electron .</pre><h3>6. Packaging the application</h3><p>We have now confirmed that our application runs and it is time to package it for the different operating systems. We are going to use the electron-packager for this application but you can explore and find other libraries that may be closer to what you want. In your terminal, run the following command to install it globally, and the one after to save it in your package.json file.</p><pre>npm install electron-packager –g</pre><pre>npm install electron-package --save</pre><p>To package for widows, we use “win32” as the platform whereas for Mac OS, we use Darwin. However, to package for Mac OS or Linux, you will need to install WineHQ.</p><pre>electron-packager . --platform=win32</pre><p>If this doesn’t work, the packager may need you to specify the electron version you are running. You can find this out by running the following command and adding it to the previous one.</p><pre>electron -v</pre><pre>electron-packager . --platform=win32 --electron-version= 1.7.9</pre><p>You now have a boilerplate angular and electron application that can be packaged for any Operating System.</p><p>I hope you liked this simple tutorial. Please clap, comment and share if you found this tutorial useful.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=667ccecf8c2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Breakdown of JavaScript Closures in Less Than 10 Minutes]]></title>
            <link>https://medium.com/@eallan02/a-breakdown-of-javascript-closures-in-15-minutes-75a317162927?source=rss-1391b7967620------2</link>
            <guid isPermaLink="false">https://medium.com/p/75a317162927</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[closure]]></category>
            <dc:creator><![CDATA[Kissa Eric]]></dc:creator>
            <pubDate>Wed, 13 Dec 2017 17:53:55 GMT</pubDate>
            <atom:updated>2017-12-30T12:29:16.653Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KSAFf9l55ssHOyxCWUcxqw.jpeg" /></figure><p>You can demo and view the code for the application <a href="https://codepen.io/erickissa/full/dJPJeO/">here</a>.</p><p>Any JavaScript developer who has worked on a big project or library and has collaborated with a large team has faced a problem with handling globals. Global variables are variables that are accessible by all functions across the application. A common bug faced by most developers when dealing with globals is when two or more functions end up modifying the same variable and as a result offer incorrect data. For example, we are building a simple food voting system to see whether people prefer pizza or burgers.</p><p>We first build a simple HTML web page called index.html using Bootstrap 4.</p><pre>&lt;!doctype html&gt;<br>&lt;html lang=&quot;en&quot;&gt;<br>&lt;head&gt;<br>  &lt;meta charset=&quot;utf-8&quot;&gt;<br>  &lt;title&gt;Javascript Closures&lt;/title&gt;</pre><pre>&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;<br>  &lt;link rel=&quot;stylesheet&quot; href=&quot;<a href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css">https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css</a>&quot; integrity=&quot;sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb&quot; crossorigin=&quot;anonymous&quot;&gt;<br>  <br>&lt;/head&gt;<br>&lt;body style = &quot;background-image: url(https://medium.com/&#39;%3Ca%20href=%22/service/https://scontent-sea1-1.cdninstagram.com/t51.2885-15/e35/11925897_718612938282417_1712675274_n.jpg?ig_cache_key=MTA4MzM4NDEwMDgzNzk0NjQ3Ng%3D%3D.2&#39;%22%3Ehttps://scontent-sea1-1.cdninstagram.com/t51.2885-15/e35/11925897_718612938282417_1712675274_n.jpg?ig_cache_key=MTA4MzM4NDEwMDgzNzk0NjQ3Ng%3D%3D.2&#39;%3C/a%3E);&quot;&gt;</pre><pre>&lt;nav class=&quot;navbar navbar-dark bg-dark justify-content-between&quot;&gt;<br>  &lt;a class=&quot;navbar-brand&quot; style=&quot;color:white;&quot;&gt;FOOD VOTE&lt;/a&gt;</pre><pre>&lt;/nav&gt;</pre><pre>&lt;div class = &quot;row text-center&quot; style=&quot;padding-top:150px&quot;&gt;</pre><pre>&lt;div class = &quot;col-md-10 offset-md-2 &quot;&gt;</pre><pre>&lt;div class = &quot;row&quot;&gt;<br>  <br>  &lt;div class = &quot;col-md-3 text-center&quot;&gt;<br>   &lt;h2 class = &quot;pizzaVote&quot;&gt;&lt;/h2&gt;<br>   &lt;img src=&quot;<a href="http://d1in1towyr636m.cloudfront.net/wp-content/uploads/2017/10/papa_johns_theworksslider-680x453.jpg">http://d1in1towyr636m.cloudfront.net/wp-content/uploads/2017/10/papa_johns_theworksslider-680x453.jpg</a>&quot;  class=&quot;img-thumbnail&quot;&gt;&lt;br&gt;<br>   &lt;button class=&quot;btn btn-primary votePizza&quot; &gt;VOTE&lt;/button&gt;</pre><pre>&lt;/div&gt;<br>  <br>  &lt;div class = &quot;col-md-2&quot;&gt;</pre><pre>&lt;h1 style=&quot;padding-top:10px&quot;&gt;VS&lt;/h1&gt;</pre><pre>&lt;/div&gt;<br>  <br>  &lt;div class = &quot;col-md-3&quot;&gt;<br>   &lt;h2 class = &quot;burgerVote&quot;&gt;&lt;/h2&gt;<br>   &lt;img src=&quot;<a href="http://www.marislurp.com/wp-content/uploads/2016/06/chicken-burger_1-680x453.jpg">http://www.marislurp.com/wp-content/uploads/2016/06/chicken-burger_1-680x453.jpg</a>&quot;  class=&quot;img-thumbnail&quot;&gt;&lt;br&gt;<br>   &lt;button class=&quot;btn btn-primary voteBurger&quot;&gt;VOTE&lt;/button&gt;<br>  &lt;/div&gt;</pre><pre>&lt;/div&gt;</pre><pre>&lt;/div&gt;</pre><pre>&lt;/div&gt;</pre><pre>&lt;script type=&quot;text/javascript&quot; src=&quot;closures.js&quot;&gt;&lt;/script&gt;<br>&lt;script src=&quot;<a href="https://code.jquery.com/jquery-3.2.1.slim.min.js">https://code.jquery.com/jquery-3.2.1.slim.min.js</a>&quot; integrity=&quot;sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;<br>&lt;script src=&quot;<a href="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js">https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js</a>&quot; integrity=&quot;sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;<br>&lt;script src=&quot;<a href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js">https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js</a>&quot; integrity=&quot;sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;</pre><pre>&lt;/body&gt;<br>&lt;/html&gt;</pre><p>We then create our external JavaScript file, let’s call it closures.js and add the ‘vote’ global variable that we will use as a counter for the votes. We also add the selectors to identify the HTML elements we are going to use and modify.</p><pre>var vote = 0;  //The counter variable for the voting functions</pre><pre>//The variables for voting and modifying the DOM<br>const pizzaVote = document.querySelector(&#39;h2.pizzaVote&#39;);<br>const burgerVote = document.querySelector(&#39;h2.burgerVote&#39;);<br>const voteBurger = document.querySelector(&#39;button.voteBurger&#39;);<br>const votePizza = document.querySelector(&#39;button.votePizza&#39;);</pre><p>After this, we set up our functions to count the pizza and burger votes and call them through the click event listeners we have added to the vote buttons.</p><pre>function voteForPizza(){    //Function using the global counter variable to increment the votes for a particular food.<br> vote +=1;<br> <br> return String(vote);<br>}</pre><pre>function voteForBurger(){<br> vote += 1;<br> <br> return String(vote);<br>}</pre><pre>if(votePizza){<br>votePizza.addEventListener(&#39;click&#39;, () =&gt; {     //Adding a click event listener to the voting buttons<br> <br> pizzaVote.textContent = voteForPizza(); //Modifying the DOM H2 tag to reflect the new votes.<br> <br>});<br>}</pre><pre>if(voteBurger){<br>voteBurger.addEventListener(&#39;click&#39;, () =&gt; {<br> <br> burgerVote.textContent = voteForBurger();<br> <br>});<br>}</pre><p>You can now run your webpage and when you try to vote, you will observe that we have run into a bug where the votes displayed add onto one another and are thus incorrect.</p><p>You may say that we could simply use two separate counter variables here like:</p><pre>var burgerVotes;<br>var pizzaVotes;</pre><p>This would work extremely well for this simple problem. However, you will find it challenging to implement the same on a project where you have to write hundreds or thousands of counter variables. The simple answer to this problem is using closures.</p><p>Closures are functions containing free variables and they can be written in JavaScript in the following way.</p><pre>function votes(){<br> var vote = 0; // create the free counter Variable</pre><pre>// This inner function now has access to the vote variable but //doesn&#39;t modify it when first called<br> return function countVotes(){ <br>  vote +=1;         // increments the votes<br>  return vote; <br> }<br> <br>}</pre><p>When using a closure, we leave the vote variable free by only modifying it in the inner function. This means that the vote variable remains at zero when the function is first called. We can now use the same closure function to process the votes for both types of food.</p><pre>var voteForPizza= votes(); //voting function to count pizza votes<br>var voteForBurger= votes(); //voting function to count burger votes</pre><p>We now no longer have the error and the correct number of votes for each food item is being returned.</p><p>We can now build an easily scalable application for our food voting system because, using our closure, we are able to add as many different food voting counters to our application and make a more fun interactive application for our users. For example:</p><pre>var voteForChickenWings = votes(); <br>var voteForFrenchFries = votes();<br>var voteForRibs = votes(); <br>var voteForSteak = votes();</pre><p>Now, by removing all the unnecessary code, our closures.js file will look like this.</p><pre>//The variables for voting and modifying the DOM<br>const pizzaVote = document.querySelector(&#39;h2.pizzaVote&#39;);<br>const burgerVote = document.querySelector(&#39;h2.burgerVote&#39;);<br>const voteBurger = document.querySelector(&#39;button.voteBurger&#39;);<br>const votePizza = document.querySelector(&#39;button.votePizza&#39;);</pre><pre>//The Closure<br>function votes(){<br> var vote = 0; // create the free counter Variable<br> <br> return function countVotes(){ <br>  vote +=1;         // increments the votes<br>  return vote; <br> }<br> <br>}</pre><pre>var voteForPizza = votes(); //voting function to count pizza votes<br>var voteForBurger = votes(); //voting function to count burger votes</pre><pre>if(votePizza){<br>votePizza.addEventListener(&#39;click&#39;, () =&gt; {     //Adding a click event listener to the voting buttons<br> <br> pizzaVote.textContent = voteForPizza(); //Modifying the DOM H2 tag to reflect the new votes.<br> <br>});<br>}</pre><pre>if(voteBurger){<br>voteBurger.addEventListener(&#39;click&#39;, () =&gt; {<br> <br> burgerVote.textContent = voteForBurger();<br> <br>});<br>}</pre><p><strong>CONCLUSION</strong></p><p>Closures are an important technique for modern application development in JavaScript because when accounting for third party usage like in library development, globals become unreliable and are more likely to cause breaks. Thus, knowledge of closures is pretty much a requirement when joining these teams.</p><p>I hope you have enjoyed this article. Clap, share or leave a comment if you found it useful or have any inquires.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=75a317162927" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>