<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Lolo Code</title>
    <description>The latest articles on DEV Community by Lolo Code (@lolo_code).</description>
    <link>https://dev.to/lolo_code</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F914521%2Ff6b78c78-7ccb-494f-ae2c-daeef4f3ab00.jpg</url>
      <title>DEV Community: Lolo Code</title>
      <link>https://dev.to/lolo_code</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lolo_code"/>
    <language>en</language>
    <item>
      <title>Build a Serverless Discord Bot with OpenAI GPT-3 in Minutes using Template Functions in Lolo</title>
      <dc:creator>Lolo Code</dc:creator>
      <pubDate>Tue, 31 Jan 2023 12:43:49 +0000</pubDate>
      <link>https://dev.to/lolo_code/build-a-serverless-discord-bot-with-openai-gpt-3-in-minutes-using-template-functions-in-lolo-12ge</link>
      <guid>https://dev.to/lolo_code/build-a-serverless-discord-bot-with-openai-gpt-3-in-minutes-using-template-functions-in-lolo-12ge</guid>
      <description>&lt;h2&gt;
  
  
  Leverage the Low Code part of Lolo Code to build a Discord Bot that will be able to receive messages and respond to messages with OpenAI’s GPT-3 in a few minutes.
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjrdjqd1p1pl9paq32x1s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjrdjqd1p1pl9paq32x1s.png" alt="Image description" width="800" height="488"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This article is a response to a user that wanted us to show how to quickly build a Serverless Discord bot that will be able to handle and create messages using OpenAI’s GPT-3. We’ll be using a library function we’ve set up so there will be minimal code involved here. You’ll be able to customize intents and partials as you wish though, by configuring the Discord Socket function in Lolo.&lt;/p&gt;

&lt;p&gt;See the Lolo workflow below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu0h1lpph7ka1ud8l7430.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu0h1lpph7ka1ud8l7430.png" alt="Image description" width="800" height="307"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The process will be about 5 or so minutes. Let’s start.&lt;/p&gt;

&lt;h3&gt;
  
  
  Create your Discord Bot
&lt;/h3&gt;

&lt;p&gt;Get started by going to &lt;a href="https://discord.com/developers/applications" rel="noopener noreferrer"&gt;https://discord.com/developers/applications&lt;/a&gt; and creating a new application. You’ll need have a Discord Server before doing this, so do create one if you don’t have one yet.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8b4gg98qc8kyst4b7a2n.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8b4gg98qc8kyst4b7a2n.gif" alt="Image description" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Go to the Bot tab and click ´Create a bot´. Once your bot has been created, you’ll need to set some Privileged Gateway Intents by scrolling downwards.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0efsir0p2nn6ctp9tr9o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0efsir0p2nn6ctp9tr9o.png" alt="Image description" width="800" height="384"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After you’ve saved do Reset Token and then save your token somewhere safe. We’ll need it in a bit.&lt;/p&gt;

&lt;p&gt;Now navigate to OAuth2 and then URL Generator. Set Bot under Scope and then Send Messages and then Read Messages/View Channels as Bot Permissions.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs51xw5ceu419o1wis88j.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs51xw5ceu419o1wis88j.gif" alt="Image description" width="600" height="301"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Copy the URL that has been generated for you at the bottom. Paste it into your browser. This is how you’ll authorize your application.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F45hwkf1l0iob3giz1k7s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F45hwkf1l0iob3giz1k7s.png" alt="Image description" width="800" height="363"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Great. We’re done here.&lt;/p&gt;

&lt;h3&gt;
  
  
  Create an Application &amp;amp; add the Discord Socket
&lt;/h3&gt;

&lt;p&gt;Go to &lt;a href="https://console.lolo.co/" rel="noopener noreferrer"&gt;Lolo Code&lt;/a&gt; and create a new application. You can create a free account on the same page if you don’t have an account.&lt;/p&gt;

&lt;p&gt;You can name the application whatever you want.&lt;/p&gt;

&lt;p&gt;Once you have an empty graph in front of you, go to the Library Function’s menu to the left. Look for Discord Socket there and add it once you find it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F26p7jw6smc4ij8rtplr6.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F26p7jw6smc4ij8rtplr6.gif" alt="Image description" width="760" height="478"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You always open up functions by double clicking them. The Discord Socket needs to be configured with the token we got earlier.&lt;/p&gt;

&lt;p&gt;We also need to set Operation → Messages.&lt;/p&gt;

&lt;p&gt;To be able to read DMs to the bot we need to set some Intents and Partials as well. Set DirectMessages, GuildMessages and MessageContent as Intents. You can set Guilds as well. Make sure you also set Channel as Partials. See our config below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fejteo3sy2s4onebfq4pu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fejteo3sy2s4onebfq4pu.png" alt="Image description" width="800" height="766"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What you set here dictates the messages your socket will receive from Discord. You can read up on Intents &lt;a href="https://discordjs.guide/popular-topics/intents.html#privileged-intents" rel="noopener noreferrer"&gt;here&lt;/a&gt; and &lt;a href="https://discordjs.guide/popular-topics/partials.html#enabling-partials" rel="noopener noreferrer"&gt;Partials&lt;/a&gt; here.&lt;/p&gt;

&lt;h3&gt;
  
  
  Process Incoming Messages from Discord
&lt;/h3&gt;

&lt;p&gt;Now that we’ve set up the Discord socket we’ll receive messages depending on the intents we set. For this one, we’ll receive messages from any channel.&lt;/p&gt;

&lt;p&gt;Depending on what messages you would like to respond to you may want to first just log the data the message comes with in a new function.&lt;/p&gt;

