<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Victor Viruete on Medium]]></title>
        <description><![CDATA[Stories by Victor Viruete on Medium]]></description>
        <link>https://medium.com/@hopsor?source=rss-5cf8c0b77f8d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*GHvNKh7OHyQgid7J.jpeg</url>
            <title>Stories by Victor Viruete on Medium</title>
            <link>https://medium.com/@hopsor?source=rss-5cf8c0b77f8d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 08:39:04 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@hopsor/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Payload case conversion in Phoenix WebSockets]]></title>
            <link>https://medium.com/stuart-engineering/payload-case-conversion-in-phoenix-frameworks-websockets-31831da8e753?source=rss-5cf8c0b77f8d------2</link>
            <guid isPermaLink="false">https://medium.com/p/31831da8e753</guid>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[websocket]]></category>
            <category><![CDATA[phoenix-framework]]></category>
            <dc:creator><![CDATA[Victor Viruete]]></dc:creator>
            <pubDate>Tue, 12 Jun 2018 11:57:42 GMT</pubDate>
            <atom:updated>2018-06-12T16:10:07.499Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*RVHa1r1pyiHt0PQQqMRraQ.png" /></figure><p>Lately I’ve been working on a personal side project which uses an Elm single page application on the front end, and a Phoenix application on the back end. In our setup there isn’t any RESTful API, all the data flows through websockets.</p><h3>Dealing with naming conventions</h3><p>As you may know, the naming convention for variables in Elm and Elixir are different. The former follows camelCase while the latter follows snake_case. We wanted to be respectful of the conventions in each of the languages but we needed to transform the payloads at some point. The decision was to keep the frontend simple so we delegated this job to Phoenix websockets.</p><p>The naive solution was to manually transform each of the payloads from snake case to camel case, or the other way around depending on the direction of the message. However this was going to be tedious and painful to manage in the long term. So we preferred a healthier approach based on application level configuration.</p><h3>Finding the answer</h3><p>The question is how? If you’re building a regular HTTP API the solution is quite straight forward. You can use <a href="https://github.com/johnnyji/proper_case">proper_case</a> or <a href="https://github.com/sticksnleaves/accent/">accent</a> packages. Both of them make it really easy by supplying Plugs that you can take adventage of. However websockets are a different case. They don’t use plugs and Phoenix’s format encoder <a href="https://hexdocs.pm/phoenix/Phoenix.Template.html#module-format-encoders">configuration</a> hasn’t any effect on channels payloads. So what’s the best way to achieve it then?</p><p>Trying to do some research I came across this <a href="https://github.com/phoenixframework/phoenix/issues/2631">issue</a> open by Michał Muskała where he asks how to change the JSON encoder for websockets. Ok, it isn’t the very same requirement we have but it’s close. We both need to alter the payloads at some point so a solution for him may work for us as well.</p><p>Fortunately Phoenix Framework’s very own creator Chris McCord <a href="https://github.com/phoenixframework/phoenix/issues/2631#issuecomment-343971565">explained</a> how to make that possible. It basically needs a custom websocket serializer. In Phoenix it has to be done twice as it implements two different versions of the websocket protocol (1.0.0 and 2.0.0).</p><p>Before 2.0 it seems very unlikely that we’ll have a simpler solution but, for the time being, writing 2 files of around 20 lines each doesn’t seem a huge pain.</p><h3>Solution implementation</h3><p>As I mentioned, the solution is not complex at all. It just involves adding a couple of files and changing the UserSocket module to use our custom serializers. I’ve put together a working example on this GitHub <a href="https://github.com/hopsor/zoquete">repository</a> however below you can find each step explanation.</p><h4>Case conversion</h4><p>At the beginning of the post I talked about the <a href="https://github.com/johnnyji/proper_case">proper_case</a> package. Although we are not going to use its plug given we work with websockets, it’s still a handy tool we can benefit from to change the case of our payloads keys.</p><p>So the first step would be adding it to our Mixfile.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/61ae36ce97cf15c7bc7ec70bef60e588/href">https://medium.com/media/61ae36ce97cf15c7bc7ec70bef60e588/href</a></iframe><p>Now it’s time to write our custom serializers.</p><h4>Writing our custom serializers</h4><p>The next step is the most important one, writing our own custom websocket serializers. This is not the stuff you do on a daily basis within a Phoenix project, so I wasn’t sure what was the best folder to put them. Inside channels/socket_serializer folder is probably where they fit best.</p><p>There we’ll create v1.ex and v2.ex modules which are basically a copy of Phoenix’s <a href="https://github.com/phoenixframework/phoenix/blob/master/lib/phoenix/transports/websocket_serializer.ex">WebSocketSerializer</a> and its <a href="https://github.com/phoenixframework/phoenix/blob/master/lib/phoenix/transports/v2/websocket_serializer.ex">second version</a> respectively. Why are there a couple of implementations of the protocol? I’m not really sure yet 😅</p><p>Now that we have our serializers based on the original Phoenix ones it’s time to change them. The serializers include functions for encoding and decoding the messages. We just need to change them to transform the payloads the way we want. In our case, given we have an Elm frontend and an Elixir backend, we need to encode payloads with pascal case and decode them with snake case.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/60d5a6f4d20d6aa775aeb2f0239387b4/href">https://medium.com/media/60d5a6f4d20d6aa775aeb2f0239387b4/href</a></iframe><p>The same applies for v1 file but it’s slightly different. You can check it out <a href="https://github.com/hopsor/zoquete/blob/master/lib/zoquete_web/channels/socket_serializer/v1.ex">here</a>.</p><h4>Wiring all up in the socket</h4><p>Now that we have our custom serializers in place we can use them in our socket module.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dfe693de2275e7622b566781c0d48f2e/href">https://medium.com/media/dfe693de2275e7622b566781c0d48f2e/href</a></iframe><p>And that’s all. Following these few steps you would be able to be consistent with naming conventions on each side of your application.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=31831da8e753" width="1" height="1" alt=""><hr><p><a href="https://medium.com/stuart-engineering/payload-case-conversion-in-phoenix-frameworks-websockets-31831da8e753">Payload case conversion in Phoenix WebSockets</a> was originally published in <a href="https://medium.com/stuart-engineering">Stuart Tech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building a Slack serverless bot with Google Apps Script and Spreadsheets]]></title>
            <link>https://medium.com/@hopsor/building-a-slack-serverless-bot-with-google-apps-script-and-spreadsheets-35bdac755a44?source=rss-5cf8c0b77f8d------2</link>
            <guid isPermaLink="false">https://medium.com/p/35bdac755a44</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[google-spreadsheets]]></category>
            <category><![CDATA[slack]]></category>
            <category><![CDATA[bots]]></category>
            <category><![CDATA[google-apps-script]]></category>
            <dc:creator><![CDATA[Victor Viruete]]></dc:creator>
            <pubDate>Thu, 08 Feb 2018 20:24:12 GMT</pubDate>
            <atom:updated>2018-02-08T20:24:12.354Z</atom:updated>
            <content:encoded><![CDATA[<p>Sometimes when you work in remote environments with multiple staging servers it’s hard to keep track of which servers are free to use. Last place I worked we had 3 of those environments. When someone needed to deploy a branch to test something we picked one randomly, then we asked in the tech team channel if that server was already taken by someone else. As you can imagine people is always busy with their daily work. It’s very frequent that the person who already took a staging server take his time until he answers.</p><p>Given this problem we needed to figure out a better workflow for this purpose. Something that only requires the interaction of the person who wants to take or leave a staging server. Waiting for answers of the rest of the team wasn’t efficient.</p><p>The perfect solution for this particular problem was a <strong>bot</strong>.</p><h3>The Bot</h3><p>The idea was to build a bot that connects to the tech team channel and listen for these commands:</p><ul><li><strong>help</strong>: shows the list of available commands</li><li><strong>list</strong>: shows the list of available and busy servers</li><li><strong>take</strong> &lt;server_name&gt; &lt;reason&gt;: marks the server as taken by you</li><li><strong>leave</strong> &lt;server_name&gt;: marks the server as available</li><li><strong>create</strong> &lt;server_name&gt;: adds a new server</li><li><strong>remove</strong> &lt;server_name&gt;: removes an existing server</li><li><strong>rename</strong> &lt;old_server_name&gt; &lt;new_server_name&gt;: renames a server</li></ul><p>All the commands must begin by the <strong>!staging</strong> prefix.</p><h3>Alternative #1: Self-hosted bot. Nope</h3><p>At the beginning my first intention was to create a nodejs or elixir project. I would build a traditional bot that connects to the Slack channel and listens for the specific commands we want.</p><p>There is a problem with this kind of simple ad hoc stuff. I feel it’s too much provisioning a virtual server and a database for it. It’s not cool having to set up something in a remote environment when requirements are dead simple.</p><p>To ease these pains we considered Heroku which makes the setup and deploy straight forward, however given the 30-min inactivity policy of free dynos we dismissed the idea.</p><h3>Alternative #2: A serverless bot? YES!</h3><p>After explaining the bot idea to my friend <a href="https://github.com/alvivi">Alvivi</a> he hinted me about Google Apps Script. It turned out he used it before in combination with Google Docs acting as the persistence layer.</p><p>With Google Apps Script you can write a bunch of javascript functions and expose them as an endpoint, without worrying about setting up anything.</p><p>It sounds hacky but it works really well!</p><h3>The Recipe</h3><p>Google Apps Scripts and Spreadsheet is not enough for setting up the whole solution so here is the full recipe of what we need:</p><ul><li>1x Slack incoming webhook</li><li>1x Slack outgoing webhook</li><li>1x Google Spreadsheet</li><li>1x Google Apps Script</li></ul><h3>Implementation</h3><h4>Spreadsheet</h4><p>First of all we need to create a new spreadsheet in our Google account. Why would we need something like this? As we are not going to have a regular database to store the statuses of our staging servers we need something to persist this information. A spreadsheet is good enough!</p><p>We can give the spreadsheet the name we want. It doesn’t matter for next steps. The content of the spreadsheet could be something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/569/1*ubfbWPh8okL9dNN-RPGkrA.png" /></figure><p>Once we’ve our spreedsheet with this content we can save it and copy the ID of it. The ID can be found in the url.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/818/1*oI44ENVS8W98AcPOMu-ntQ.png" /><figcaption>Blurry area corresponds to the ID of the spreadsheet that you should copy.</figcaption></figure><h4>Set up Slack incoming webhook</h4><p>Setting up a Slack incoming webhook is really easy and it’s probably something you have already done before. You just have to go to Slack Apps directory and search for “incoming webhook”, create a new one and copy the <strong>Webhook URL</strong>. You can leave the rest of the settings as default since we’ll override some of them from our script.</p><h4>Script</h4><p>Visit <a href="https://script.google.com/">script.google.com</a>, click on <strong>New script</strong> and it’ll bring you to the Google Apps Script editor. There you just copy the content of the script located in this <a href="https://github.com/hopsor/bartolo/blob/master/Code.gs">github repository</a>. The script basically contains the logic to communicate with Slack and alter the content of the spreadsheet where we persist the statuses of all our staging environments.</p><p>The script itself isn’t something really sophisticated. I won’t explain it in detail but I think it’s worth to mention how Google Apps Script runs it. Given our script is gonna be considered a web application accessed by a specific url it can receive GET and POST requests. In our case, as we’ll use it in combination with Slack Outgoing Webhook which makes POST requests, it’ll need to implement the <strong>doPost</strong> function.</p><pre>function doPost(e){<br>  var commandReceived = e.parameter[&quot;text&quot;];<br><br>  if (commandReceived.match(/help/)) showHelp();<br>  if (commandReceived.match(/list/)) listStaging();<br>  if (commandReceived.match(/take/)) take(e);<br>  if (commandReceived.match(/leave/)) leave(e);<br>  if (commandReceived.match(/create/)) create(e);<br>  if (commandReceived.match(/remove/)) remove(e);<br>  if (commandReceived.match(/rename/)) rename(e);<br>}</pre><p>As you can see <strong>doPost</strong> function basically acts as a router of commands. It identifies which command we are using and then it executes the proper function to handle it.</p><p>Our script keeps record of all the events we send by logging them into a separate page of our spreadsheet. To facilitate this job we use Peter Herrmann’s library <a href="https://github.com/peterherrmann/BetterLog#setup">BetterLog</a>. Adding it to our project is quite straight forward following instructions in the readme.</p><p>Now we are going to expose a few properties we’ll need inside our script. We can do it by going to <em>Project Properties &gt; Script Properties</em> tab. We’ll add the following ones:</p><ul><li>SPREADSHEET_ID</li><li>SLACK_CHANNEL_NAME</li><li>SLACK_INCOMING_WEBHOOK</li></ul><p>Once we have done all the above we save the project and click on <em>Publish &gt; Deploy as web app</em>. Then a popup will show up. There we have to change the access to the app to <strong>anyone, even anonymous</strong>. If it’s the first time it’ll ask you to review some permissions to give the script the ability to alter the spreadsheet. After giving permissions the popup will generate our script url which we’ll save for using it later.</p><p>That should be all regarding Google Apps Script!</p><h4>Set up Slack outgoing webhook</h4><p>The last step is making possible for our Google Apps Script to listen slack channel commands. In fact our script won’t be actively listening. It’ll be responsability of Slack to keep it posted when someone sends a command starting by <strong>!staging</strong>.</p><p>That’s done by creating a Slack outgoing webhook. We have to go to the Slack Apps directory and search for “outgoing webhooks”. Then we create a new one with following settings:</p><ul><li>Channel: The channel we’ll send commands from.</li><li>Trigger Word(s): <strong>!staging</strong></li><li>URL(s): The URL generated Google Apps Scripts in the last step.</li></ul><p>Click on <strong>Save settings</strong> and we should be ready to go.</p><h3>Demo</h3><iframe src="https://cdn.embedly.com/widgets/media.html?url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D21-6YElN8ec&amp;src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F21-6YElN8ec%3Ffeature%3Doembed&amp;type=text%2Fhtml&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/855984df2426c9c7780140f201fec708/href">https://medium.com/media/855984df2426c9c7780140f201fec708/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=35bdac755a44" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>