<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Automation on Pablo Seminario</title><link>https://seminar.io/tags/automation/</link><description>Recent content in Automation on Pablo Seminario</description><generator>Hugo</generator><language>en</language><lastBuildDate>Mon, 03 Sep 2018 18:48:00 +0000</lastBuildDate><atom:link href="https://seminar.io/tags/automation/index.xml" rel="self" type="application/rss+xml"/><item><title>Building a serverless Telegram bot</title><link>https://seminar.io/posts/building-serverless-telegram-bot/</link><pubDate>Mon, 03 Sep 2018 18:48:00 +0000</pubDate><guid>https://seminar.io/posts/building-serverless-telegram-bot/</guid><description>&lt;p&gt;With the arrival of &lt;a href="https://en.wikipedia.org/wiki/Function_as_a_service"&gt;FaaS&lt;/a&gt; and all the serverless infrastructure providers (AWS, Google Cloud, Azure, etc) now is easy to deploy our own Telegram bot.&lt;/p&gt;&#10;&lt;p&gt;A Webhook-based bot it&amp;rsquo;s the perfect use case for serverless computing, using for example Google Cloud Functions we&amp;rsquo;ll have all these advantages:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Our function will be associated to a public endpoint (IPv4 and IPv6) over HTTPS (using a valid certificate provided by Google)&lt;/li&gt;&#10;&lt;li&gt;Pay only for function invocations and its compute resources consumption, so it could be cheaper than paying for a server powered on all the time&lt;/li&gt;&#10;&lt;li&gt;No server management&lt;/li&gt;&#10;&lt;li&gt;Scales automatically&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h4 id="requirements"&gt;Requirements&lt;/h4&gt;&#10;&lt;p&gt;Before start building your bot you&amp;rsquo;ll need to:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Register a new bot following these instructions &lt;a href="https://core.telegram.org/bots#3-how-do-i-create-a-bot"&gt;core.telegram.org/bots&lt;/a&gt; in order to get an API token&lt;/li&gt;&#10;&lt;li&gt;Using a Google Cloud account, register a new project, enable the Cloud Functions API and install the Cloud SDK following &lt;a href="https://cloud.google.com/functions/docs/quickstart"&gt;the official quickstart&lt;/a&gt;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h4 id="code"&gt;Code&lt;/h4&gt;&#10;&lt;p&gt;In a few lines of Python you can code a simple echo bot that will reply with the same message that it receives:&lt;/p&gt;&#10;&#10;&#10;&#10;&#10;&lt;div class="code-block"&gt;&#10; &lt;div class="code-block__bar"&gt;&#10; &lt;span&gt;text&lt;/span&gt;&#10; &lt;span&gt;text&lt;/span&gt;&#10; &lt;/div&gt;&#10; &lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;# main.py&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;import os&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;import telegram&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;def webhook(request):&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; bot = telegram.Bot(token=os.environ[&amp;#34;TELEGRAM_TOKEN&amp;#34;])&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; if request.method == &amp;#34;POST&amp;#34;:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; update = telegram.Update.de_json(request.get_json(force=True), bot)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; chat_id = update.message.chat.id&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; # Reply with the same message&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; bot.sendMessage(chat_id=chat_id, text=update.message.text)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; return &amp;#34;ok&amp;#34;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;p&gt;The only dependency needed is the amazing &lt;a href="https://python-telegram-bot.org/"&gt;python-telegram-bot&lt;/a&gt; library:&lt;/p&gt;&#10;&#10;&#10;&#10;&#10;&lt;div class="code-block"&gt;&#10; &lt;div class="code-block__bar"&gt;&#10; &lt;span&gt;text&lt;/span&gt;&#10; &lt;span&gt;text&lt;/span&gt;&#10; &lt;/div&gt;&#10; &lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;# requirements.txt &#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;python-telegram-bot==11.1.0&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;/div&gt;</description></item></channel></rss>