&lt;p&gt;So, create a new function and paste in this code into it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;handler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ev&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;log&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// you'll have access to the client as well as the interaction in the event object&lt;/span&gt;
  &lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;incoming event data: &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ev&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;interaction&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code will log the interaction data to the Logs in your Lolo application.&lt;/p&gt;

&lt;p&gt;You can connect the Discord Socket to the new function now and then Save → Configure → Advanced Settings → Set Node V.16 → Update. This should deploy your application with Node version 16.&lt;/p&gt;

&lt;p&gt;See the clip below on how we do this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F08zc0hhnzj9sc5odwng2.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F08zc0hhnzj9sc5odwng2.gif" alt="Image description" width="720" height="453"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Please wait up to a minute before you see your Logs coming in when you first deploy with new dependencies.&lt;/p&gt;

&lt;p&gt;Once it has been deployed, you can write something in a private message to your bot in Discord and see the event come in in your Logs in Lolo.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fac5icy4eucpz6ixe7nao.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fac5icy4eucpz6ixe7nao.png" alt="Image description" width="800" height="401"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You may want to look into this event data to see what you want to filter with. I will be filtering out messages that are coming from this channelid and then I will filter out any messages made by the bot itself. I.e. I only want the message that comes from a direct DM and is not made by the bot itself.&lt;/p&gt;

&lt;p&gt;So open up the new function again, remove the previous code and paste in this code. You will have to tweak two values in it, as you need to set the your own channelId and authorId.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;handler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ev&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;route&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;log&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// you'll have access to the client as well as the interaction in the event object&lt;/span&gt;
  &lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;incoming event data: &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ev&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;interaction&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

  &lt;span class="c1"&gt;// parse interaction data to extract channel and author&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;interaction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ev&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;interaction&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

  &lt;span class="c1"&gt;// filter the message - in this case the channelID of the DM and do not respond if author is bot with this id&lt;/span&gt;
  &lt;span class="c1"&gt;// REMEMBER TO CHANGE IDS&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;interaction&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;channelId&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;CHANNELID HERE&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;interaction&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;authorId&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;BOT AUTHOR ID HERE&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;ev&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;interaction&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ev&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;out&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I have already logged a message from the bot to get that interaction.authorId but you may want to start only with filtering interaction.channelid and some other identifier. You have access to the message content itself in interaction.content that you can use instead.&lt;/p&gt;

&lt;p&gt;Let’s make an API call to OpenAI now.&lt;/p&gt;

&lt;h3&gt;
  
  
  Make an OpenAI API Call with the Message
&lt;/h3&gt;

&lt;p&gt;If you look at the above code, we’re setting ev.message with the text content here when the message passes that if statement. We’ll use this in the next function now.&lt;/p&gt;

&lt;p&gt;Open up your Library Function’s menu again and look for Lolo/OpenAI Completions. Once you find it, click it to add it on. Open it up to configure it.&lt;/p&gt;

&lt;p&gt;We’ll need an API key here for this. So go get it in your OpenAI account. If you don’t have an account yet, do create one and then create a new key here.&lt;/p&gt;

&lt;p&gt;Set prompt as {event.message} and then the model as text-davinci-003. Also set some additional options, temperature as 0.3 and max_tokens as 1500. Tick in the Store Prompt History, if you want a feeling of a conversation with your bot. This is optional.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Furn8m6eluodn0q5217yu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Furn8m6eluodn0q5217yu.png" alt="Image description" width="800" height="721"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You may tweak this as you’d like. It is simply making an API call. However, you don’t want to use too high of a value for max_tokens or you may end up with 400 errors.&lt;/p&gt;

&lt;p&gt;We’re done here so we can set up the response we’ll send back to Discord now in another new function.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reply to the Message with the OpenAI Response
&lt;/h3&gt;

&lt;p&gt;Go back to your graph and create another new function.&lt;/p&gt;

&lt;p&gt;Paste in this code into its code handler.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;handler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ev&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// OpenAI response&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;aiResponse&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ev&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;choices&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// respond to discord message&lt;/span&gt;
  &lt;span class="nx"&gt;ev&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;interaction&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;aiResponse&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Rename it Reply and remove the out port.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb9bm2g2vfejc7fbgkr6w.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb9bm2g2vfejc7fbgkr6w.gif" alt="Image description" width="720" height="453"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Remember to connect all your functions with the correct ports. See the finished workflow below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjfxrmw13q8c39ijuf0tr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjfxrmw13q8c39ijuf0tr.png" alt="Image description" width="800" height="327"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Save your application. Now go to configure instead of Run, and set Node version 16 under Advanced Settings. After this click Update. This will run your application with node version 16.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fejvndfe8v5qrr237miw4.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fejvndfe8v5qrr237miw4.gif" alt="Image description" width="720" height="453"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You shouldn’t have to pick node version each time, we’re fixing this with the new IDE for the future. But, unfortunately you’ll have to set this each time you re-deploy as it is now.&lt;/p&gt;

&lt;p&gt;Wait about a minutes to see your Logs come in. It should log ‘bot live’ when it is running.&lt;/p&gt;

&lt;h3&gt;
  
  
  Test your GPT-3 AI Discord Bot
&lt;/h3&gt;

&lt;p&gt;Now go back to Discord and DM your bot with some message to test it out.&lt;/p&gt;

&lt;p&gt;The result should look like the conversation below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnqo3yqgn0ryoqvx3uuxa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnqo3yqgn0ryoqvx3uuxa.png" alt="Image description" width="800" height="479"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Try out whatever with it. We haven’t tested everything yet and it goes without saying you should be able to do more advanced stuff with this.&lt;/p&gt;

