<?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: Fox Larsson</title>
    <description>The latest articles on DEV Community by Fox Larsson (@foxlarsson).</description>
    <link>https://dev.to/foxlarsson</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%2F340488%2F00ea8f0e-1e94-441e-9229-d3f4c37066be.jpeg</url>
      <title>DEV Community: Fox Larsson</title>
      <link>https://dev.to/foxlarsson</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/foxlarsson"/>
    <language>en</language>
    <item>
      <title>My first Python Telegram bot — and some tips if you want to follow the same tutorial</title>
      <dc:creator>Fox Larsson</dc:creator>
      <pubDate>Mon, 24 Feb 2020 08:52:49 +0000</pubDate>
      <link>https://dev.to/foxlarsson/my-first-python-telegram-bot-and-some-tips-if-you-want-to-follow-the-same-tutorial-1il3</link>
      <guid>https://dev.to/foxlarsson/my-first-python-telegram-bot-and-some-tips-if-you-want-to-follow-the-same-tutorial-1il3</guid>
      <description>&lt;p&gt;Woot-woopety-woot! I just finished writing my first Python Telegram bot!&lt;/p&gt;

&lt;p&gt;Thanks a bunch to &lt;a href="https://www.freecodecamp.org/news/learn-to-build-your-first-bot-in-telegram-with-python-4c99526765e4/"&gt;Dzaky Widya Putra&lt;/a&gt; for the tutorial that actually gave me the confidence (and detailed instructions) to give the thing a shot - and to my phone's AI for recommending it to me.&lt;/p&gt;

&lt;p&gt;The bot is a simple one: it takes one command, "/boop", and returns a random cute doggo pic. The source of the images is the &lt;a href="https://random.dog/"&gt;Random Dog API&lt;/a&gt;, and the bot works through the &lt;a href="https://github.com/python-telegram-bot/python-telegram-bot"&gt;python-telegram-bot library&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I've been studying Python for two and a half weeks now, and all this time I was following the Hyperskill.org curriculum. So far I've completed all their beginner-level projects. So I have a bit of a basic understanding of Python, but no fancy skills.&lt;/p&gt;

&lt;p&gt;I was worried that the chatbot project would be out of my scope since I hadn't used external libraries or APIs before. But after the &lt;a href="https://github.com/python-telegram-bot/python-telegram-bot/wiki/Extensions-%E2%80%93-Your-first-Bot"&gt;official library tutorial&lt;/a&gt; and a bit of anxiety and trepidation, I found out that it's more fun than terrifying (with a pinch of debugging frustration).&lt;/p&gt;

&lt;p&gt;You can check out my bot &lt;a href="https://t.me/radodog_bot"&gt;here&lt;/a&gt; - however, it currently works only when I launch the program on my computer. Deploying it so it can run continuously is my next plan (maybe Heroku?), I'm really curious about to do that.&lt;/p&gt;

&lt;h3&gt;
  
  
  If you want to try it yourself:
&lt;/h3&gt;

&lt;p&gt;I think Dzaky's &lt;a href="https://www.freecodecamp.org/news/learn-to-build-your-first-bot-in-telegram-with-python-4c99526765e4/"&gt;tutorial&lt;/a&gt; does a great job of getting you through the process. There were a few small things that inadvertently made me stumble, so I decided to share them here and explain.&lt;/p&gt;

&lt;h4&gt;
  
  
  Don't forget to install the requests library
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://pypi.org/project/requests/2.7.0/"&gt;Requests&lt;/a&gt; is not a built-in module in Python. This means you can't just import it, you have to install in first. Dzaky's tutorial mentions installing the python-telegram-bot library separately, but not requests, so it took me a while to figure out why things were not working, and I did spend some time half blankly staring at my code doing nothing. As you might imagine, reading the error message helped.&lt;/p&gt;

&lt;h4&gt;
  
  
  Pay attention to the Updater arguments
&lt;/h4&gt;

&lt;p&gt;In the tutorial when you create your instance of the Updater class, it goes like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;updater = Updater('YOUR_TOKEN')
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;For me, this threw a Deprecated error. As far as I understand, in the latest version of the library you also need to set use_context to True, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;updater = Updater('TOKEN', use_context=True)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  If the tutorial's bop function is not working for you check the version from Github
&lt;/h4&gt;

&lt;p&gt;When I was through with the first stage of the tutorial I fired up my bot, sent the command in Telegram and... nothing happened. Not even a hint of a dog pic.&lt;/p&gt;

&lt;p&gt;I thought maybe I missed something, copied and pasted the sample code from the tutorial, tried again, and still - nothing.&lt;/p&gt;

&lt;p&gt;I copied the code from the project's Github, and immediately booped a cute doggo into existence.&lt;/p&gt;

&lt;p&gt;If the same happens to you, here's what I figured out after comparing the two version's line by line. Take a look at the bop function from the tutorial and the same block from the Github version:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def bop(bot, update):
    url = get_image_url()
    chat_id = update.message.chat_id
    bot.send_photo(chat_id=chat_id, photo=url)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def bop(update, context):
    url = get_image_url()
    chat_id = update.message.chat_id
    context.bot.send_photo(chat_id=chat_id, photo=url)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The Github version uses the new context-based syntax, that's why this block works, and the other one doesn't.&lt;/p&gt;

&lt;h4&gt;
  
  
  I think this is a great project to take your first look at Telegram bots
&lt;/h4&gt;

&lt;p&gt;I hope you find those tips helpful and they save you a bit of frustration and doubt, although those two are really part of the learning process. =) For me, the project was fun, manageable, and an inspiration to dig deeper. And I'm not even going to deny that I went down a rabbit hole of booping and dog adoration (and being pleased with myself) when my code worked. It's a good thing my own dogster was sleeping, or he would have been really jealous.&lt;/p&gt;

&lt;p&gt;If there's something you don't understand, all the errors are google-able, and if you want an extra challenge, try doing the same, but with &lt;a href="https://rapidapi.com/lilbbytrimble/api/random-cat-gif"&gt;cat gifs&lt;/a&gt;, once you've figured out the basics.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jmg0BDG_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://artfox.dev/wp-content/uploads/2020/02/sleeping_doggo-min.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jmg0BDG_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://artfox.dev/wp-content/uploads/2020/02/sleeping_doggo-min.png" alt="A picture of my dog sleeping beside me while I work."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This could have been generated by the bot, but nope, it's my very own sleeping doggo.&lt;/p&gt;

&lt;p&gt;Good luck in exploring Telegram bots, they're super fun! And a big thanks to Dzaky for the original tutorial.&lt;/p&gt;

&lt;p&gt;Thank you for reading, this post was originally published on &lt;a href="https://artfox.dev/my-first-python-telegram-bot"&gt;ArtFox.Dev&lt;/a&gt; on 24.02.2020.&lt;/p&gt;

&lt;p&gt;P.S.: this is my first dev.to post, and I'm super anxious =)&lt;/p&gt;

</description>
      <category>python</category>
      <category>chatbot</category>
      <category>telegram</category>
    </item>
  </channel>
</rss>
