{"id":6251,"date":"2020-08-06T03:31:39","date_gmt":"2020-08-06T03:31:39","guid":{"rendered":"https:\/\/internetmarketingoasis.com\/blog\/how-to-make-a-scalable-sms-chatbot-using-twilio-python-and-google-sheets-with-free-code\/"},"modified":"2020-08-06T03:31:58","modified_gmt":"2020-08-06T03:31:58","slug":"how-to-make-a-scalable-sms-chatbot-using-twilio-python-and-google-sheets-with-free-code","status":"publish","type":"post","link":"https:\/\/internetmarketingoasis.com\/blog\/how-to-make-a-scalable-sms-chatbot-using-twilio-python-and-google-sheets-with-free-code\/","title":{"rendered":"How to Make a Scalable SMS Chatbot Using Twilio, Python, and Google Sheets (with Free Code)"},"content":{"rendered":"<p> <br \/>\n<\/p>\n<div>\n<p>Many of us are serving to companies which might be going through arduous occasions, or we\u2019re going through arduous occasions ourselves. If you\u2019re working for a firm (or shopper) that is in bother, using SMS chatbots could possibly be a approach for you to look exterior your regular listing of options and assist them reach a utterly completely different approach. If you\u2019re a marketer on the lookout for work, including this to your listing of abilities may imply you retain issues ticking alongside whereas lots of the common doorways are closed \u2014 or that you simply open new doorways.<\/p>\n<h2>What you\u2019ll get<\/h2>\n<p>In this publish, I offer you directions and code to produce not only one, however a sequence of text-based chatbots that may be managed by Google Sheets. <\/p>\n<p>The instance right here is about up to work with eating places, however could possibly be tailored to work with any enterprise that wants to obtain orders, examine them in opposition to stock\/menus, and observe them down to be fulfilled. <\/p>\n<p>Once the system is about up, there might be no coding crucial to create a new SMS-based chatbot for a new enterprise. Plus, that enterprise might be ready to handle key particulars (like incoming orders and a menu) by merely updating a Google Sheet, making all of this much more accessible than most different choices.<\/p>\n<p>But first, some context.<\/p>\n<h2>Some context<br \/><\/h2>\n<p>In September 2017, as one in all my first large ardour tasks at Distilled, I wrote a Moz weblog publish telling folks how to make a chatbot and giving freely some instance code. <\/p>\n<p>This April, I bought an electronic mail from a man named Alexandre Silvestre. Alex had launched \u201ca non-profit effort to help the local small business owners navigate these challenging times, save as many jobs as possible, and continue to serve our community while helping to flatten the curve.\u201d <\/p>\n<p>This effort started\u00a0by specializing in eating places. Alex had discovered my 2017 publish (holy moly, content material advertising and marketing works!) and requested if I may assist his group construct a chatbot. We agreed on some primary necessities for the bot:<\/p>\n<ul>\n<li>It had to work solely inside textual content message (and if the order was tremendous sophisticated it had to have the option to arrange a name instantly with the restaurant).<\/li>\n<li>Running it had to be as shut to free as potential.<\/li>\n<li>Restaurants had to have the option to examine on orders, replace menus, and many others., with out organising particular accounts.<\/li>\n<\/ul>\n<p>The answer we agreed on had three components:<\/p>\n<ul>\n<li>Twilio (paid): provides the telephone quantity and handles a lot of the conversational back-and-forth.<\/li>\n<li>Google Cloud Functions (semi-free): when a URL is named it runs code (together with updating our database for the restaurant) and returns a response.<\/li>\n<li>Google Sheets (free): our database platform. We have a sheet which lists all the companies utilizing our chatbot, and linking off to the person Google Sheets for every enterprise.<\/li>\n<\/ul>\n<p>I\u2019ll take you thru every of those parts in flip and inform you how to work with them. <\/p>\n<p>If you\u2019re coming again to this publish, or simply need assistance with one space, be at liberty to leap to the precise half you\u2019re eager about:<\/p>\n<p>\u2014<a href=\"#pricing\" a=\"\">Pricing<br \/>&#13;<br \/>\n<\/a>\u2014Twilio<br \/>&#13;<br \/>\n\u2014Google Sheets<br \/>\u2014Google Cloud Functions<br \/>\u2014Test the bot<br \/>\u2014Break issues and have enjoyable<br \/>\u2014Postscript \u2014 bizarre hacks<\/p>\n<h2><a id=\"pricing\"\/>Pricing<\/h2>\n<p>This ought to all run fairly cheaply \u2014 I\u2019m speaking like 4 cents an order. <\/p>\n<p>Even so, all the time ensure that any pricing alerts are coming by means of to an electronic mail handle you actively monitor.<\/p>\n<p>When you are simply beginning on this, or if you\u2019ve made a change (like including new performance or new companies), be sure you examine again in in your credit over the following few weeks so you already know what\u2019s happening.<\/p>\n<h3>Twilio<\/h3>\n<p>Local Twilio telephone numbers price about $1.00 per thirty days. It\u2019ll price about $zero.0075 to ship and obtain texts, and Twilio Studio \u2014 which we use to do a lot of the \u201cconversation\u201d \u2014 prices $zero.01 each time it\u2019s activated (the primary 1,00zero each month are free).<\/p>\n<p>So, assuming you&#8217;ve got 2,500 textual content orders a month and every order takes about 5 textual content messages, it\u2019s coming to about $100 a month in complete.<\/p>\n<h3>Google Sheets<\/h3>\n<p>Google Sheets is free, and nice. Long stay Google Sheets.<\/p>\n<h3>Google Cloud Functions<\/h3>\n<p>Google shares full pricing particulars right here, however the vital issues to find out about are:<\/p>\n<p><strong>1. Promotional credit<\/strong><\/p>\n<p>You get a free trial which lasts up to a yr, and it contains $300 of promotional credit, so it\u2019ll spend that earlier than it spends your cash. We\u2019d spent $zero.00 (together with promotional credit) on the finish of a month of testing. That\u2019s as a result of there\u2019s additionally a month-to-month free allowance.<\/p>\n<p><strong>2. Free allowance and pricing construction<\/strong><\/p>\n<p>Even other than the free credit, Google provides a free allowance each month. If we assume that every order requires about 5 activations of our code and our code takes up to 5 seconds to run every time (which is a whereas however typically Google Sheets is sluggish), we could possibly be getting up to over 400,00zero orders per thirty days earlier than we dip into the promotional credit.<\/p>\n<h2><a id=\"twilio\"\/>Twilio<\/h2>\n<p>Twilio is a paid platform that permits you to purchase a telephone quantity and have that quantity\u00a0robotically ship sure responses primarily based on enter. <\/p>\n<p>If you don\u2019t need to learn extra about Twilio and simply need the free Twilio chatbot circulate, <strong>right here it&#8217;s<\/strong>.<\/p>\n<h3>Step 1: Buy a Twilio telephone quantity<\/h3>\n<p>Once you\u2019ve purchased a telephone quantity, you possibly can obtain texts to that quantity and they\u2019ll be processed in your Twilio account. You may ship texts from that quantity. <\/p>\n<h3>Step 2: Find your telephone quantity <\/h3>\n<p>You can see your listing of bought telephone numbers by clicking the Twilio menu within the prime left hand nook and then clicking \u201cPhone Numbers\u201d. Or, you possibly can simply go to phone-numbers\/incoming. <\/p>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"http:\/\/d1avok0lzls2w.cloudfront.net\/uploads\/blog\/5f28657ee06267-68623.jpg\" data-image=\"wqv8vrowg51m\"\/><\/figure>\n<p>Once you see your telephone quantity listed, make a observe of it. <\/p>\n<h3>Step three: Create your Studio Flow<\/h3>\n<p>Studio is Twilio\u2019s drag-and-drop editor that permits you to create the construction of your dialog. A studio \u201cflow\u201d is simply the title of a particular dialog you\u2019ve constructed.<\/p>\n<p>You can get to Twilio Studio by clicking on the Twilio menu once more and clicking on \u201cStudio\u201d beneath \u201cRuntime\u201d.<\/p>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"http:\/\/d1avok0lzls2w.cloudfront.net\/uploads\/blog\/5f28657ee06267-9228.png\" data-image=\"1udk2g8rgdok\"\/><\/figure>\n<p>Create a new circulate by clicking \u201cCreate a flow\u201d.<\/p>\n<p>When you create a new circulate, you\u2019ll be given the choice to begin from scratch or use one of many built-in choices to construct your circulate for you (though they gained\u2019t be as in-depth because the template I\u2019m sharing right here). <\/p>\n<p>If you need to use a model of the circulate which Alex and I constructed, choose \u201cImport from JSON\u201d and click on \u201cNext\u201d.  Then, obtain this file and copy the contents into the field that comes up. <\/p>\n<p>Make certain that it begins with a single  brace,\u00a0and ends with a single \u00a0brace. The field that comes up will robotically have  in it and should you don\u2019t delete them earlier than you paste, you\u2019ll double-up and it gained\u2019t settle for your enter.<\/p>\n<p>If all goes effectively, you\u2019ll be offered with a circulate that appears like this:<\/p>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"http:\/\/d1avok0lzls2w.cloudfront.net\/uploads\/blog\/5f28657fa5e045-166158.jpg\" data-image=\"3zk42xu4wmdq\"\/><\/figure>\n<p>You may be asking:\u00a0What within the title of all that&#8217;s holy is that tangle of coloured spaghetti?<\/p>\n<p>That\u2019s the Twilio Studio circulate we created and, don\u2019t fear, it principally splits up into a sequence of multiple-choice questions the place the reply to every determines the place you go subsequent within the circulate.<\/p>\n<p>Everything on the canvas that you could see is a widget from the Twilio Studio widget library linked along with \u201cif this, then that\u201d sort circumstances.<\/p>\n<h4>The Studio Flow\u00a0course of<\/h4>\n<p>Before we go into particular blocks within the course of, right here\u2019s an summary of what occurs:<\/p>\n<ol>\n<li>A buyer messages one in all our Twilio numbers<\/li>\n<li>Based on the precise quantity messaged, we glance up the restaurant related to it. We then use the title and saved menu of the restaurant to message the shopper. <\/li>\n<li>If the shopper tries to order off-menu, we join a name to the restaurant<\/li>\n<li>If the shopper chooses one thing from our menu, we ask their title, then report their order within the sheet for that restaurant and inform them when to arrive to decide up their order<\/li>\n<li>As\/when the consumer messages to inform us they&#8217;re exterior the restaurant, we ask whether or not they&#8217;re on-foot\/a description of their automobile. We report the automobile description in the identical restaurant sheet.<\/li>\n<\/ol>\n<p>Let\u2019s have a look at some instance constructing blocks lets?<\/p>\n<h5>Initial Trigger<\/h5>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/d2v4zi8pl64nxt.cloudfront.net\/sms-chatbot-free-code\/5f2865805de068.84592842.png\" width=\"624\" height=\"304\" data-image=\"o743w1l4dt9x\"\/><\/figure>\n<p>The preliminary set off seems proper initially of each circulate, and splits the incoming contact primarily based on whether or not it\u2019s a textual content message, a telephone name, or if code is accessing it. <\/p>\n<p>\u201cIncoming Message\u201d means the contact was through textual content message. We solely want to fear about that one for now, so let\u2019s deal with the left-hand line.<\/p>\n<h5>Record the truth that we\u2019re beginning a new interplay<\/h5>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/d2v4zi8pl64nxt.cloudfront.net\/sms-chatbot-free-code\/5f28658103de91.67702467.png\" width=\"624\" height=\"308\" data-image=\"6143lgrjy05u\"\/><\/figure>\n<p>Next, we use a \u201cSet Variables\u201d block, which you&#8217;ll seize from the widget library.<\/p>\n<p>The \u201cSet Variables\u201d block lets us save report info that we would like to refer to later. For instance, we begin by simply setting the \u201cstage\u201d of our interplay. We say that the stage is \u201cstart\u201d as in, we&#8217;re initially of the interplay. Later on we\u2019ll examine what the worth of stage is, each in Studio and in our exterior code, in order that we all know what to do, when.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/d2v4zi8pl64nxt.cloudfront.net\/sms-chatbot-free-code\/5f286581c963d9.61528866.png\" width=\"436\" height=\"451\" data-image=\"59pu2ntedjhz\"\/><\/figure>\n<h5>Get our menu<\/h5>\n<p>We assume that if somebody messaged us, triggering the chatbot, they&#8217;re trying to order so the following stage is to work out what the relevant menu is. <\/p>\n<p>Now, we may simply write the menu out instantly into Studio and say that every time somebody sends us a message, we reply with the identical listing of choices. But that has a couple issues.<\/p>\n<p>First, it could imply that if we would like to set this up for a number of eating places, we\u2019d have to create a new circulate for every.\u00a0<\/p>\n<p>The larger concern is that eating places usually change their menus. If we would like this to be one thing we will provide to a number of completely different eating places, we don\u2019t need to spend all our time manually updating Twilio each time a restaurant runs out of an ingredient.<\/p>\n<p>So what we actually want is for the eating places to have the option to listing their very own menus. This is the place Google Sheets is available in, however we\u2019ll get to that later. In Twilio, we simply want to have the option to ask for exterior info and ahead that exterior info to the consumer. To do this we use a Webhook widget:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/d2v4zi8pl64nxt.cloudfront.net\/sms-chatbot-free-code\/5f28658253e999.92379539.png\" width=\"624\" height=\"319\" data-image=\"oa41hi5fz849\"\/><\/figure>\n<p>This widget makes a request to a URL, will get the response, and then lets us use the content material of the response in our messages and circulate. <\/p>\n<p>If the request to the URL is profitable, Twilio will robotically proceed to our success step, in any other case we will set it to ship an \u201cOops, something went wrong\u201d response with the Fail possibility.<\/p>\n<p>In this case, our Webhook will make a request to the Google Cloud capabilities URL (extra on that later). The request we ship will embrace some details about the consumer and what we want the code to do. The info might be in JSON format (the identical format that we used to import the Twilio circulate I shared above).<\/p>\n<p>Our JSON will embrace the precise Twilio telephone quantity that is been messaged, and we\u2019ll use that quantity to differentiate between eating places, in addition to the telephone quantity that contacted us. It\u2019ll additionally embrace the content material of the textual content message we obtained and the \u201cstage\u201d we set earlier, so the code is aware of what the consumer is on the lookout for.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/d2v4zi8pl64nxt.cloudfront.net\/sms-chatbot-free-code\/5f286582e6d0c0.15470116.png\" width=\"311\" height=\"714\" data-image=\"268o86qfa6u8\"\/><\/figure>\n<p>Then the code will do some stuff (we\u2019ll get to that later) and return info of its personal. We can then inform Twilio to use components of the response in messages.<\/p>\n<h5>Send a message in response<\/h5>\n<p>Next we will use the knowledge we obtained to assemble and ship a message to the consumer. Twilio will keep in mind the quantity you\u2019re in a dialog with and it\u2019ll ship your messages to that quantity.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/d2v4zi8pl64nxt.cloudfront.net\/sms-chatbot-free-code\/5f2865837a95a1.59446647.png\" width=\"624\" height=\"323\" data-image=\"bcbdye5drzlx\"\/><\/figure>\n<p>This is the &#8220;Send &amp; Wait For Reply&#8221; widget, that means that after this message is shipped, Twilio will assume the dialog remains to be going quite than ending it there.<\/p>\n<p>In this case, we\u2019re writing our welcome message. We may write out simply plain content material, however we would like to use a number of the\u00a0variables we bought from our Webhook widget. We referred to as that particular Webhook widget \u201cget_options\u201d, so we entry the content material we bought from it by writing:<\/p>\n<p>{{widgets.get_options<\/p>\n<p>The response comes again in JSON, and fortuitously Twilio robotically breaks that up for us.\u00a0<\/p>\n<p>We can entry particular person components of the response by writing &#8220;parsed&#8221; and then the label we gave that info in our response. As it&#8217;s, the response from the code seemed one thing like this:<\/p>\n<p>\u201cname\u201d: restaurant_name,<\/p>\n<p>\u201cdishes_string\u201d: &#8220;You can select from Margherita Pizza, Hawaiian Pizza, Vegetarian Pizza\u201d<\/p>\n<p>\u201cadditions\u201d: \u201clarge, medium, small\u201d<\/p>\n<p>We get the out there menu by writing &#8220;&#8221;, and then we write the message under which might be despatched to individuals who contact the bot:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/d2v4zi8pl64nxt.cloudfront.net\/sms-chatbot-free-code\/5f286584231070.12068786.png\" width=\"624\" height=\"407\" data-image=\"dplt17ijxsc4\"\/><\/figure>\n<h5>Make a resolution primarily based on a message<\/h5>\n<p>We can\u2019t assume everybody goes to use the bot in precisely the identical approach so we want to have the option to change what we do primarily based on sure circumstances. The &#8220;Split Based On\u2026&#8221; widget is how we choose sure circumstances and set what to do if they&#8217;re met.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/d2v4zi8pl64nxt.cloudfront.net\/sms-chatbot-free-code\/5f286584c9e565.37848347.png\" width=\"624\" height=\"232\" data-image=\"m8h9vro2fm20\"\/><\/figure>\n<p>In this case, we use the content material of the response to our earlier message which we entry utilizing options_follow_up.inbound.Body. \u201cOptions_follow_up\u201d is the title of the Send &amp; Wait widget we simply spoke about, \u201cinbound\u201d means the response and, \u201cBody\u201d means the textual content inside it.<\/p>\n<p>Then we set a situation. If the consumer responds with something alongside the traces of \u201cother\u201d, \u201cno\u201d, \u201chelp\u201d, and many others., they\u2019ll get despatched off on one other observe to have a telephone name. If they reply with something not on that listing, they may be making an attempt to order, so we take their order and examine it with our code:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/d2v4zi8pl64nxt.cloudfront.net\/sms-chatbot-free-code\/5f2865858b1707.17057050.png\" width=\"624\" height=\"728\" data-image=\"u2edqe4wxc2j\"\/><\/figure>\n<h5>Set up a name<\/h5>\n<p>If the consumer says they need one thing off-menu, we\u2019ll want to arrange a name with the restaurant. We do this by first calling the consumer:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/d2v4zi8pl64nxt.cloudfront.net\/sms-chatbot-free-code\/5f286586528758.82167270.png\" width=\"611\" height=\"334\" data-image=\"she8h07zcq3k\"\/><\/figure>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/d2v4zi8pl64nxt.cloudfront.net\/sms-chatbot-free-code\/5f28658711bb51.59889976.png\" width=\"624\" height=\"180\" data-image=\"106v88yqdk9t\"\/><\/figure>\n<p>Then, once they decide up, connecting that decision to the restaurant quantity which we\u2019ve already seemed up in our sheets:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/d2v4zi8pl64nxt.cloudfront.net\/sms-chatbot-free-code\/5f2865879b3cb5.85482516.png\" width=\"624\" height=\"333\" data-image=\"p9yjod38xkn5\"\/><\/figure>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/d2v4zi8pl64nxt.cloudfront.net\/sms-chatbot-free-code\/5f28658859b2d2.96616536.png\" width=\"624\" height=\"140\" data-image=\"2i7umg35bzz1\"\/><\/figure>\n<h3>Step four: Select your studio circulate for this telephone quantity<\/h3>\n<p>Follow the directions in step two to get again to the precise itemizing for the telephone quantity you acquire. Then scroll to the underside and choose the Studio Flow you created.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/d2v4zi8pl64nxt.cloudfront.net\/sms-chatbot-free-code\/5f286588de8570.32885994.png\" width=\"624\" height=\"79\" data-image=\"63erh9h19o57\"\/><\/figure>\n<h2><a id=\"googlesheets\"\/>Google Sheets<\/h2>\n<p>This chatbot makes use of two Google Sheets. <\/p>\n<h3>Free lookup sheet<\/h3>\n<p>The lookup sheet holds a listing of Twilio telephone numbers, the restaurant they&#8217;ve been assigned to, and the URL of the Google Sheet which holds the small print for that restaurant, in order that we all know the place to search for every.<\/p>\n<p>You\u2019ll want to create a copy of the sheet to use it. I\u2019ve included a row within the sheet I shared, explaining every of the columns. Feel free to delete that when you already know what you\u2019re doing.<\/p>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"http:\/\/d1avok0lzls2w.cloudfront.net\/uploads\/blog\/screen-shot-2020-08-04-at-1-53792.jpg\" data-image=\"utmixqdp8bee\"\/><\/figure>\n<h3>Free instance restaurant sheet<\/h3>\n<p>The restaurant-specific sheet is the place we embrace all of our details about the restaurant in a sequence of tabs. You\u2019ll want to create a copy of the sheet to use it.\u00a0<\/p>\n<h4>Orders<\/h4>\n<p>The orders tab is especially utilized by our code. It will robotically write within the order time, buyer title, buyer telephone quantity, and particulars of the order. By default it\u2019ll write FALSE within the \u201cPAID\/READY?\u201d column, which the restaurant will then want to replace. <\/p>\n<p>In the ultimate stage, the script will add TRUE to the \u201cCUSTOMER HERE?\u201d column and give the automotive description within the \u201cPICK UP INFO\u201d column.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/d2v4zi8pl64nxt.cloudfront.net\/sms-chatbot-free-code\/5f28658a23ffb2.57184708.png\" width=\"624\" height=\"213\" data-image=\"5l29m09206hr\"\/><\/figure>\n<h4>Wait time<\/h4>\n<p>This is a pretty easy tab, because it accommodates one cell the place the restaurant writes in how lengthy it\u2019ll be earlier than orders are prepared. Our code will extract that and give it to Twilio to let prospects know the way lengthy they&#8217;re going to probably be ready.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/d2v4zi8pl64nxt.cloudfront.net\/sms-chatbot-free-code\/5f28658ae01108.79987678.png\" width=\"236\" height=\"79\" data-image=\"vhqiipmch54c\"\/><\/figure>\n<h4>Available dishes and additions tabs<\/h4>\n<p>The restaurant lists the dishes which might be\u00a0out there now together with easy variations to these dishes, then these menus are despatched to prospects once they contact the restaurant. When the code receives an order, it\u2019ll additionally examine that order in opposition to the listing of dishes it despatched to see if the shopper is choosing one of many selections.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/d2v4zi8pl64nxt.cloudfront.net\/sms-chatbot-free-code\/5f28658b8d6c85.52430980.png\" width=\"624\" height=\"281\" data-image=\"1dhx4d28xm13\"\/><\/figure>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/d2v4zi8pl64nxt.cloudfront.net\/sms-chatbot-free-code\/5f28658c33be49.78717800.png\" width=\"624\" height=\"293\" data-image=\"tpo94tzj0xu6\"\/><\/figure>\n<h4>Script utilizing sheet tab<\/h4>\n<p>You don\u2019t want to contact this one in any respect \u2014 it\u2019s simply a precaution to keep away from our code by accident overwriting itself.<\/p>\n<p>Imagine a scenario the place our code will get an order, finds the primary empty row within the orders sheet, and writes that order down there. However, on the similar time, another person makes an order for a similar restaurant, one other occasion of our code additionally seems to be for the primary empty row, selects the identical one, and they each write in it on the similar time. We\u2019d lose at the very least one order although the code thinks all the pieces is okay.<\/p>\n<p>To attempt to keep away from that, when our code begins to use the sheet, the very first thing it does is change the \u201cScript using sheet\u201d worth to TRUE and writes down when it begins utilizing it. Then, when it\u2019s accomplished, it adjustments the worth again to FALSE.<\/p>\n<p>If our script goes to use the sheet and sees that \u201cScript using sheet\u201d is about to TRUE, it&#8217;s going to wait till that worth turns into FALSE and then write down the order.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/d2v4zi8pl64nxt.cloudfront.net\/sms-chatbot-free-code\/5f28658cd1e277.89314716.png\" width=\"563\" height=\"107\" data-image=\"ypuq5b8l2qjq\"\/><\/figure>\n<h3>How do I take advantage of the sheets?<\/h3>\n<p><strong>Example restaurant sheet<\/strong>:<\/p>\n<ol>\n<li>Make a copy of the instance restaurant sheet. <\/li>\n<li>Fill out all the small print to your check restaurant. <\/li>\n<li>Copy the URL of the sheet.<\/li>\n<\/ol>\n<p><strong>Lookup sheet<\/strong>:<\/p>\n<ol>\n<li>Make a copy of the lookup sheet (you\u2019ll solely want to create one).  <\/li>\n<li>Don\u2019t delete something within the \u201cextracted id\u201d column however exchange all the pieces else.<\/li>\n<li>Put your Twilio quantity within the first column.<\/li>\n<li>Paste the URL of your check restaurant within the Business Sheet URL column.<\/li>\n<li>Add your small business\u2019 telephone quantity within the last column.<\/li>\n<\/ol>\n<p><strong>Sharing<\/strong>:<\/p>\n<ol>\n<li>Find the \u201cService Account\u201d electronic mail handle (which I\u2019ll direct you to within the Cloud Functions part).<\/li>\n<li>Make certain that each sheets are shared with that electronic mail handle having edit entry.<\/li>\n<\/ol>\n<p><strong>Creating a new restaurant<\/strong>:<\/p>\n<ol>\n<li>Any time you want to create a new restaurant, simply make a copy of the restaurant sheet.<\/li>\n<li>Make certain you tick \u201cshare with the same people\u201d if you\u2019re copying it.<\/li>\n<li>Clear out the present particulars.<\/li>\n<li>Paste the brand new Google Sheet URL in a new line of your lookup sheet.<\/li>\n<\/ol>\n<p>When the code runs, it\u2019ll open up the lookup sheet, use the Twilio telephone quantity to discover the precise sheet ID for that restaurant, go to that sheet, and return the menu.<\/p>\n<h2><a id=\"googlecloudfunctions\"\/>Google Cloud Functions<\/h2>\n<p>Google Cloud Functions is a easy approach to robotically run code on-line with out having to arrange servers or set up a entire bunch of particular packages someplace to ensure your code is transferable.<\/p>\n<p>If you don\u2019t need to be taught extra about Google Cloud and simply need code to run \u2014 <strong>right here\u2019s the free chatbot Python code<\/strong>.<\/p>\n<h3>What is the code doing?<\/h3>\n<p>Our code doesn\u2019t attempt to deal with any of the particular conversations, it simply will get requests from Twilio \u2014 together with particulars in regards to the consumer and what stage they&#8217;re at \u2014 and performs some easy capabilities.<\/p>\n<h4>Stage 1: \u201cStart\u201d<\/h4>\n<p>The code receives a message from Twilio together with the Twilio quantity that was activated and the stage the consumer is at (begin). Based on it being the \u201cstart\u201d stage, the code prompts the beginning operate. <\/p>\n<p>It seems to be up the precise restaurant sheet primarily based on the Twilio quantity, then returns the menu for that restaurant.<\/p>\n<p>It additionally sends Twilio issues like the precise restaurant\u2019s quantity and a condensed model of the menu and additions for us to examine orders in opposition to.<\/p>\n<h4>Stage 2: \u201cChosen\u201d<\/h4>\n<p>The code receives the stage the consumer is at (chosen) in addition to their order message, the sheet ID for the restaurant, and the condensed menu (which it despatched to Twilio earlier than), so we don\u2019t have to look these issues up once more.<\/p>\n<p>Based on it being the \u201cchosen\u201d stage, the code prompts the chosen operate. It checks if the order matches our condensed menu. If they did not, it tells Twilio that the message doesn\u2019t appear like an order.\u00a0<\/p>\n<p>If the order does match our menu, it writes the order down within the first clean line. It additionally creates an order ID, which is a mixture of the time and a portion of the consumer\u2019s telephone quantity.<\/p>\n<p>It sends Twilio a message again saying if the order matched our menu and, if it did match our menu, what the order quantity is.<\/p>\n<h4>Stage three: \u201cArrived\u201d<\/h4>\n<p>The code receives the stage the consumer is at (arrived) and prompts the arrived operate. It additionally receives the message describing the consumer\u2019s automobile, the restaurant-specific sheet ID, and the order quantity, all of which it beforehand instructed Twilio.<\/p>\n<p>It seems to be up the restaurant sheet, and finds the order ID that matches the one it was despatched, then updates that row to present the consumer has arrived and the outline of their automotive.<\/p>\n<h4>Twilio handles all of the context<\/h4>\n<p>It might sound bizarre to you that each time the code finds some info (as an illustration, the sheet ID to lookup) it sends that info to Twilio and requests it afresh in a while. That\u2019s as a result of our code doesn\u2019t know what\u2019s happening in any respect, apart from what Twilio tells it. Every time we activate our code, it begins precisely the identical approach so it has no approach of understanding which consumer is texting Twilio, what stage they&#8217;re at, and even what restaurant we\u2019re speaking about.<\/p>\n<p>Twilio remembers this stuff for the course of the interplay, so we use it to deal with all of that stuff. Our code is a quite simple \u201cdo-er\u201d \u2014 it doesn\u2019t \u201cknow\u201d something for greater than about 5 seconds at a time.<\/p>\n<h3>How do I arrange the code?<\/h3>\n<p>I don\u2019t have time to describe how to use Google Cloud Functions in-depth, or how to code in Python, however the code I\u2019ve shared above contains a honest variety of notes explaining what\u2019s happening, and I\u2019ll speak you thru the steps particular to this course of.<\/p>\n<h4>Step 1: Set up<\/h4>\n<p>Make certain you:<\/p>\n<h4>Step 2: Create a new operate<\/h4>\n<p>Go right here and click on \u201ccreate a new function\u201d. If you haven\u2019t created a challenge earlier than, you may want to do this first, and you may give the challenge no matter title you want.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/d2v4zi8pl64nxt.cloudfront.net\/sms-chatbot-free-code\/5f28658d8f3472.55862613.png\" width=\"624\" height=\"93\" data-image=\"lnq66adnrpx5\"\/><\/figure>\n<h4>Step three: Set out the small print to your operate<\/h4>\n<p>The display shot under provides you a lot of the small print you want. I\u2019d suggest you select 256MB for reminiscence \u2014 it ought to be sufficient. If you discover you run into issues (or if you need to be extra cautious from the beginning), then enhance it to 512MB.<\/p>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"http:\/\/d1avok0lzls2w.cloudfront.net\/uploads\/blog\/5f286590ef3968-139109.jpg\" data-image=\"1v8bdp5sukw7\"\/><\/figure>\n<p>Make certain you choose HTTP because the set off and observe down the URL it provides you (should you overlook, you possibly can all the time discover the URL by going to the \u201cTrigger\u201d tab of the operate).<\/p>\n<p>Also be sure you tick the choice\u00a0to enable Unauthenticated Access (that approach Twilio might be ready to begin the operate).<\/p>\n<p>Select &#8220;Inline editor&#8221; and paste within the Gist code I gave you (it\u2019s closely commented, I like to recommend giving it a learn to be sure you\u2019re proud of what it\u2019s doing).<\/p>\n<p>Click \u201cREQUIREMENTS.TXT\u201d and paste within the following traces of libraries you\u2019ll want to use:<\/p>\n<p>Make certain \u201cfunction to execute\u201d is SMS,\u00a0then click on the \u201cEnvironment Variables\u201d dropdown.<\/p>\n<p>Just like I\u2019ve accomplished above, click on \u201c+ ADD VARIABLE\u201d, write &#8220;spreadsheet_id&#8221; within the &#8220;Name&#8221; column, and within the &#8220;Value&#8221; column, paste within the ID of your lookup sheet. You get the ID by trying on the URL of the lookup sheet, and copying all the pieces between the final two slashes (outlined in crimson under).<\/p>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"http:\/\/d1avok0lzls2w.cloudfront.net\/uploads\/blog\/5f28658f053451-12973.jpg\" data-image=\"p82m9ukv2qbz\"\/><\/figure>\n<p>Click on the \u201cService account\u201d drop down. It\u00a0ought to give you simply \u201cApp Engine default service account\u201d and offer you an electronic mail handle (as under) \u2014 that is the e-mail handle you want all your Google Sheets to be shared with. Write it down someplace and add it as an edit consumer for each your lookup and restaurant-specific sheets.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/d2v4zi8pl64nxt.cloudfront.net\/sms-chatbot-free-code\/5f28658f9dd235.88532110.png\" width=\"624\" height=\"93\" data-image=\"18tenxjsrpve\"\/><\/figure>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/d2v4zi8pl64nxt.cloudfront.net\/sms-chatbot-free-code\/5f286590635b46.13167057.png\" width=\"624\" height=\"195\" data-image=\"3prn38qvnmiy\"\/><\/figure>\n<p>Once you\u2019ve accomplished all of that, click on \u201cDeploy\u201d.<\/p>\n<p>Once you deploy, it is best to land again on the primary display to your Cloud Function. The inexperienced tick within the prime left hand nook tells you all the pieces is working.<\/p>\n<h4>Step four: Turn on Sheets API<\/h4>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"http:\/\/d1avok0lzls2w.cloudfront.net\/uploads\/blog\/5f28659282fda7-59952.jpg\" data-image=\"qv4m5mjceax7\"\/><\/figure>\n<p>The first time your code tries to entry Google Sheets, it won&#8217;t have the option to since you want to swap on the Google Sheets API to your account. Go right here, choose the challenge you\u2019re engaged on with the dropdown menu within the prime left nook, then click on the large blue &#8220;ENABLE&#8221; button. <\/p>\n<h4>Step 5: Go again to Twilio and paste within the HTTP set off to your code<\/h4>\n<p>Remember the set off URL we famous down from once we had been creating our operate? Go again to your Twilio Studio and discover all the blocks with the &lt;\/&gt; signal within the prime left nook:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/d2v4zi8pl64nxt.cloudfront.net\/sms-chatbot-free-code\/5f2865931a22f6.74509200.png\" width=\"624\" height=\"324\" data-image=\"64yz3jkywm8b\"\/><\/figure>\n<p>Click on every in flip and paste your Google Cloud URL into the REQUEST URL field that comes up on the proper aspect of the display:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/d2v4zi8pl64nxt.cloudfront.net\/sms-chatbot-free-code\/5f286593b38e50.42446384.png\" width=\"624\" height=\"99\" data-image=\"o2tdtaaqm4nj\"\/><\/figure>\n<h2><a id=\"testbot\"\/>Test the bot<\/h2>\n<p>By now it is best to have your Cloud Function arrange. You must also have each of your Google Sheets arrange and shared along with your Cloud Function service account.<\/p>\n<p>The subsequent step is to check the bot. Start by texting your Twilio quantity the phrase \u201corder\u201d to get it going. It ought to reply with a menu that your code pulls out of your restaurant-specific Google Sheet. Follow the steps it sends you thru to the tip and examine your Google Sheet to ensure it\u2019s updating correctly.<\/p>\n<p>If for some cause it\u2019s not working, there are two locations you possibly can examine. Twilio retains a log of all of the errors it sees which you could find by clicking the little \u201cDebugger\u201d image within the prime proper nook:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/d2v4zi8pl64nxt.cloudfront.net\/sms-chatbot-free-code\/5f2865945483a4.95845406.png\" width=\"524\" height=\"163\" data-image=\"hkbdqtfwkjxc\"\/><\/figure>\n<p>Google additionally retains a report of all the pieces that occurs along with your Cloud Function. This contains non-error notifications. You can see all of that by clicking &#8220;VIEW LOGS&#8221; on the prime:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/d2v4zi8pl64nxt.cloudfront.net\/sms-chatbot-free-code\/5f286594f22173.27811194.png\" width=\"624\" height=\"77\" data-image=\"o082hjul7rje\"\/><\/figure>\n<h2><a id=\"breakthings\"\/>Conclusion: break issues and have enjoyable<\/h2>\n<p>All of that is under no circumstances excellent, and I\u2019m certain there\u2019s stuff you possibly can add and enhance, however that is a approach of constructing a community of scalable chatbots, every particular to a completely different enterprise, and every partially managed by that enterprise at minimal price.<\/p>\n<p>Give this a attempt, break it, enhance it, tear it up and begin once more, and let me know what you suppose!<\/p>\n<hr\/>\n<h2><a id=\"postscript\"\/>Postscript: bizarre hacks<\/h2>\n<p>This bit is just actually for people who find themselves , however as a result of we\u2019ve intentionally accomplished this on a shoestring, we run into a couple bizarre points \u2014 primarily round requests to our bot when it hasn\u2019t been activated for a bit.<\/p>\n<p>When Twilio will get messages for the primary time in a whereas, it activates fairly rapidly and expects different issues to accomplish that, too. For instance, when Twilio makes requests to our code, it assumes that the code failed if it takes greater than about 5 seconds. That\u2019s not that uncommon \u2014 a lot of chat platforms demand a five-second max turnaround time.<\/p>\n<p>Cloud Functions are ready to run fairly quick, even with decrease reminiscence allowances, however Google Sheets all the time appears to be a bit gradual when accessed by means of the API. In reality, Google Sheets is especially gradual if it hasn\u2019t been accessed in a while. <\/p>\n<p>That can imply that, if nobody has used your bot just lately, Google Sheets API takes too lengthy to reply the primary time and Twilio provides up earlier than our code can return, inflicting an error.<\/p>\n<p>There are a couple components of our script designed to keep away from that.<\/p>\n<h3>Trying once more<\/h3>\n<p>The first time we activate our Cloud Function, we don\u2019t need it to truly change something, we simply need info. So in Twilio, we begin by creating a variable referred to as \u201cretries\u201d and setting the worth as zero.\u00a0<\/p>\n<p>If the request fails, we examine if the retries worth is zero. If it&#8217;s, then we set the retries worth to 1 and attempt once more. If it fails a second time, we don\u2019t need to maintain doing this eternally so we ship an error and cease there.<\/p>\n<h3>Waking the sheet up<\/h3>\n<p>The second time we activate our Cloud Function we do need it to do one thing. We can\u2019t simply do it once more if it doesn\u2019t return in time as a result of we\u2019ll find yourself with duplicate orders, which is a headache for the restaurant.<\/p>\n<p>Instead, throughout an earlier a part of the trade, we make a pointless change to one in all our sheets, simply in order that it\u2019s prepared for once we make the vital change.<\/p>\n<p>In our conversational circulate we:<\/p>\n<ol>\n<li>Send the menu<\/li>\n<li>Get the response<\/li>\n<li>Ask for the consumer\u2019s title<\/li>\n<li>Write the order<\/li>\n<\/ol>\n<p>We don\u2019t want to do something to the sheet till step 4, however after we get the consumer\u2019s response (earlier than we ask their title), we activate our code as soon as to write one thing ineffective into the order sheet. We say to Twilio \u2014 whether or not that succeeds or fails \u2014 maintain going with the interplay, as a result of it doesn\u2019t matter at that time whether or not we\u2019ve returned in time. Then, hopefully, by the point we go to write in our order, Google Sheets is prepared for some precise use.<\/p>\n<h3>There are limitations<\/h3>\n<p>Google Sheets just isn&#8217;t the best database \u2014 it\u2019s gradual and may imply we miss the timeouts for Twilio. But these couple of additional steps assist us work round a few of these limitations.<\/p>\n<\/p><\/div>\n<p><br \/>\n<br \/><a href=\"https:\/\/feedpress.me\/link\/9375\/13779574\/sms-chatbot-free-code\">Source hyperlink Internet Marketing<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Many of us are serving to companies which might be going through arduous occasions, or we\u2019re going through arduous occasions ourselves. If you\u2019re working for a firm (or shopper) that is in bother, using SMS <a class=\"mh-excerpt-more\" href=\"https:\/\/internetmarketingoasis.com\/blog\/how-to-make-a-scalable-sms-chatbot-using-twilio-python-and-google-sheets-with-free-code\/\" title=\"How to Make a Scalable SMS Chatbot Using Twilio, Python, and Google Sheets (with Free Code)\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":4,"featured_media":6253,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3],"_links":{"self":[{"href":"https:\/\/internetmarketingoasis.com\/blog\/wp-json\/wp\/v2\/posts\/6251"}],"collection":[{"href":"https:\/\/internetmarketingoasis.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/internetmarketingoasis.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/internetmarketingoasis.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/internetmarketingoasis.com\/blog\/wp-json\/wp\/v2\/comments?post=6251"}],"version-history":[{"count":0,"href":"https:\/\/internetmarketingoasis.com\/blog\/wp-json\/wp\/v2\/posts\/6251\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/internetmarketingoasis.com\/blog\/wp-json\/wp\/v2\/media\/6253"}],"wp:attachment":[{"href":"https:\/\/internetmarketingoasis.com\/blog\/wp-json\/wp\/v2\/media?parent=6251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/internetmarketingoasis.com\/blog\/wp-json\/wp\/v2\/categories?post=6251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/internetmarketingoasis.com\/blog\/wp-json\/wp\/v2\/tags?post=6251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}