&lt;p&gt;Join us in &lt;a href="https://discord.gg/9WEcEvvjDw" rel="noopener noreferrer"&gt;discord&lt;/a&gt; to talk with others or get help from the team.&lt;/p&gt;

&lt;p&gt;❤ Lolo&lt;/p&gt;

</description>
      <category>motivation</category>
      <category>career</category>
      <category>workplace</category>
    </item>
    <item>
      <title>Create a Free WhatsApp Binance Bot in a Few Minutes with Serverless in Lolo</title>
      <dc:creator>Lolo Code</dc:creator>
      <pubDate>Tue, 31 Jan 2023 12:19:39 +0000</pubDate>
      <link>https://dev.to/lolo_code/create-a-free-whatsapp-binance-bot-in-a-few-minutes-with-serverless-in-lolo-2plm</link>
      <guid>https://dev.to/lolo_code/create-a-free-whatsapp-binance-bot-in-a-few-minutes-with-serverless-in-lolo-2plm</guid>
      <description>&lt;h2&gt;
  
  
  For anyone keen to set up their own WhatsApp bots and stream real-time cryptocurrency data from Binance in a few minutes with minimal code with a Serverless app.
&lt;/h2&gt;

&lt;p&gt;For this article we’ll help you go through the entire process of setting up a Facebook app that allows a bot user to receive and send messages. Then we’re setting up a Lolo application (i.e. a Serverless event-driven app) to receive and send messages to WhatsApp via a few triggers (library functions).&lt;/p&gt;

&lt;p&gt;We’re also learning how to quickly set up a Binance data stream to get continuous updates on btc. You can choose which cryptocurrency you’d like. You’ll also be able to chose any trade steam. This is just a demo for something simple. We thank Jesper on our team for setting most of this up so it will go smoothly.&lt;/p&gt;

&lt;p&gt;The process will look like this.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Get Credentials from Facebook&lt;/li&gt;
&lt;li&gt;Create a Lolo Application&lt;/li&gt;
&lt;li&gt;Configure Webhooks&lt;/li&gt;
&lt;li&gt;Process Messages&lt;/li&gt;
&lt;li&gt;Set up a Binance stream to receive price updates for BTC&lt;/li&gt;
&lt;li&gt;Send back a reply to WhatsApp&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Facebook or Meta does make us jump through a few hoops for credentials here but once you’ve set this up once it will be easier the next time. And isn’t learning how to create WhatsApp bots pretty essential?&lt;/p&gt;

&lt;h3&gt;
  
  
  Get Credentials from Meta for WhatsApp
&lt;/h3&gt;

&lt;p&gt;Log into developer.facebook and create a new app. If you don’t have a developer account do open one.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flfz6pr1iw7pqcxw0c81g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flfz6pr1iw7pqcxw0c81g.png" alt="Image description" width="800" height="515"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Go to settings → basic to get your app secret.&lt;/p&gt;

&lt;p&gt;We’re done here for now but do keep the tab open because we’re coming back to set up a webhook in a bit.&lt;/p&gt;

&lt;h3&gt;
  
  
  Create a Lolo Application to set up a Webhook
&lt;/h3&gt;

&lt;p&gt;Create a new application in &lt;a href="https://console.lolo.co/" rel="noopener noreferrer"&gt;Lolo Code.&lt;/a&gt; You can create a free account on the same page if Lolo is new to you.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F38gwcn3vn8n66v89e3u6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F38gwcn3vn8n66v89e3u6.png" alt="Image description" width="800" height="599"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Add the Lolo/WhatsApp Trigger to your application.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqe2k0h1mny9h6lop6m1i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqe2k0h1mny9h6lop6m1i.png" alt="Image description" width="800" height="673"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Save and Run your application.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdzz3steytgazltssvo08.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdzz3steytgazltssvo08.png" alt="Image description" width="800" height="675"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Go to docs to find your callback url as we’ll need it in a bit.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F32c80jnbtzn9amepx7jf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F32c80jnbtzn9amepx7jf.png" alt="Image description" width="800" height="675"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Configure Webhooks in Facebook to Create a WhatsApp Bot that can Receive Messages
&lt;/h3&gt;

&lt;p&gt;Go back to Facebook’s developer dashboard and find Products → webhooks.&lt;/p&gt;

&lt;p&gt;Choose WhatsApp Business Account and then click ”subscribe to this object.” Here you’ll paste in your callback url that we got from Lolo as well as the verification string we put into our WhatsApp trigger in Lolo, i.e. itisme.&lt;/p&gt;

&lt;p&gt;Once you’re done you should see choices to subscribe to, what you’re looking for is Messages. Click subscribe.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi4927elphfq43goqqu8i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi4927elphfq43goqqu8i.png" alt="Image description" width="800" height="571"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now navigate to Get Started under WhatsApp just under Webhooks, here you’ll need to add your phone number of your WhatsApp account. You’ll get a verification code to your whatsApp account to verify it.&lt;/p&gt;

&lt;p&gt;You’ll also want to save the phone number that Facebook gives you as well, because it’ll be your bot’s number.&lt;/p&gt;

&lt;h3&gt;
  
  
  Process Messages Coming From WhatsApp
&lt;/h3&gt;

&lt;p&gt;If you try to message the number of our new bot we’ll get a payload from Whatsapp with something like the JSON below. Right now we’re not logging anything in the Lolo app but just to demonstrate what the payload will look like.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"from"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"timestamp"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"1671457845"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"text"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; 
    &lt;/span&gt;&lt;span class="nl"&gt;"body"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"incoming text here"&lt;/span&gt;&lt;span class="w"&gt; 
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"text"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So to access the text of the message in a connecting node via the event (ev) object that is accessible in the event handler we would use ev.text.body. To do this, create a new raw function in your Lolo app.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbryx4r6166a17hb19p5x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbryx4r6166a17hb19p5x.png" alt="Image description" width="800" height="656"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Double click it to change its code. We get a bunch of default code in the handler that we need to replace. Right now it would just be logging the event object to the Logs. So, remove the default text and copy the text below to paste it into its handler.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;handler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ev&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;route&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;log&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// access text of whatsapp payload&lt;/span&gt;
  &lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ev&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="c1"&gt;// route the messages to the right port&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ev&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;ev&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;btc&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ev&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;btc&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we are setting the naming to btc (as we will be checking Bitcoin pricing) but you can change this later.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx5dhh6keykmgh1l30s6c.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx5dhh6keykmgh1l30s6c.gif" alt="Image description" width="800" height="426"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see in the code above, we are routing the message to btc here. To route this to a port called btc, we need to create it. So, go to the Settings and then look for the tab called Ports.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8bmkkjrfzucz2kjed9w1.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8bmkkjrfzucz2kjed9w1.gif" alt="Image description" width="760" height="405"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can also rename the function to Handle Messages and connect the two nodes.&lt;/p&gt;

&lt;p&gt;What we’ve done so far is to set up a webhook that we’ve given Facebook so it will be triggered when a WhatsApp message comes in via the Bot’s number. When a message is sent to this number, the Lolo app will recieve an event with a payload of data. We then process the message by accessing ev.text.body in another node to see if the message is equal to &lt;code&gt;btc&lt;/code&gt; and if so we’ll route the message further via the outport btc.&lt;/p&gt;

&lt;p&gt;Now we’ll start streaming Binance data.&lt;/p&gt;

&lt;h3&gt;
  
  
  Set up a Binance Stream to Receive Price Updates for BTC
&lt;/h3&gt;

&lt;p&gt;Go look for the Lolo/Binance Trigger in your menu palette, it should be available there.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuum6cu57urzryyyrhht8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuum6cu57urzryyyrhht8.png" alt="Image description" width="800" height="653"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click it to add it to your application and then double click it to configure it. It may take 20 seconds for the params to show so do wait for them.&lt;/p&gt;

&lt;p&gt;Configuring the trigger is real easy. Just select the kind of data stream you’re interested in. Depending on the stream some additional data might be required, but all options are just simple drop down menus.&lt;/p&gt;

&lt;p&gt;In this case we’re interested in the ticker for the price of Bitcoin in USD.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F32ljgafwbn6klzjisr6n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F32ljgafwbn6klzjisr6n.png" alt="Image description" width="800" height="652"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;More information about what streams are available and what they contain can be found in the &lt;a href="https://docs.binance.us/?shell#market-data-streams" rel="noopener noreferrer"&gt;Binance docs.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now you have created a data stream with BTC pricing, this will produce an event every second. You can log the event in another function to see the events being produced but we’ll skip that here.&lt;/p&gt;

&lt;p&gt;You can probably think of other things to do with this I imagine. Maybe setting up a an if statement that looks for abnormalities and then pushes a message to WhatsApp when the price falls below or above your given thresholds. This would be easier than this article.&lt;/p&gt;

&lt;p&gt;In any case, we’re doing something different here.&lt;/p&gt;

&lt;h3&gt;
  
  
  Get Current BTC Price When the WhatsApp Message Comes Through
&lt;/h3&gt;

&lt;p&gt;Right, so when we ask for btc in WhatsApp we need to somehow get the current price that has been streamed by the Binance trigger. So, we need a way to store the latest price so that we can retrieve it when the btc command is received.&lt;/p&gt;

&lt;p&gt;To do this we add another raw function in the bottom right corner. Remove the default code in its handler and insert the code below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;latest_price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;handler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ev&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;update&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;latest_price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ev&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;get&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;route&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;ev&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;latest_price&lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above lines of code stores the price in the latest_price variable if the incoming port is update (i.e. comes from the streaming Binance data trigger), and if the port coming in is get (i.e. comes from the WhatsApp event), it routes the latest_price that the variable has stored to the out port.&lt;/p&gt;

&lt;p&gt;For this function we then need two input ports (update &amp;amp; get) and one output port. To configure ports for a function navigate to Settings and then to the Ports tab. Look below on what this should look like.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Figxvmh2enj44l0kko9gf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Figxvmh2enj44l0kko9gf.png" alt="Image description" width="800" height="625"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can rename the function to Store as well.&lt;/p&gt;

&lt;p&gt;If this is confusing, just keep going. It’ll be less so once you’re done.&lt;/p&gt;

&lt;p&gt;We also need to connect the right ports. Remember when an event is hit data flows from one node to another via the ports. So make sure your ports are connected like the picture below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fic6mwon40hb6xhlcbb5m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fic6mwon40hb6xhlcbb5m.png" alt="Image description" width="800" height="647"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The get port should be connected to Handle Messages (coming from WhatsApp) and the update port should be connected to the Binance Stream Trigger.&lt;/p&gt;

&lt;p&gt;Craft a Reply to Send to WhatsApp&lt;br&gt;
Now we need to send back a reply to the WhatsApp message but first we need to just structure the event object correctly to be proccessed by the library function.&lt;/p&gt;

&lt;p&gt;You can probably do this in the function above but let’s do it separately.&lt;/p&gt;

&lt;p&gt;Create a new additional function in Lolo, remove the default code and paste in the code below into its handler.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;handler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;latest_price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;ev&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;}},&lt;/span&gt; &lt;span class="nx"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;route&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;to&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`The latest price of BTC is &lt;/span&gt;&lt;span class="se"&gt;\$&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;latest_price&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;.`&lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code is setting up the event object so it can be processed by the WhatsApp Send function in a bit. It’s another library function like the Binance Trigger and has been set up by Lolo so it’ll be easy to use.&lt;/p&gt;

&lt;p&gt;Rename the function to Reply. You can connect it to the Store function’s output port as well.&lt;/p&gt;

&lt;p&gt;The last thing we have to do now is send a message back to WhatsApp.&lt;/p&gt;

&lt;h3&gt;
  
  
  Send Back BTC Pricing to WhatsApp
&lt;/h3&gt;

&lt;p&gt;Search for another function called Lolo/WhatsApp Send in your Lolo Application.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3dlf4d0louair15vsjc1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3dlf4d0louair15vsjc1.png" alt="Image description" width="800" height="751"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once you’ve found it, you can add it to your application. Double click it to configure it.&lt;/p&gt;

&lt;p&gt;Here we need to add a few things. And here is also where you need to spend a bit more time as Meta is difficult about sending messages to WhatsApp so we’ll need a few more credentials.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmfxq6awjpent1k78tbbc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmfxq6awjpent1k78tbbc.png" alt="Image description" width="800" height="748"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Go back to your Facebook Dashboard and navigate to WhatsApp again and then Getting Started. Here you can find your Phone Number ID. Remember it is the phone number ID we’re looking for and not phone number.&lt;/p&gt;

&lt;p&gt;You can paste that ID into the second input.&lt;/p&gt;

&lt;p&gt;For getting a META access token we’ll have to go through a few more hoops. The reason is that META would like you to create a so called system user with correct privileges. But you only have to do this once, after this you can use this bot user for other things.&lt;/p&gt;

&lt;p&gt;To create a system user, go to business.facebook.com. You should land on a page that looks like this. For you this will be a personal account, for us it the our Lolo Business account. We never really use Facebook but we do have an account there hehe.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fueo9dc009e0uhz8cije9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fueo9dc009e0uhz8cije9.png" alt="Image description" width="800" height="653"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Press the Settings button and then find Business Settings to press that too.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvlxicc7x5dn9yb8ne3ic.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvlxicc7x5dn9yb8ne3ic.png" alt="Image description" width="800" height="559"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This should bring you to a page like this where you can add system users.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh2zxfrvcqhporri2d3pg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh2zxfrvcqhporri2d3pg.png" alt="Image description" width="800" height="586"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can click Add and then name the user whatever you’d like. Choose Employee as well.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faa7q0j7x1qw39z4dd5f1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faa7q0j7x1qw39z4dd5f1.png" alt="Image description" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When it has been generated for you, click Add Assets. Then go to Apps, choose your App where you’ve set up WhatsApp and then tick Access to Develop app.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fusvv1lavyr21b8bj8zp5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fusvv1lavyr21b8bj8zp5.png" alt="Image description" width="800" height="554"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hit save. Now the our created system user need to be given access to the WhatsApp account. To do that, in business settings, navigate to Accounts → WhatsApp-Account.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3kcuuv679abfz20vfu0h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3kcuuv679abfz20vfu0h.png" alt="Image description" width="800" height="547"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click the add people button. A dialog pops up. Select the system user, and toggle partial access.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5w3u1z3iday98ltv1ufg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5w3u1z3iday98ltv1ufg.png" alt="Image description" width="800" height="540"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hit save.&lt;/p&gt;

&lt;p&gt;We now need to generate an access token for this system user. Go back to the page system users settings page and click Generate Token.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwbssh63cyvvrggppezrn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwbssh63cyvvrggppezrn.png" alt="Image description" width="800" height="544"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A dialog will pop up asking you to choose an app. Once you’ve choosen your app you will have to set Permissions. We are looking for whatsapp_business_messaging.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj5vf3jw4u1rxez4glr30.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj5vf3jw4u1rxez4glr30.png" alt="Image description" width="800" height="530"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Scroll down the bottom of the dialog and hit the button that says generate. You’ll see a new dialog with the generated token. We finally got it!&lt;/p&gt;

&lt;p&gt;Go back to your Lolo app and paste in the token in the Lolo/WhatsApp Send node.&lt;/p&gt;

&lt;h3&gt;
  
  
  Test it Out
&lt;/h3&gt;

&lt;p&gt;Use the phone number we got from the Facebook App and then send it btc it should send you back the current Bitcoin price.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flwzui7sn2bt97mlp3dna.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flwzui7sn2bt97mlp3dna.gif" alt="Image description" width="400" height="225"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It goes without saying that you can create more complex stuff than this. It is just a demo of the WhatsApp and Binance Library Functions we got available.&lt;/p&gt;

&lt;p&gt;❤ Lolo&lt;/p&gt;

</description>
      <category>gratitude</category>
      <category>braziliandevs</category>
      <category>community</category>
    </item>
    <item>
      <title>Create a free OpenAI GPT-3 Bot to Have Someone to Talk to at Work via a Slack app with Serverless</title>
      <dc:creator>Lolo Code</dc:creator>
      <pubDate>Thu, 08 Dec 2022 16:10:08 +0000</pubDate>
      <link>https://dev.to/lolo_code/create-a-free-openai-gpt-3-bot-to-have-someone-to-talk-to-at-work-via-a-slack-app-with-serverless-2fm3</link>
      <guid>https://dev.to/lolo_code/create-a-free-openai-gpt-3-bot-to-have-someone-to-talk-to-at-work-via-a-slack-app-with-serverless-2fm3</guid>
      <description>&lt;p&gt;OpenAI invites everyone to test ChatGPT, a new AI-powered chatbot so let’s use it to quickly build a Slack app to communicate with it for free. Yes, we created a library function for this so it’ll be quick and you won't need that much programming skills for this.&lt;/p&gt;

&lt;p&gt;Check out a preview of the finished work below. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4zokrsts5zxo6i4tfe58.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4zokrsts5zxo6i4tfe58.gif" alt="Image description" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To go through the steps required to do this.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We need to create a new Slack app and enable slash commands (I’ve picked /talkhere as you can see in the screen above.&lt;/li&gt;
&lt;li&gt;When this command is called via Slack we will call OpenAI via a library function in Lolo. You’ll be able to set temperature and max tokens if you want.&lt;/li&gt;
&lt;li&gt;Then we’ll route this data to Slack via another Library function in Lolo allowing us to override the message with the OpenAI answer.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The result will be an answer in Slack from OpenAI based on our prompt.&lt;/p&gt;

&lt;p&gt;Check out our serverless Lolo app below as well.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9rl5ty3lxnmsvoaty6s8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9rl5ty3lxnmsvoaty6s8.png" alt="Image description" width="800" height="361"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You have 18$ worth of free tokens in OpenAI that you can use, and that will last a long time. The Lolo app is free. Creating a Slack bot in Slack is free as well so no need to pay anything for this to work.&lt;/p&gt;

&lt;p&gt;We tested a bunch of things, cleaning up code, doing translations, basic questions, talking to it, creating python code, correcting messages. You name it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting an OpenAI API Key
&lt;/h2&gt;

&lt;p&gt;You’ll need an OpenAI API key, that you can quickly get just signing up &lt;a href="https://beta.openai.com/" rel="noopener noreferrer"&gt;here&lt;/a&gt;. Then navigate to API Keys to create a new key. Save the key somewhere, we’ll need it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj0vcv26xpxocrkc3x3pz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj0vcv26xpxocrkc3x3pz.png" alt="Image description" width="800" height="535"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up a Lolo Application
&lt;/h2&gt;

&lt;p&gt;Create a Lolo account &lt;a href="https://console.lolo.co/" rel="noopener noreferrer"&gt;here&lt;/a&gt; if you don’t have one already. Otherwise, create a new app and then find a HTTP trigger in the menu to the left of the graph.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpqi20s7ri6l2k1oe32se.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpqi20s7ri6l2k1oe32se.gif" alt="Image description" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Add it to your application and then make sure to set a path and rename it to Slack Webhook.&lt;/p&gt;

&lt;p&gt;Psst! We forgot to show this but you should also set method to POST and not GET. Slack will send us a POST request, so if you don’t have the method set to post it won’t work.&lt;/p&gt;

&lt;p&gt;Navigate to the HTTP trigger again to copy the external url, we’ll need it in a bit when we set up our Slack command.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhs22gr5wg3cvt32euy5s.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhs22gr5wg3cvt32euy5s.gif" alt="Image description" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We’ll also set up to send back the required Slack response within 3 seconds in another node right away. So, create a new function in the bottom right corner and add this code block in there.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;exports.handler = async(ev, ctx) =&amp;gt; {
  const { route, emit } = ctx;

  // Emit response right away to Slack
  emit('response', { statusCode: '200'});

  ev.responseURL = ev.body.response_url;
  ev.message = ev.body.text;

  // route the event object to the next node
  route(ev);
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You’ll see in the code above that we are also getting the responseURL from the payload. We’ll need this one later to send back a message to Slack.&lt;/p&gt;

&lt;p&gt;We’re also collecting the message that we’ll use as the prompt when calling OpenAI and setting it to the event object (so it can be used in another node later).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh319yyia38hb2i96k1pi.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh319yyia38hb2i96k1pi.gif" alt="Image description" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let’s create a Slack app so we can use this webhook we’ve set up.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a Slack Application
&lt;/h2&gt;

&lt;p&gt;Go &lt;a href="https://api.slack.com/apps/" rel="noopener noreferrer"&gt;here&lt;/a&gt; and then press create new app. Unless you want to use an already existing application.&lt;/p&gt;

&lt;p&gt;Then navigate to slash commands, and create a new command. You can name it whatever you want but you need to set the external URL that we got from Lolo as the request URL. Slack will send a post request to it when it is hit so we can process that request (make the OpenAI call and so on).&lt;/p&gt;

&lt;p&gt;After this make sure to install your application to your workspace.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9i5vfbeplmybgdg5vxim.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9i5vfbeplmybgdg5vxim.gif" alt="Image description" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We’re done here now so let’s make the OpenAI API call.&lt;/p&gt;

&lt;h2&gt;
  
  
  Calling OpenAI with the Slack Message as the Prompt
&lt;/h2&gt;

&lt;p&gt;Now we’re heading back to the Lolo app and looking for the Lolo/OpenAI library function in the palette. Where you found the first HTTP trigger.&lt;/p&gt;

&lt;p&gt;Open it up to set the required values. Use the OpenAI API key that we got earlier. You can create another one if you’ve lost the first one.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flypfgg1lfvkurl47cj45.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flypfgg1lfvkurl47cj45.png" alt="Image description" width="800" height="644"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here we’ve set temperature as 0.3 which seems to be the amount of creativity you want it to answer with. We’ve also set max_tokens to 4000 so the message doesn’t get cut off. You can several more options to set here but make sure you use the right wording for them. For the first time use these two options or none at all, after that you can fine tune it as you want.&lt;/p&gt;

&lt;p&gt;The model is set to davinci which is the more expensive one, you can change this to ada or so. The prompt is set to a dynamic value from the event object that we have access to in each node. Remember that we set ev.message to the text we received from the Slack payload earlier.&lt;/p&gt;

&lt;p&gt;That is it here. Go back to your graph.&lt;/p&gt;

&lt;h2&gt;
  
  
  Send back a Message to Slack with the Response URL
&lt;/h2&gt;

&lt;p&gt;Now we need to look for the Slack library function to send back the response. So again open up the function palette in your graph in Lolo and add the Slack Msg (beta) from the list.&lt;/p&gt;

&lt;p&gt;Open it up to configure it. We need to set operation to Slack Response.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4vggkgotdq11p8qrhkrc.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4vggkgotdq11p8qrhkrc.gif" alt="Image description" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We’ll need to configure this one with a bit of dynamic values here. We have the response url set to the event object and OpenAI will give us back the response in {event.response} so to access the text of the response we will need to set {event.response.data.choices[0].text}&lt;/p&gt;

&lt;p&gt;See the exact values you want to set below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3ut81rpuixzjkqxve7to.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3ut81rpuixzjkqxve7to.png" alt="Image description" width="800" height="518"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To understand this, we are getting a payload with the Slack slash command and then saving the response url so we can send an HTTP request to it when we are ready (i.e. when we’ve made the OpenAI request and gotten back the correct data). This library function does this for us though but it does need somewhere to post to, i.e. to this response url.&lt;/p&gt;

&lt;p&gt;We’re done here so we can go back and make sure to connect all the nodes now and then Save and Run your application.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzxjou21b8oo178rganq7.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzxjou21b8oo178rganq7.gif" alt="Image description" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Go to your Logs to see things happen in your Lolo app. To make sure it has been deployed, look for “Listen to port 4000.” Do wait at least a minute.&lt;/p&gt;

&lt;p&gt;Now we can test it out. Go to Slack and try out your command with some text you want to ask OpenAI.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs6edirkm9dbsuhf19dty.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs6edirkm9dbsuhf19dty.gif" alt="Image description" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Try out whatever with it. We haven’t tested everything yet and it goes without saying you should be able to do more advanced stuff with this.&lt;/p&gt;

&lt;p&gt;❤ Lolo&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>watercooler</category>
    </item>
    <item>
      <title>Build a Serverless WebSocket Application in 5 minutes with Lolo</title>
      <dc:creator>Lolo Code</dc:creator>
      <pubDate>Mon, 05 Sep 2022 14:19:53 +0000</pubDate>
      <link>https://dev.to/lolo_code/build-a-serverless-websocket-application-in-5-minutes-with-lolo-4bo5</link>
      <guid>https://dev.to/lolo_code/build-a-serverless-websocket-application-in-5-minutes-with-lolo-4bo5</guid>
      <description>&lt;p&gt;Originally published &lt;a href="https://medium.com/aws-tip/build-a-serverless-websocket-application-in-5-minutes-with-lolo-1aea24f817a6"&gt;here&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;WebSockets are mainly used to create real-time applications, such as chat apps, collaboration platforms and streaming dashboards. These applications take advantage of two-way/bidirectional communication between a server and users’ browsers.&lt;/p&gt;

&lt;p&gt;Serverless is usually associated with FaaS, an isolated stateless function that is triggered by another service. Creating a Serverless Websocket application would require you to use AWS Lambda with WebSockets in API Gateway and DynamoDB to store connections. This is overly complicated.&lt;/p&gt;

&lt;p&gt;We’ll show you how to create something faster. Serverless iFaaS platforms don’t require initialization before the function executes so you don’t pay a price for waking up a container once it has been deployed. It’s technically always live. You also have access to a baked in key / value store which means that you don’t end up loosing state between subsequent invocations. I.e. you don’t have to start from a blank slate on every invocation. Hence the lack of need for an external database such as DynamoDB.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Plan
&lt;/h2&gt;

&lt;p&gt;We’ll demonstrate something very simple here by using a pre-made Websocket Trigger to connect and broadcast all messages to all connected clients. See the visual workflow below. You’ll need a free Lolo account to set this up yourself.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_BepU0x---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2iszn2f1wkx7cl0ue4bs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_BepU0x---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2iszn2f1wkx7cl0ue4bs.png" alt="Image description" width="880" height="361"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want to get started right away you can simply copy the contents of &lt;a href="https://drive.google.com/file/d/1okiUnbDpTrDk2Zyu5x6rS2Lo4e5sRkvd/view?usp=sharing"&gt;this document&lt;/a&gt; and paste it into a new application in a blank graph in Lolo. Save and Run the application to deploy it. That’s 1 minute of work.&lt;/p&gt;

&lt;p&gt;Do follow along though if you want to understand how to use it or if you want to set this up from scratch.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create an Application and add a Trigger
&lt;/h2&gt;

&lt;p&gt;To begin, we need to set up a new WebSocket server that can handle inbound WebSocket requests from clients.&lt;/p&gt;

&lt;p&gt;We do this by first creating a new application in your Lolo account and then adding the Lolo/Websocket Trigger from the Functions Gallery on the left of the graph. Add in a path name, such as /socket. You can also rename it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--S4ywmpwH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p7sj285b3491f9lczoj1.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--S4ywmpwH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p7sj285b3491f9lczoj1.gif" alt="Image description" width="880" height="387"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Process the Incoming Request
&lt;/h2&gt;

&lt;p&gt;When you add this WebSocket Trigger you get three output ports in the node called req, message and close so you need to setup ways to process these requests. Add another inline Function (i.e. a custom function) and name it ‘Process Request.’ Remove the in port and instead add in three in ports called req, msg and close.&lt;/p&gt;

&lt;p&gt;Link the ports to the right in and out ports with the nodes, look below for an illustration on how to do this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7AEpbP2E--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/usqjyej11bd0njnlxya9.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7AEpbP2E--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/usqjyej11bd0njnlxya9.gif" alt="Image description" width="880" height="346"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We need a bit of code to do something here with the WebSocket connection. Copy and paste in the code from below. Open up the Process Request node and paste in the code below in the handler.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const connections = {};

exports.handler = async (ev, ctx) =&amp;gt; {
  const { route, input, inputs, log, emit } = ctx;
  const { sessionId } = ev;

  // check incoming port (i.e. req, message or close)
  switch (input) {

    // on first connection
    case inputs.req:
      connections[sessionId] = {
        send: body =&amp;gt; emit('response', { body }),
        end: () =&amp;gt; emit('response', { end: true }),
        info: ev.headers
      }
      ev.body = { connected: true, yourConnectionId: sessionId };
      // re-route data to 'req' output port
      route(ev, 'req');
      break;

    // on subsequent messages 
    case inputs.msg:
      // re-route data to 'msg' output port
      route({ connections, message: ev.message, sessionId }, 'msg')
      break;

    // when client disconnects 
    case inputs.close:
      log.info("client has disconnected");
      delete connections[sessionId];
      break;
  }
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code above is checking what ports the incoming data is routed through, giving us a way to handle a new connection by adding it to connections. We are also setting up how we want to handle subsequent messages and a disconnecting client by using the in ports, msg and close.&lt;/p&gt;

&lt;p&gt;This code above though is re-routing to other nodes via the route() method as well. As you can see we have two output routes here to ‘req’ and ‘msg.’ We thus need to add those two output ports as well.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--exULcRQy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/40mm9y7uazfnyl0lksc9.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--exULcRQy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/40mm9y7uazfnyl0lksc9.gif" alt="Image description" width="880" height="352"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Send back a response
&lt;/h2&gt;

&lt;p&gt;Now we have two outports in the Process Request Function that needs to go somewhere, so we create another inline Function called Affirm Connection and paste in the code below.&lt;/p&gt;

&lt;p&gt;All this does is signal the listener within the WebSocket Trigger to send a response to the client. We will route the req route to this node.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;exports.handler = async(ev, ctx) =&amp;gt; {
  const { emit, log } = ctx;
  // Log to the console that a client has connected
  log.info("client has connected");
  // send response to the client
  emit('response', ev);
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Along with this we also need a way to handle subsequent messages. In this case we said we would broadcast all messages to all clients when someone sends something. So, create another inline Function (i.e. a custom function) and call it Handle Messages. Paste in the code below, see comments to understand what it does.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;exports.handler = async (ev, ctx) =&amp;gt; {
  // extract connections, and current session id from the event data
  const { connections, sessionId } = ev;
  // send messages
  await broadcastToAll(ctx, connections, sessionId, ev);
};

const broadcastToAll = async ({ log }, connections, currentSessionId, ev) =&amp;gt; {

  // loop through connections
  Object.keys(connections).forEach(sessionId =&amp;gt; {
    try {
        // send message to everyone but the current sessionId
        if (currentSessionId !== sessionId) {
        connections[sessionId].send(`${sessionId} says: ${ev.message}`)
        } 
    } catch (e) {
      log.error(e)
    }
  })

};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can remove the out ports for both of these new Functions. Now we also have to route the data visually from the Process Request node to the other two nodes we’ve created.&lt;/p&gt;

&lt;p&gt;See these steps in action below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--so8dkj-S--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/68wfuv6eemvi15ktd8rq.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--so8dkj-S--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/68wfuv6eemvi15ktd8rq.gif" alt="Image description" width="880" height="383"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Save and Deploy
&lt;/h2&gt;

&lt;p&gt;Alright, that was it. You can save and deploy.&lt;/p&gt;

&lt;p&gt;Look in the Logs for the ‘Listening to port 4000’ message to see if it is ready to use. This may take awhile. We are not using any NPM modules here so should be faster but if we did we would have to wait about a minute.&lt;/p&gt;

&lt;p&gt;Now go into the WebSocket Trigger to collect the External URL. We are going to use it to connect to this Websocket.&lt;/p&gt;

&lt;p&gt;Open two terminals on your computer and then connect via wscat.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;wscat -c wss://eu-1.lolo.co/:appId/socket
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remember to NPM install wscat -g (via your terminal) first if you don’t already have wscat installed.&lt;/p&gt;

&lt;p&gt;Now you should be able to talk via the different terminals. You can ask a friend to connect on a different computer and talk that way.&lt;/p&gt;

&lt;p&gt;It goes without saying that you can connect this via a frontend to build a quick chat app.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PgVn5InJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bp5wystph8pnfe8fdl76.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PgVn5InJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bp5wystph8pnfe8fdl76.gif" alt="Image description" width="880" height="383"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Remember if you are having issues, just copy the contents of &lt;a href="https://drive.google.com/file/d/1okiUnbDpTrDk2Zyu5x6rS2Lo4e5sRkvd/view?usp=sharing"&gt;this document&lt;/a&gt; and paste it into an empty graph in a Lolo application to get the entire app set up for you.&lt;/p&gt;

&lt;p&gt;Let us know if you have any feedback. We’re keen to create a better Serverless experience.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
