<?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: Codédex</title>
    <description>The latest articles on DEV Community by Codédex (@codedex).</description>
    <link>https://dev.to/codedex</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%2Forganization%2Fprofile_image%2F5909%2Fa4f43064-b4f0-498d-ad0f-eb0c998efe35.png</url>
      <title>DEV Community: Codédex</title>
      <link>https://dev.to/codedex</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/codedex"/>
    <language>en</language>
    <item>
      <title>Monthly Challenge #4: Valentine's Day 💘</title>
      <dc:creator>Sonny Li</dc:creator>
      <pubDate>Thu, 09 Feb 2023 19:08:57 +0000</pubDate>
      <link>https://dev.to/codedex/monthly-challenge-4-valentines-day-2o1g</link>
      <guid>https://dev.to/codedex/monthly-challenge-4-valentines-day-2o1g</guid>
      <description>&lt;p&gt;What better way to celebrate Valentine's Day than by sharing a poem with your friends and family? In this challenge, you will create a poem on a web page using HTML. 💌&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Requirements:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use a combination of at least five common HTML elements, such as headings, paragraphs, lists, and text formatting.&lt;/li&gt;
&lt;li&gt;The poem can take on any form you like! You can freestyle with as many lines as your heart desires, but here are some suggestions for common poem types:

&lt;ul&gt;
&lt;li&gt;Sonnet (14 lines)&lt;/li&gt;
&lt;li&gt;Haiku (3 lines; 5 syllable, 3 syllables, 5 syllables, respectively)&lt;/li&gt;
&lt;li&gt;Limerick (5 lines; rhyme scheme AABAB)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Bonus:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Make your Valentine's Day poem as pretty or complex in style (background color, border, etc.) by doing one of the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use a &lt;code&gt;&amp;lt;style&amp;gt;&lt;/code&gt; element in the &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; of the document.&lt;/li&gt;
&lt;li&gt;Apply a global style attribute to various elements in the &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt; of the document.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="//www.codedex.io/html"&gt;Try our free course&lt;/a&gt; if you need a quick refresher on HTML!&lt;/p&gt;

&lt;p&gt;Submit a screenshot of your web page by Feb 14th for a chance to win a special surprise! If you want, you can tag the person the valentine is for so they can see it.&lt;/p&gt;

&lt;p&gt;May the best poet win. 💕&lt;/p&gt;

</description>
      <category>challenge</category>
      <category>html</category>
      <category>beginners</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Monthly Challenge #3: Morse Code 🆘</title>
      <dc:creator>Sonny Li</dc:creator>
      <pubDate>Mon, 02 Jan 2023 22:06:09 +0000</pubDate>
      <link>https://dev.to/codedex/monthly-challenge-3-morse-code-4d97</link>
      <guid>https://dev.to/codedex/monthly-challenge-3-morse-code-4d97</guid>
      <description>&lt;h3&gt;
  
  
  Morse Code
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Morse_code" rel="noopener noreferrer"&gt;Morse code&lt;/a&gt; is a communication method to encode characters as dots (.) and dashes (-), developed in the 1830s.&lt;/p&gt;

&lt;h3&gt;
  
  
  January Challenge
&lt;/h3&gt;

&lt;p&gt;Create a &lt;code&gt;translate()&lt;/code&gt; Python function that takes in a message and converts it to morse code. &lt;/p&gt;

&lt;p&gt;Post the code/output screenshot in the Twitter thread by Jan 14th for a chance to win a Codédex hoodie! 🎁&lt;/p&gt;

&lt;p&gt;&lt;iframe class="tweet-embed" id="tweet-1609910099824496647-936" src="https://platform.twitter.com/embed/Tweet.html?id=1609910099824496647"&gt;
&lt;/iframe&gt;

  // Detect dark theme
  var iframe = document.getElementById('tweet-1609910099824496647-936');
  if (document.body.className.includes('dark-theme')) {
    iframe.src = "https://platform.twitter.com/embed/Tweet.html?id=1609910099824496647&amp;amp;theme=dark"
  }



&lt;/p&gt;

</description>
      <category>devmeme</category>
      <category>watercooler</category>
    </item>
    <item>
      <title>Monthly Challenge #2: Holiday ASCII Art ️ ️☃️</title>
      <dc:creator>Sonny Li</dc:creator>
      <pubDate>Thu, 01 Dec 2022 18:10:39 +0000</pubDate>
      <link>https://dev.to/codedex/monthly-twitter-challenge-holiday-ascii-art-lbl</link>
      <guid>https://dev.to/codedex/monthly-twitter-challenge-holiday-ascii-art-lbl</guid>
      <description>&lt;p&gt;Welcome to Codédex's second Monthly Twitter Challenge! This one is also created by our SWE Intern, &lt;a class="mentioned-user" href="https://dev.to/asiqurrahman"&gt;@asiqurrahman&lt;/a&gt;. 🙌&lt;/p&gt;

&lt;p&gt;&lt;iframe class="tweet-embed" id="tweet-1598365914134679552-665" src="https://platform.twitter.com/embed/Tweet.html?id=1598365914134679552"&gt;
&lt;/iframe&gt;

  // Detect dark theme
  var iframe = document.getElementById('tweet-1598365914134679552-665');
  if (document.body.className.includes('dark-theme')) {
    iframe.src = "https://platform.twitter.com/embed/Tweet.html?id=1598365914134679552&amp;amp;theme=dark"
  }



&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ASCII Art:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/ASCII_art" rel="noopener noreferrer"&gt;ASCII art&lt;/a&gt; is a computer graphic design technique that piece together a picture using characters defined by the ASCII Standard from 1963.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Challenge:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Using your coding language of choice, create a holiday-themed ASCII art in the terminal. Be as creative as you can. And it has to be original! 🎅🏼🎄☃️❄️&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fleb1hye3yp0krwaizl0i.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fleb1hye3yp0krwaizl0i.gif" alt="GIF" width="529" height="411"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Post your code &amp;amp; screenshot below by Dec 14th for a chance to win a limited edition Codédex sweater mailed to your door by Christmas morning! 🎁&lt;/p&gt;

</description>
      <category>programming</category>
      <category>challenge</category>
      <category>beginners</category>
      <category>codedex</category>
    </item>
    <item>
      <title>Codédex Launch on Product Hunt! 🚀</title>
      <dc:creator>Sonny Li</dc:creator>
      <pubDate>Mon, 07 Nov 2022 12:01:08 +0000</pubDate>
      <link>https://dev.to/codedex/codedex-launch-on-product-hunt-13o8</link>
      <guid>https://dev.to/codedex/codedex-launch-on-product-hunt-13o8</guid>
      <description>&lt;p&gt;Hi Devs!&lt;/p&gt;

&lt;p&gt;It’s Sonny from &lt;a href="https://www.codedex.io"&gt;Codédex&lt;/a&gt; –– a brand new learn to code platform for Gen Z. &lt;/p&gt;

&lt;p&gt;We’re a small-but-mighty team based in Brooklyn, NY but have teammates across the globe.&lt;/p&gt;

&lt;p&gt;Since June 2022, we’ve gone full in on our mission to empower the next generation of builders and dreamers. And today, we are finally launching to the world on Product Hunt — please support us!&lt;/p&gt;

&lt;p&gt;Head over to Product Hunt to give us an upvote! We are aiming for the Top 5 Products of the Day and it would mean the world if you can give us 30 seconds of your time.&lt;/p&gt;

&lt;p&gt;⬆️ &lt;a href="https://www.producthunt.com/posts/codedex"&gt;https://www.producthunt.com/posts/codedex&lt;/a&gt; ⬆️&lt;/p&gt;

&lt;p&gt;Here's a preview of the site:&lt;/p&gt;

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

&lt;p&gt;Lastly, huge shoutout out to the whole team: Dharma, Malcolm, Asiqur, Jerry, Rose, and Lil for working on this night and day for the past few months. LFG team!&lt;/p&gt;

&lt;p&gt;Happy coding,&lt;br&gt;
Sonny 🤠&lt;/p&gt;

</description>
      <category>news</category>
      <category>startup</category>
      <category>codenewbie</category>
      <category>python</category>
    </item>
    <item>
      <title>How do programmers say "I love you"? 🤔</title>
      <dc:creator>Sonny Li</dc:creator>
      <pubDate>Thu, 03 Nov 2022 16:04:42 +0000</pubDate>
      <link>https://dev.to/codedex/how-do-programmers-say-i-love-you-4e27</link>
      <guid>https://dev.to/codedex/how-do-programmers-say-i-love-you-4e27</guid>
      <description>&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; WRONG answers only! &lt;/p&gt;

&lt;p&gt;Feel free to reply in the comments or in the Twitter thread:&lt;/p&gt;


&lt;blockquote class="ltag__twitter-tweet"&gt;

  &lt;div class="ltag__twitter-tweet__main"&gt;
    &lt;div class="ltag__twitter-tweet__header"&gt;
      &lt;img class="ltag__twitter-tweet__profile-image" src="https://res.cloudinary.com/practicaldev/image/fetch/s--dH3PEO97--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://pbs.twimg.com/profile_images/1523772807544512513/C-ckSsu3_normal.jpg" alt="Codédex profile image"&gt;
      &lt;div class="ltag__twitter-tweet__full-name"&gt;
        Codédex
      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__username"&gt;
        @codedex_io
      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__twitter-logo"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ir1kO05j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/twitter-f95605061196010f91e64806688390eb1a4dbc9e913682e043eb8b1e06ca484f.svg" alt="twitter logo"&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__body"&gt;
      How do programmers say "I love you"? 🤔&lt;br&gt;Wrong answers only.
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__date"&gt;
      00:16 AM - 03 Nov 2022
    &lt;/div&gt;


    &lt;div class="ltag__twitter-tweet__actions"&gt;
      &lt;a href="https://twitter.com/intent/tweet?in_reply_to=1587961941359730688" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fFnoeFxk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/twitter-reply-action-238fe0a37991706a6880ed13941c3efd6b371e4aefe288fe8e0db85250708bc4.svg" alt="Twitter reply action"&gt;
      &lt;/a&gt;
      &lt;a href="https://twitter.com/intent/retweet?tweet_id=1587961941359730688" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--k6dcrOn8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/twitter-retweet-action-632c83532a4e7de573c5c08dbb090ee18b348b13e2793175fea914827bc42046.svg" alt="Twitter retweet action"&gt;
      &lt;/a&gt;
      &lt;a href="https://twitter.com/intent/like?tweet_id=1587961941359730688" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SRQc9lOp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/twitter-like-action-1ea89f4b87c7d37465b0eb78d51fcb7fe6c03a089805d7ea014ba71365be5171.svg" alt="Twitter like action"&gt;
      &lt;/a&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/blockquote&gt;


&lt;p&gt;That's all! Happy Thursday, Devs! 💖&lt;/p&gt;

</description>
      <category>programming</category>
      <category>codenewbie</category>
      <category>codedex</category>
    </item>
    <item>
      <title>Monthly Challenge #1: Palindrome 🔤</title>
      <dc:creator>Sonny Li</dc:creator>
      <pubDate>Tue, 01 Nov 2022 16:39:08 +0000</pubDate>
      <link>https://dev.to/codedex/monthly-twitter-challenge-1-palindrome-3hg6</link>
      <guid>https://dev.to/codedex/monthly-twitter-challenge-1-palindrome-3hg6</guid>
      <description>&lt;p&gt;Welcome to Codédex's first ever Monthly Twitter Challenge! This one is created by our SWE Intern, &lt;a class="mentioned-user" href="https://dev.to/asiqurrahman"&gt;@asiqurrahman&lt;/a&gt;. 🙌&lt;/p&gt;

&lt;p&gt;&lt;iframe class="tweet-embed" id="tweet-1587072993448058883-356" src="https://platform.twitter.com/embed/Tweet.html?id=1587072993448058883"&gt;
&lt;/iframe&gt;

  // Detect dark theme
  var iframe = document.getElementById('tweet-1587072993448058883-356');
  if (document.body.className.includes('dark-theme')) {
    iframe.src = "https://platform.twitter.com/embed/Tweet.html?id=1587072993448058883&amp;amp;theme=dark"
  }



&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Palindrome:&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;A word, sentence, or number that reads the same forward or backward (e.g., &lt;em&gt;racecar&lt;/em&gt;, &lt;em&gt;civic&lt;/em&gt;, and &lt;em&gt;121&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Challenge:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Create a Python function that:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Takes in a string as a parameter.&lt;/li&gt;
&lt;li&gt;Checks if it's a palindrome.&lt;/li&gt;
&lt;li&gt;Returns &lt;code&gt;True&lt;/code&gt; if it is or &lt;code&gt;False&lt;/code&gt; if it's not.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Requirements:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;📦 Don’t use any packages.&lt;/li&gt;
&lt;li&gt;💬 Make sure to comment the code.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Post your program screenshot in the &lt;a href="https://twitter.com/codedex_io/status/1587072993448058883"&gt;Twitter thread&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;And we will pick a random winner by noon this Friday. The prize is a free &lt;a href="https://codedex.myshopify.com"&gt;Codédex tee&lt;/a&gt; and bragging rights! 🏆&lt;/p&gt;




&lt;p&gt;&lt;em&gt;&lt;strong&gt;Update:&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;We have a winner to the Codédex Monthly Challenge: Palindrome! 🥁...&lt;/p&gt;

&lt;p&gt;Congrats to Michael Gonzalez! 🇪🇨 &lt;/p&gt;

&lt;p&gt;The reasons being:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Solution covers string &amp;amp; number&lt;/li&gt;
&lt;li&gt;Good comments&lt;/li&gt;
&lt;li&gt;Good breakdown of the functions&lt;/li&gt;
&lt;li&gt;First to post bonus&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's the solution: &lt;/p&gt;

&lt;p&gt;&lt;iframe class="tweet-embed" id="tweet-1587105952607703040-740" src="https://platform.twitter.com/embed/Tweet.html?id=1587105952607703040"&gt;
&lt;/iframe&gt;

  // Detect dark theme
  var iframe = document.getElementById('tweet-1587105952607703040-740');
  if (document.body.className.includes('dark-theme')) {
    iframe.src = "https://platform.twitter.com/embed/Tweet.html?id=1587105952607703040&amp;amp;theme=dark"
  }



&lt;/p&gt;

</description>
      <category>python</category>
      <category>challenge</category>
      <category>beginners</category>
      <category>codedex</category>
    </item>
    <item>
      <title>Generate a Blog with OpenAI</title>
      <dc:creator>Asiqur Rahman</dc:creator>
      <pubDate>Wed, 19 Oct 2022 20:06:42 +0000</pubDate>
      <link>https://dev.to/codedex/generate-a-blog-with-openai-5eio</link>
      <guid>https://dev.to/codedex/generate-a-blog-with-openai-5eio</guid>
      <description>&lt;p&gt;&lt;strong&gt;Prerequisites:&lt;/strong&gt; Python fundamentals&lt;br&gt;
&lt;strong&gt;Versions:&lt;/strong&gt; Python 3.10, python-dotenv 0.21.0, openai 0.23.0&lt;br&gt;
&lt;strong&gt;Read Time:&lt;/strong&gt; 60 minutes&lt;/p&gt;
&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Artificial_intelligence" rel="noopener noreferrer"&gt;Artificial Intelligence (AI)&lt;/a&gt; is becoming the next big technology to harness. From smart fridges to self-driving cars, AI is implemented in almost everything you can think of. So let's get ahead of the pack and learn how we can leverage the power of AI with Python and OpenAI.&lt;/p&gt;

&lt;p&gt;In this tutorial, we'll learn how to create a blog generator with &lt;a href="https://openai.com/api/" rel="noopener noreferrer"&gt;GPT-3&lt;/a&gt;, an AI model provided by &lt;a href="https://www.openai.com" rel="noopener noreferrer"&gt;OpenAI&lt;/a&gt;. The generator will read a topic to talk about as the input, and GPT-3 will return us a paragraph about that topic as the output. &lt;/p&gt;

&lt;p&gt;So AI will be "writing" stuff for us. Say goodbye to writer's block!&lt;/p&gt;

&lt;p&gt;But wait, hold on! Artificial intelligence?! AI models?! This must be complicated to code. 😵&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fcodedex-io%2Fprojects%2Fmain%2Fprojects%2Fgenerate-a-blog-with-openai%2Fcalculation-math.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fcodedex-io%2Fprojects%2Fmain%2Fprojects%2Fgenerate-a-blog-with-openai%2Fcalculation-math.gif" alt="meme"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Nope, it's easier than you think. It takes around 25 lines of Python code!&lt;/p&gt;

&lt;p&gt;The final result will look something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fcodedex-io%2Fprojects%2Fmain%2Fprojects%2Fgenerate-a-blog-with-openai%2Fgenerator-demo.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fcodedex-io%2Fprojects%2Fmain%2Fprojects%2Fgenerate-a-blog-with-openai%2Fgenerator-demo.gif" alt="generator demo"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Who knows, maybe this entire project was written by the generator we're about to create. 👀&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  What is GPT-3?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/GPT-3" rel="noopener noreferrer"&gt;GPT-3&lt;/a&gt; is an AI model released by OpenAI in 2020. An AI model is a program trained on a bunch of data to perform a specific task. In this case, GPT-3 was trained to speak like a human and predict what comes next given the context of a sentence, with its training dataset being 45 terabytes of text (!) from the internet. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;For reference, if you had to keep writing until your paper hits 45 terabytes in size, you would have to write &lt;a href="https://www.techtarget.com/searchstorage/definition/How-many-bytes-for" rel="noopener noreferrer"&gt;22,500,000,000&lt;/a&gt; pages worth of plain text. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Since GPT-3 was trained on internet data, it knows what the internet knows (not everything of course). This means that if we were to give GPT-3 a sentence, it would be able to predict what comes next in that sentence with high accuracy, based on all the text that was used to train it.&lt;/p&gt;

&lt;p&gt;Now we know what we'll be working with, let's build the program!&lt;/p&gt;
&lt;h2&gt;
  
  
  Setting Up
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;OpenAI Account&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Before we do anything, we need an &lt;a href="https://openai.com/api" rel="noopener noreferrer"&gt;OpenAI&lt;/a&gt; account. We'll need this account access to an API key that we can use to work with GPT-3.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/API" rel="noopener noreferrer"&gt;API (Application Programming Interface)&lt;/a&gt; is a way for two computers to communicate with each other. Think of it like two friends texting back and forth. An API key is a code we receive to access the API. Think of it like an important password, so don’t share it with others!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Go to &lt;a href="http://www.openai.com" rel="noopener noreferrer"&gt;www.openai.com&lt;/a&gt; and sign up for an OpenAI account.&lt;/p&gt;

&lt;p&gt;After you've created an account, click on your profile picture on the top right, then click "View API keys" to access your API key. You should see &lt;a href="https://beta.openai.com/account/api-keys" rel="noopener noreferrer"&gt;this page&lt;/a&gt; and it should look like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fcodedex-io%2Fprojects%2Fmain%2Fprojects%2Fgenerate-a-blog-with-openai%2Fapi-key.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fcodedex-io%2Fprojects%2Fmain%2Fprojects%2Fgenerate-a-blog-with-openai%2Fapi-key.png" alt="API Key"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that we know where the API key is located, let's keep it in mind for later.&lt;/p&gt;

&lt;p&gt;With the API key, we get access to GPT-3 and \$18 worth of free credit. Meaning that we can use GPT-3 for free until we go over the \$18, which is more than enough to complete this project.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python Setup&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For this project, we'll need &lt;a href="https://www.python.org/downloads/" rel="noopener noreferrer"&gt;Python 3&lt;/a&gt; and &lt;a href="https://pip.pypa.io/en/stable/" rel="noopener noreferrer"&gt;pip&lt;/a&gt; (package installer) installed.&lt;/p&gt;

&lt;p&gt;Assuming that we have those two installed, let's open up the code editor of our choice (we recommend &lt;a href="https://code.visualstudio.com" rel="noopener noreferrer"&gt;VS Code&lt;/a&gt;) and create a new file called &lt;strong&gt;blog_generator.py&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: You can name this file anything except for &lt;strong&gt;openai.py&lt;/strong&gt;, since the name will clash with a package we'll be installing.&lt;/p&gt;
&lt;h2&gt;
  
  
  Beginning the Project
&lt;/h2&gt;

&lt;p&gt;At the core of this project, all we'll be doing is sending data with instructions to a server owned by OpenAI, then receiving a response back from that server and displaying it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Install openai&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We'll be interacting with GPT-3 model using a python package called &lt;code&gt;openai&lt;/code&gt;. This package consists of methods that can connect to the internet and grant us access to the GPT-3 model hosted by OpenAI, the company.&lt;/p&gt;

&lt;p&gt;To install &lt;code&gt;openai&lt;/code&gt;, all we have to do is run the following command in our terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;openai
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can now use this package by importing it into our &lt;strong&gt;blog_generator.py&lt;/strong&gt; file like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;openai&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Authorize API Key&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Before we can work with GPT-3 we need to set our API key in the &lt;code&gt;openai&lt;/code&gt; module. Remember, the API key is what gives us access to GPT-3; it authorizes us and says we're allowed to use this API.&lt;/p&gt;

&lt;p&gt;We can set our API key by extending a method in the &lt;code&gt;openai&lt;/code&gt; module called &lt;code&gt;api_key&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;openai&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;api_key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Your_API_Key&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The method will take in the API key as a string. Remember, your API key is located in your &lt;a href="https://beta.openai.com/account/api-keys" rel="noopener noreferrer"&gt;OpenAI account&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;So far, the code should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;openai&lt;/span&gt;

&lt;span class="n"&gt;openai&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;api_key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;sk-jAjqdWoqZLGsh7nXf5i8T3BlbkFJ9CYRk&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;# Fill in your own key
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The Core Function
&lt;/h2&gt;

&lt;p&gt;Now that we have access to GPT-3, we can get to the meat of the application, which is creating a function that takes in a prompt as user input and returns a paragraph about that prompt. &lt;/p&gt;

&lt;p&gt;That function will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;generate_blog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;paragraph_topic&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;openai&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Completion&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;text-davinci-002&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;prompt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Write a paragraph about the following topic. &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;paragraph_topic&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;max_tokens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;temperature&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.3&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="n"&gt;retrieve_blog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&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="n"&gt;text&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;retrieve_blog&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's break down this function and see what's going on here.&lt;/p&gt;

&lt;p&gt;First, we defined a function called &lt;code&gt;generate_blog()&lt;/code&gt;. There's a single parameter called &lt;code&gt;paragraph_topic&lt;/code&gt;, which will be the topic used to generate the paragraph:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;generate_blog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;paragraph_topic&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="c1"&gt;# The code inside
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And let's go inside the function. Here's the first part:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;generate_blog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;paragraph_topic&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;openai&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Completion&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;text-davinci-002&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;prompt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Write a paragraph about the following topic. &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;paragraph_topic&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;max_tokens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;temperature&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.3&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the bulk of our function and where we use GPT-3. We created a variable called &lt;code&gt;response&lt;/code&gt; to store the response generated by the output of the &lt;code&gt;Completion.create()&lt;/code&gt; method call in our &lt;code&gt;openai&lt;/code&gt; module. &lt;/p&gt;

&lt;p&gt;GPT-3 has different endpoints for specific purposes, but for our goal, we'll use the &lt;a href="https://beta.openai.com/docs/api-reference/completions" rel="noopener noreferrer"&gt;completion&lt;/a&gt; endpoint. The completion endpoint will generate text depending on the provided prompt. You can read about the different endpoints in the &lt;a href="https://beta.openai.com/docs/introduction" rel="noopener noreferrer"&gt;documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now that we have access to the completion endpoint, we need to specify a few things, The first one being:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;model&lt;/code&gt;: The model parameter will take in the model we want to use. GPT-3 has four models that we can use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;text-davinci-002&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;text-curie-001&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;text-babbage-001&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;text-ada-001&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These models perform the same task but at a different power level. More power equals better and more coherent responses, with &lt;code&gt;text-davinci-002&lt;/code&gt; being the most powerful and &lt;code&gt;text-babbage-001&lt;/code&gt; being the least. You can think of it like a car vs. a bike. They both perform the same task of taking you from one place to another, but the car will perform better. You can read more about the models in the &lt;a href="https://beta.openai.com/docs/models/gpt-3" rel="noopener noreferrer"&gt;documentation&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;prompt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Write a paragraph about the following topic. &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;paragraph_topic&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;prompt&lt;/code&gt;: This is where we design the main instructions for GPT-3. This parameter will take in our &lt;code&gt;paragraph_topic&lt;/code&gt; argument, but before that, we can tell GPT-3 what to do with that argument. Currently, we are instructing GPT-3 to &lt;code&gt;Write a paragraph about the following topic&lt;/code&gt;. GPT-3 will try its best to follow this instruction and return us a paragraph. &lt;/p&gt;

&lt;p&gt;GPT-3 is very flexible; if the initial string is changed to &lt;code&gt;Write a blog outline about the following topic&lt;/code&gt;, it will give us an outline instead of a normal paragraph. You can later play around with this by telling the model exactly what it should generate and seeing what interesting responses you get.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;max_tokens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;400&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;tokens&lt;/code&gt;: The token number decides how long the response is going to be. A larger token number will produce a longer response. By setting a specific number, we're saying that the response can't go past this token size. The way tokens are counted towards a response is a bit complex, but you can read this &lt;a href="https://help.openai.com/en/articles/4936856-what-are-tokens-and-how-to-count-them" rel="noopener noreferrer"&gt;article&lt;/a&gt; by OpenAI that explains how token size is calculated.&lt;/p&gt;

&lt;p&gt;Roughly 75 words is about 100 tokens. A paragraph has 300 words on average. So, 400 tokens is about the length of a normal paragraph. The model &lt;code&gt;text-davinci-002&lt;/code&gt; has a token limit of 4,000.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;temperature&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;temperature&lt;/code&gt;: Temperature determines the randomness of a response. A higher temperature will produce a more creative response, while a lower temperature will produce a more well-defined response.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;0&lt;/code&gt;: The same response every time.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;1&lt;/code&gt;: A different response every time, even if it's the same prompt.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are plenty of other fields that we can specify to fine-tune the model even more, which you can read in the &lt;a href="https://beta.openai.com/docs/api-reference/completions/create" rel="noopener noreferrer"&gt;documentation&lt;/a&gt;, but for now, these are the four fields we need to concern ourselves with.&lt;/p&gt;

&lt;p&gt;Now that we have our model setup, we can run our function, and the following things will happen:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First, the &lt;code&gt;openai&lt;/code&gt; module will take our API key, along with the fields we specified in the &lt;code&gt;response&lt;/code&gt; variable, and make a request to the completion endpoint.&lt;/li&gt;
&lt;li&gt;OpenAI will then verify that we're allowed to use GPT-3 by verifying our API key.&lt;/li&gt;
&lt;li&gt;After verification, GPT-3 will use the specified fields to produce a response.&lt;/li&gt;
&lt;li&gt;The produced response will be returned back in the form of an object and stored in the &lt;code&gt;response&lt;/code&gt; variable.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That returned object will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "logprobs": null,
      "text": "\n\nPython is a programming language with many features, such as an intuitive syntax and powerful data structures. It was created in the late 1980s by Guido van Rossum, with the goal of providing a simple yet powerful scripting language. Python has since become one of the most popular programming languages, with a wide range of applications in fields such as web development, scientific computing, and artificial intelligence."
    }
  ],
  "created": 1664302504,
  "id": "cmpl-5v9OiMOjRyoyypRQWAdpyAtjtgVev",
  "model": "text-davinci-002",
  "object": "text_completion",
  "usage": {
    "completion_tokens": 80,
    "prompt_tokens": 19,
    "total_tokens": 99
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We’re provided with tons of information about the response, but the only thing we care about is the &lt;code&gt;text&lt;/code&gt; field containing generated text.&lt;/p&gt;

&lt;p&gt;We can access the value in the &lt;code&gt;text&lt;/code&gt; field like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;retrieve_blog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&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="n"&gt;text&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, we return the &lt;code&gt;retrieve_blog&lt;/code&gt; variable which holds the paragraph we just dug out of the dictionary.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;retrieve_blog&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Whoah! Let's take a moment and breathe. That was a lot we just covered. Let's give ourselves a pat on the back as we're 90% done with the application.&lt;/p&gt;

&lt;p&gt;We can test to see if our code works so far by printing out the &lt;code&gt;generate_blog()&lt;/code&gt; function we just created, giving it a topic to write about, and seeing the response we get.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;generate_blog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Why NYC is better than your city.&lt;/span&gt;&lt;span class="sh"&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's the complete code so far:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;openai&lt;/span&gt;

&lt;span class="n"&gt;openai&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;api_key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;sk-jAjqdWoqZLGsh7nXf5i8T3BlbkFJ9CYRk&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;# Fill in your own key
&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;generate_blog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;paragraph_topic&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;openai&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Completion&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;text-davinci-002&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;prompt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Write a paragraph about the following topic. &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;paragraph_topic&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;max_tokens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;temperature&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.3&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="n"&gt;retrieve_blog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&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="n"&gt;text&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;retrieve_blog&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;generate_blog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Why NYC is better than your city.&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And boom, after 2-3 seconds, it should spit out a paragraph like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fcodedex-io%2Fprojects%2Fmain%2Fprojects%2Fgenerate-a-blog-with-openai%2Foutput-nyc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fcodedex-io%2Fprojects%2Fmain%2Fprojects%2Fgenerate-a-blog-with-openai%2Foutput-nyc.png" alt="Output: NYC"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Try running the code a couple more times; the output should be different every time! 🤯&lt;/p&gt;

&lt;h2&gt;
  
  
  Multiple Paragraphs
&lt;/h2&gt;

&lt;p&gt;Right now, if we run our code, we'll only be able to generate one paragraph worth of text. Remember, we're trying to create a blog generator, and a blog has multiple sections, with each paragraph having a different topic.&lt;/p&gt;

&lt;p&gt;Let's add some additional code to generate as many paragraphs as we want, with each paragraph discussing a different topic:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;keep_writing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;keep_writing&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="n"&gt;answer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Write a paragraph? Y for yes, anything else for no. &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nf"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;answer&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Y&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;paragraph_topic&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;What should this paragraph talk about? &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;generate_blog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;paragraph_topic&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;keep_writing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First, we defined a variable called &lt;code&gt;keep_writing&lt;/code&gt;, to use as a boolean value for the following &lt;code&gt;while&lt;/code&gt; loop.&lt;/p&gt;

&lt;p&gt;In the &lt;code&gt;while&lt;/code&gt; loop, we created an &lt;code&gt;answer&lt;/code&gt; variable that will take in an input from the user using the built-in &lt;code&gt;input()&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;We then created an &lt;code&gt;if&lt;/code&gt; statement that will either continue the loop or stop the loop.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If the input from the user is &lt;code&gt;Y&lt;/code&gt;, then we will ask the user what topic they want to generate text about, storing that value in a variable called &lt;code&gt;paragraph_topic&lt;/code&gt;. Then we will execute and print the &lt;code&gt;generate_blog()&lt;/code&gt; function using the &lt;code&gt;parapgraph_topic&lt;/code&gt; variable as its argument.&lt;/li&gt;
&lt;li&gt;Else, we will stop the loop by assigning the &lt;code&gt;keep_writing&lt;/code&gt; variable to &lt;code&gt;False&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With that complete, we can now write as many paragraphs as we want by running the program once!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Rate Limit&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Since we're using a &lt;code&gt;while&lt;/code&gt; loop, we have the potential to be rate limited.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Rate_limiting" rel="noopener noreferrer"&gt;Rate limit&lt;/a&gt; is the number of API calls an app or user can make within a given time period.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is normally done to protect the API from abuse or &lt;a href="https://en.wikipedia.org/wiki/Denial-of-service_attack" rel="noopener noreferrer"&gt;DoS&lt;/a&gt; attacks.&lt;/p&gt;

&lt;p&gt;For GPT-3, the rate limit is 20 requests per minute. As long as we don't run the function that fast, we'll be fine. But in a rare case that it does occur, GPT-3 will stop producing responses and make us wait a minute to produce another response.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Credit Limit&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;By this point, if you have been playing with the API nonstop, there's a chance that you might have exceeded the $18 limit. The following error is thrown when that happens:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;openai.error.RateLimitError:  
You exceeded your current quota, please check your plan and billing details.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If that's the case, go to OpenAI's &lt;a href="https://beta.openai.com/account/billing/overview" rel="noopener noreferrer"&gt;Billing overview&lt;/a&gt; page and create a paid account.&lt;/p&gt;

&lt;p&gt;Let's take another breather. We're almost done!&lt;/p&gt;

&lt;h2&gt;
  
  
  Securing Our App
&lt;/h2&gt;

&lt;p&gt;Let's think about this for a minute. We created this amazing application and want to share it with the world, right? Well, when we deploy it to the web or share it with our friends, they'll be able to see every piece of code in the program. That's where the issue lies!&lt;/p&gt;

&lt;p&gt;At the beginning of this article, we created an account with OpenAI and were assigned an API key. Remember, this API key is what gives us access to GPT-3. Since GPT-3 is a paid service, the API key is also used to track usage and charge us accordingly. So what happens when someone knows our API key? They'll be able to use the service with our key, and we'll be the one charged, potentially thousands of dollars!&lt;/p&gt;

&lt;p&gt;In order to protect ourselves, we need to hide the API key in our code but still be able to use it. Let's see how we can do that.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Install python-dotenv&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pypi.org/project/python-dotenv" rel="noopener noreferrer"&gt;&lt;code&gt;python-dotenv&lt;/code&gt;&lt;/a&gt; is a package that allows us to create and use environment variables without having to set them in the operating system manually.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Environment variables are variables whose values are set outside the program, typically in the operating system.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We can install &lt;code&gt;python-dotenv&lt;/code&gt; by running the following command in the terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;python-dotenv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;.env File&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Then in our project's root directory, create a file called &lt;strong&gt;.env&lt;/strong&gt;. This file will hold our environment variable.&lt;/p&gt;

&lt;p&gt;Open up the &lt;strong&gt;.env&lt;/strong&gt; file and create a variable like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;API_KEY=&amp;lt;Your_API_Key&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The variable will take in our API key without any quotation marks or spaces. Remember to name this variable as &lt;code&gt;API_KEY&lt;/code&gt; only.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python File&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now that we have our environment variable set, let's open up the &lt;strong&gt;blog_generator.py&lt;/strong&gt; file, and paste this code under &lt;code&gt;import openai&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;dotenv&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;dotenv_values&lt;/span&gt;

&lt;span class="n"&gt;config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;dotenv_values&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;.env&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First, we've imported a method called &lt;code&gt;dotenv_values&lt;/code&gt; from the module.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;dotenv_values()&lt;/code&gt; will take in the path to the &lt;strong&gt;.env&lt;/strong&gt; file and return us a dictionary with all the variables in the &lt;strong&gt;.env&lt;/strong&gt; file. We then created a &lt;code&gt;config&lt;/code&gt; variable to hold that dictionary.&lt;/p&gt;

&lt;p&gt;Now, all we have to do is replace the exposed API key with the environment variable in the &lt;code&gt;config&lt;/code&gt; dictionary like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;openai&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;api_key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;API_KEY&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it! Our API key is now safe and hidden from the main code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: If you want to push your code to &lt;a href="https://www.github.com" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;, you don't want to push the &lt;strong&gt;.env&lt;/strong&gt; file as well. In the root directory of your project, create a file called &lt;strong&gt;.gitignore&lt;/strong&gt;, and in the Git ignore file, type in &lt;code&gt;.env&lt;/code&gt;. This will prevent the file from being tracked by Git and ultimately pushed to GitHub.&lt;/p&gt;

&lt;p&gt;With all that set and done, we’re finished! The code should now look like this!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;blog_generator.py&lt;/strong&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Generate a Blog with OpenAI 📝
&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;openai&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;dotenv&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;dotenv_values&lt;/span&gt;

&lt;span class="n"&gt;config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;dotenv_values&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;.env&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;openai&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;api_key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;API_KEY&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;generate_blog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;paragraph_topic&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;openai&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Completion&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
   &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;text-davinci-002&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="n"&gt;prompt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Write a paragraph about the following topic. &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;paragraph_topic&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="n"&gt;max_tokens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="n"&gt;temperature&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.3&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;retrieve_blog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&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="n"&gt;text&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;retrieve_blog&lt;/span&gt;

&lt;span class="n"&gt;keep_writing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;keep_writing&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="n"&gt;answer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Write a paragraph? Y for yes, anything else for no. &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nf"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;answer&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Y&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;paragraph_topic&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;What should this paragraph talk about? &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;generate_blog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;paragraph_topic&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;keep_writing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;.env&lt;/strong&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;API_KEY&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;sk-jAjqdWoqZLGsh7nXf5i8T3BlbkFJ9CYRk
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Finish Line
&lt;/h2&gt;

&lt;p&gt;Congrats, you just created a blog generator with OpenAI and Python! Throughout the project, we learned how to use GPT-3 to generate a paragraph, use a &lt;code&gt;while&lt;/code&gt; loop to create multiple paragraphs, and secure our app with a &lt;strong&gt;.env&lt;/strong&gt; file. 🙌&lt;/p&gt;

&lt;p&gt;AI is expanding rapidly, and the first few to utilize it properly through services like GPT-3 will become the inovators in the field. Hope this project helps you understand it a bit more.&lt;/p&gt;

&lt;p&gt;And lastly, we would love to see what you build with this tutorial! Tag &lt;a href="https://dev.to@codedex_io"&gt;@codedex_io&lt;/a&gt; and &lt;a href="https://twitter.com/openai" rel="noopener noreferrer"&gt;@openai&lt;/a&gt; on Twitter if you make something cool!&lt;/p&gt;

&lt;h2&gt;
  
  
  More Resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/codedex-io/projects/blob/main/projects/generate-a-blog-with-openai/blog_generator.py" rel="noopener noreferrer"&gt;Solution on GitHub&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://openai.com" rel="noopener noreferrer"&gt;OpenAI&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pypi.org/project/python-dotenv" rel="noopener noreferrer"&gt;python-dotenv&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
      <category>tutorial</category>
      <category>ai</category>
      <category>codedex</category>
    </item>
    <item>
      <title>How to Contribute to Hacktoberfest with Codédex</title>
      <dc:creator>Jerry Zhu</dc:creator>
      <pubDate>Sat, 15 Oct 2022 15:38:45 +0000</pubDate>
      <link>https://dev.to/codedex/how-to-contribute-to-hacktoberfest-with-codedex-8h9</link>
      <guid>https://dev.to/codedex/how-to-contribute-to-hacktoberfest-with-codedex-8h9</guid>
      <description>&lt;p&gt;&lt;a href="https://hacktoberfest.com/" rel="noopener noreferrer"&gt;Hacktoberfest&lt;/a&gt; is here, which means open source contributions are in full swing! 🎃 &lt;/p&gt;

&lt;p&gt;However, making your very first open source contribution can be super daunting. Don’t fret, Codédex is here to help!&lt;/p&gt;

&lt;p&gt;Here is how to contribute to open source and Hacktoberfest with Codédex and make your very first pull request to a public project on GitHub. &lt;/p&gt;

&lt;h2&gt;
  
  
  What is Codédex?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.codedex.io" rel="noopener noreferrer"&gt;Codédex&lt;/a&gt; is an 8-bit learn to code platform by developers, for developers! One of the main features of Codédex is the project tutorials! &lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/codedex-io/projects" rel="noopener noreferrer"&gt;Codédex Projects&lt;/a&gt; is a catalog of fun, beginner-friendly, and real-world programming tutorials available to developers all over the world. Our goal is to create a mountain of top-notch coding content on the web, from Python to Web Development, VR/AR to Machine Learning.&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%2Fgh774n4l803eb7d1rzq2.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%2Fgh774n4l803eb7d1rzq2.gif" alt="Image description" width="800" height="517"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that the MVP is out, our goal is to invite awesome content creators from the community to author one or two project tutorials, since maintaining a resource of this type at-scale is only possible if it's open-sourced. That means we need your help, whether it's contributing, reviewing (catching bugs/typos), or evangelizing!&lt;/p&gt;

&lt;p&gt;Here are some cool examples of Codédex Projects:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;🔗 &lt;a href="https://www.codedex.io/projects/generate-a-qr-code-with-python" rel="noopener noreferrer"&gt;Generate a QR Code with Python&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;📝 &lt;a href="https://www.codedex.io/projects/generate-a-blog-with-openai" rel="noopener noreferrer"&gt;Generate a Blog with OpenAI&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;🎞 &lt;a href="https://www.codedex.io/projects/create-a-gif-with-python" rel="noopener noreferrer"&gt;Create a GIF with Python&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;📊 &lt;a href="https://www.codedex.io/projects/visualize-youtube-data-with-plotly" rel="noopener noreferrer"&gt;Visualize YouTube Data with Plotly Express&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;🤖 &lt;a href="https://www.codedex.io/projects/build-a-discord-bot-with-python" rel="noopener noreferrer"&gt;Build a Discord Bot with Python&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why Contribute?
&lt;/h2&gt;

&lt;p&gt;This is great, but why should you contribute to Codédex Projects? &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Valuable article writing and real world coding experience.&lt;/li&gt;
&lt;li&gt;Mentorship from the Codédex team to support your project tutorial.&lt;/li&gt;
&lt;li&gt;Ability to publish your tutorials and grow your online presence on sites such as Dev.to, Indie Hackers, LinkedIn, and Medium.&lt;/li&gt;
&lt;li&gt;Access to Codédex premium features such as the Discord Server and community events.&lt;/li&gt;
&lt;li&gt;A free swag from our &lt;a href="https://codedex.myshopify.com/" rel="noopener noreferrer"&gt;merch store&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.dev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/codedex-io" rel="noopener noreferrer"&gt;
        codedex-io
      &lt;/a&gt; / &lt;a href="https://github.com/codedex-io/projects" rel="noopener noreferrer"&gt;
        projects
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      Codédex Projects 📝
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;div&gt;
  &lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;🚀 Codédex Projects&lt;/h1&gt;
&lt;/div&gt;
  &lt;strong&gt;The best programming tutorials on the web.&lt;/strong&gt;&lt;br&gt;
  &lt;strong&gt;Built by developers, for developers.&lt;/strong&gt;
&lt;/div&gt;
&lt;br&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;What are Projects?&lt;/h2&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a href="https://www.codedex.io/projects" rel="nofollow noopener noreferrer"&gt;Codédex Projects&lt;/a&gt; is a catalog of fun, beginner-friendly, and real-world programming tutorials available to developers all over the world. Our goal is to create a mountain of top-notch coding content on the web, from Python to Web Development, VR/AR to Machine Learning.&lt;/p&gt;
&lt;p&gt;Here are some examples of Codédex Projects:&lt;/p&gt;
&lt;ul class="contains-task-list"&gt;
&lt;li class="task-list-item"&gt;
 🔗 &lt;a href="https://www.codedex.io/projects/generate-a-qr-code-with-python" rel="nofollow noopener noreferrer"&gt;Generate a QR Code with Python&lt;/a&gt;
&lt;/li&gt;
&lt;li class="task-list-item"&gt;
 📝 &lt;a href="https://www.codedex.io/projects/generate-a-blog-with-openai" rel="nofollow noopener noreferrer"&gt;Generate a Blog with OpenAI&lt;/a&gt;
&lt;/li&gt;
&lt;li class="task-list-item"&gt;
 🎞 &lt;a href="https://www.codedex.io/projects/create-a-gif-with-python" rel="nofollow noopener noreferrer"&gt;Create a GIF with Python&lt;/a&gt;
&lt;/li&gt;
&lt;li class="task-list-item"&gt;
 📊 &lt;a href="https://www.codedex.io/projects/visualize-youtube-data-with-plotly" rel="nofollow noopener noreferrer"&gt;Visualize YouTube Data with Plotly Express&lt;/a&gt;
&lt;/li&gt;
&lt;li class="task-list-item"&gt;
 🤖 &lt;a href="https://www.codedex.io/projects/build-a-discord-bot-with-python" rel="nofollow noopener noreferrer"&gt;Build a Discord Bot with Python&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Contributing&lt;/h2&gt;
&lt;/div&gt;
&lt;p&gt;Ready to add your own project tutorial? Learn more by reading our &lt;a href="https://github.com/codedex-io/projects/blob/main/.github/CONTRIBUTING.md" rel="noopener noreferrer"&gt;Contribution Guide&lt;/a&gt;! 🙌&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;What stage of development are we in right now?&lt;/h2&gt;

&lt;/div&gt;
&lt;p&gt;Now that the MVP is out, our goal for Codédex Projects is to invite awesome content creators from the community to author one or…&lt;/p&gt;
&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/codedex-io/projects" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;p&gt;To contribute a project tutorial for Codédex, you can follow these 3 simple steps to get started! &lt;/p&gt;

&lt;h3&gt;
  
  
  1. Open an Issue
&lt;/h3&gt;

&lt;p&gt;First, read our &lt;a href="https://github.com/codedex-io/projects/blob/main/.github/CONTRIBUTING.md" rel="noopener noreferrer"&gt;contributing guidelines&lt;/a&gt; file to learn about the workflow for contributing.&lt;br&gt;&lt;br&gt;
Next, decide what topic you want to talk about! Your topic can be beginner, intermediate, or advanced (see the &lt;a href="https://github.com/codedex-io/projects/blob/main/.github/CONTRIBUTING.md" rel="noopener noreferrer"&gt;contributing guidelines&lt;/a&gt; for details)! Once you have 3-4 cool topic ideas, open an issue under &lt;a href="https://github.com/codedex-io/projects/issues" rel="noopener noreferrer"&gt;codedex-io/projects&lt;/a&gt;, following the issue template.&lt;/p&gt;

&lt;p&gt;Our team will get back to you with the project tutorial out of the list that you should do. Once that happens, you can start creating your project tutorial! &lt;/p&gt;

&lt;h3&gt;
  
  
  2. Open a Pull Request
&lt;/h3&gt;

&lt;p&gt;When writing your project tutorial, feel free to reach out to &lt;a href="https://twitter.com/sonnynomnom" rel="noopener noreferrer"&gt;@sonnynomnom&lt;/a&gt; with any questions. Remember that your project tutorial should be interesting and engaging for others to read, while being easy to follow! Make sure to also follow our content guidelines. &lt;/p&gt;

&lt;p&gt;Once it is completed, simply open a pull request, following the pull request template! &lt;/p&gt;

&lt;h3&gt;
  
  
  How do I submit a Pull Request (PR)?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Fork &lt;a href="https://github.com/codedex-io/projects" rel="noopener noreferrer"&gt;the repository&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Clone the forked repository to your computer.&lt;/li&gt;
&lt;li&gt;Create and switch into a new branch.&lt;/li&gt;
&lt;li&gt;Create a new folder with your project tutorial &lt;strong&gt;.mdx&lt;/strong&gt; file in it.&lt;/li&gt;
&lt;li&gt;Make a pull request to merge your fork with this repository.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. Review and Publish
&lt;/h3&gt;

&lt;p&gt;When you complete your pull request, our team will review your pull request, and provide any comments or suggestions! Once those are completed, you can publish your tutorial on any site of your choosing! &lt;/p&gt;

&lt;p&gt;So what are you waiting for? Contribute to Codédex and make an impact in the coding and open source communities! Happy Hacktoberfest! &lt;/p&gt;

&lt;p&gt;Jerry from Codédex 🚀&lt;/p&gt;

</description>
      <category>hacktoberfest</category>
      <category>codedex</category>
      <category>beginners</category>
      <category>opensource</category>
    </item>
    <item>
      <title>User Interviews for Dummies</title>
      <dc:creator>Malcolm Ridenour</dc:creator>
      <pubDate>Thu, 13 Oct 2022 16:58:36 +0000</pubDate>
      <link>https://dev.to/codedex/user-interviews-for-dummies-1775</link>
      <guid>https://dev.to/codedex/user-interviews-for-dummies-1775</guid>
      <description>&lt;p&gt;Hi Devs! 👋 My name is Malcolm. I’m a brand new Customer Success Manager in my first tech job. When I was told to conduct a batch of user interviews, I assumed it would be easy. And after I completely bombed the first few, I knew this was out of my element.&lt;/p&gt;

&lt;p&gt;My team then gave me a tiny pink book called &lt;em&gt;&lt;a href="https://www.momtestbook.com" rel="noopener noreferrer"&gt;The Mom Test&lt;/a&gt;&lt;/em&gt; by Ron Fitzpatrick, and it immediately changed not only the quality of my user interviews but also my thought process on identifying the good ideas from the bad IRL! &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fkdmzl5185hdgc56h55sk.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fkdmzl5185hdgc56h55sk.jpg" alt="The Mom Test Book" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I’d like to share three takeaways from the book and perhaps help someone new to user interviews like myself:&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Customer conversations are bad by default and it’s your job to fix them by asking better questions.
&lt;/h2&gt;

&lt;p&gt;This is a cardinal rule from the book. Here are some examples of some bad questions (that I personally asked) that can give false positives and empty compliments that don't identify problems or offer solutions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Do you think X is a good idea?&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Would you buy a product that did X?&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;How much would you pay for X?&lt;/em&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These questions lead to fluff - answers based on hypotheticals and detached from historical facts.&lt;/p&gt;

&lt;p&gt;Here are examples of some good questions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Talk me through the last time X happened?&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Have you ever purchased a product like X?&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;How are you dealing with this problem now?&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;The good questions focus on specifics of the past instead of the generics or opinions about the future. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You won’t be able to completely stop some user interviews from being unproductive or awkward, but your questions are the only way to mitigate the dry and interrogative nature of them. &lt;/p&gt;

&lt;h2&gt;
  
  
  2. You can’t learn anything useful unless you're willing to spend a few minutes being quiet.
&lt;/h2&gt;

&lt;p&gt;This is something I struggled with: &lt;/p&gt;

&lt;p&gt;I talked too much and listened too little during my interviews!&lt;/p&gt;

&lt;p&gt;During lulls in a conversation or when the user would say something that would spark my interest, I would allow the conversation to travel to a place that isn't useful anymore. And that's a no-go from the book. 🙅&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The interviewer should only be speaking to ask or explain a question or guiding a user to the next question; the rest of the time should be listening to what your user has to say.  &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A great way to discover whether or not this is a problem for you is to record your interviews (with their permission of course) and listening to them afterwards. If you are talking more than they are, you are not using your time wisely.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Don't pitch your ideas to customers and don't mention your solution.
&lt;/h2&gt;

&lt;p&gt;Naturally, when a customer gives you a negative critique or shares a problem they encountered with your product - it is almost reflexive to apologize and tell them what your intention was and what the team is doing behind the scenes to work on that problem. But don’t do it! 😱&lt;/p&gt;

&lt;p&gt;There are two reasons you shouldn't pitch your ideas or explain your solutions to customers:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;It is a waste of time that you could be using to listen for more information to solve the problem, instead of apologizing longer than you need to.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Most people are friendly and want to tell you what you want to hear. Little white lies told to appear amicable, but those don't help your business! What you want is the truth. So keep your plans and pitches to a minimum and let them tell you their opinions unsullied by letting them know what your plans are.  &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fqbs6nkh2mbrqa7l57x0e.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fqbs6nkh2mbrqa7l57x0e.gif" alt="Everybody Lies GIF" width="500" height="328"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So learn from my mistakes when conducting your own user interviews and don’t let any of these obstacles get in the way of collecting the information you need from the most important part of any project or build – the folks that use them! &lt;/p&gt;

&lt;p&gt;Happy interviewing and pick up &lt;em&gt;The Mom Test&lt;/em&gt; by Ron Fitzpatrick to put your user interviews into overdrive.&lt;/p&gt;

&lt;p&gt;💖 Malcolm from Codédex&lt;/p&gt;

&lt;p&gt;&lt;iframe class="tweet-embed" id="tweet-1570869230341615618-812" src="https://platform.twitter.com/embed/Tweet.html?id=1570869230341615618"&gt;
&lt;/iframe&gt;

  // Detect dark theme
  var iframe = document.getElementById('tweet-1570869230341615618-812');
  if (document.body.className.includes('dark-theme')) {
    iframe.src = "https://platform.twitter.com/embed/Tweet.html?id=1570869230341615618&amp;amp;theme=dark"
  }



&lt;/p&gt;

</description>
      <category>startup</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>codedex</category>
    </item>
    <item>
      <title>Attending Your First Hackathon: Tips And Tricks</title>
      <dc:creator>Jerry Zhu</dc:creator>
      <pubDate>Tue, 11 Oct 2022 19:55:07 +0000</pubDate>
      <link>https://dev.to/codedex/attending-your-first-hackathon-tips-and-tricks-5d7k</link>
      <guid>https://dev.to/codedex/attending-your-first-hackathon-tips-and-tricks-5d7k</guid>
      <description>&lt;p&gt;What do you think of when I say the word “hackathon”? Is it a group of people in black hoodies in the dark typing fiendishly in the terminal?&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%2Fnwt7q6qpwwd59jwxa1wy.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%2Fnwt7q6qpwwd59jwxa1wy.gif" alt="Image description" width="498" height="217"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this article, you will learn what a hackathon is, tips and tricks to achieve your goals, and how to get the most out of your hackathon experience! 🚀&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Hackathon?
&lt;/h2&gt;

&lt;p&gt;A &lt;a href="https://en.wikipedia.org/wiki/Hackathon" rel="noopener noreferrer"&gt;hackathon&lt;/a&gt; is usually a 24-48 hour event where programmers, or “hackers”, get together and create cool projects together! Hackers get together in groups of 2-4 to build a project about something they are passionate about. Although building a project and getting judged on it is the bulk of the hackathon experience, it is definitely not the only thing happening! &lt;/p&gt;

&lt;p&gt;There are also several types of events happening in tandem, including activities, workshops, and networking events, as well as food! In terms of networking, there will be sponsor booths, where you can talk to representatives from companies, and hacker meetups. Being able to manage your time given everything that is going on around you can be tricky. &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%2Focxt0mtudaiuwkj8jv95.jpg" 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%2Focxt0mtudaiuwkj8jv95.jpg" alt="Image description" width="700" height="394"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here are a few tips and tricks!&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Do Your Research Beforehand
&lt;/h3&gt;

&lt;p&gt;Before going into the hackathon, do some research into what the hackathon is about. This can be as simple as reading the FAQ, or looking at prizes and sponsors. You should have a good idea of what the theme is (if there is one), which prizes you want to go for, and which sponsor libraries you are interested in. Having these things in mind will help you plan your hackathon project better. &lt;/p&gt;

&lt;h3&gt;
  
  
  2. Project Idea and Schedule
&lt;/h3&gt;

&lt;p&gt;During the hackathon, you will have to work with a team to think of an idea. As such, it would be beneficial to think of an idea beforehand. A great way to come up with an idea is to think about a problem you’d like to see solved, whether it be in your life, community, or the world! You can sculpt a potential solution through real world issues you want to tackle! &lt;/p&gt;

&lt;p&gt;For experienced hackers, a great way to plan ahead of time is to look at the hackathon schedule, and create a Google Calendar based on what you want to be doing during those times. Although unexpected things could happen, knowing approximately how much time you want to commit to each part of the hackathon will go a long way in making it more successful! &lt;/p&gt;

&lt;p&gt;However, in order to plan your schedule, you must know your strengths and weaknesses. &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%2Foeo1j6gmoz512c5g8rom.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%2Foeo1j6gmoz512c5g8rom.png" alt="Image description" width="800" height="378"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Know Your Strengths and Weaknesses
&lt;/h3&gt;

&lt;p&gt;This is an extremely crucial point when tackling your project during the hackathon. Make sure that you know exactly what you want to accomplish, and what is required to get there.&lt;/p&gt;

&lt;p&gt;In terms of libraries, reach out to your team to see what they are comfortable with, and how to align them with the project. &lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For complete code newbies, using HTML/CSS or no-code libraries like &lt;a href="https://bubble.io/" rel="noopener noreferrer"&gt;Bubble&lt;/a&gt; or &lt;a href="https://www.adalo.com/" rel="noopener noreferrer"&gt;Adalo&lt;/a&gt; could be a great suggestion for deployment.&lt;/li&gt;
&lt;li&gt;For less experienced programmers, HTML/CSS/JavaScript are great languages for building a website and hosting it on &lt;a href="https://www.repl.it" rel="noopener noreferrer"&gt;Repl.it&lt;/a&gt;, &lt;a href="https://www.glitch.com" rel="noopener noreferrer"&gt;Glitch&lt;/a&gt;, or &lt;a href="https://pages.github.com/" rel="noopener noreferrer"&gt;GitHub pages&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;For more experienced programmers, look into Python or JavaScript libraries for a more complex application. You can deploy it on &lt;a href="https://www.netlify.com" rel="noopener noreferrer"&gt;Netlify&lt;/a&gt; or &lt;a href="https://vercel.com" rel="noopener noreferrer"&gt;Vercel&lt;/a&gt; and use a database, or look into cloud services and containerization.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;However, don’t do something you are not super comfortable with: after all, there are only 24 hours, so if you have looked into a framework but have no idea how it works, maybe look into a simpler one. &lt;/p&gt;

&lt;p&gt;Knowing your strengths when it comes to learning new libraries is also important when deciding sponsor or prize categories to pursue. Going out of your way to learn a difficult tool or library for a prize may be tempting, but it might be more beneficial to stick to the basics. &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%2Fru0jgvz95eb6a3swsro3.webp" 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%2Fru0jgvz95eb6a3swsro3.webp" alt="meme" width="500" height="300"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Manage Your Time Well
&lt;/h3&gt;

&lt;p&gt;To do well at a hackathon, it is extremely important to manage your time well. There are so many things going on during a hackathon, and it can be overwhelming to try to do all of them while also wanting to focus on your project. Having a point of focus is very important. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you care about your project more, spend less time going to events. &lt;/li&gt;
&lt;li&gt;If you want to build a network and have fun, go to activities and sponsor booths. &lt;/li&gt;
&lt;li&gt;If you want to learn more about coding, attend workshops and reach out to mentors. &lt;/li&gt;
&lt;li&gt;If you really want to win, make sure to spend an ample amount of time on the pitch.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Don’t be afraid to have fun, but also make sure you achieve what you want to! &lt;/p&gt;

&lt;h3&gt;
  
  
  5. Take Away Something
&lt;/h3&gt;

&lt;p&gt;The most important thing is to take something away from the hackathon! Maybe it’s the friends and connections you made, maybe it’s the tools and libraries you learned, and maybe it’s the project you just made that can be added to your resume or personal portfolio. In any case, learning something from a hackathon, and gaining experience, will help your next hackathon experience be even better.&lt;/p&gt;

&lt;p&gt;Feel free to utilize these tips the next time you’re at a hackathon. Remember, as long as you’re having fun, you’re succeeding! &lt;/p&gt;

&lt;p&gt;Go put yourself out there and happy hacking 💻!&lt;/p&gt;

&lt;p&gt;Jerry from Codédex&lt;/p&gt;

</description>
      <category>hackathon</category>
      <category>codenewbie</category>
      <category>beginners</category>
      <category>codedex</category>
    </item>
    <item>
      <title>Generate a QR Code with Python</title>
      <dc:creator>Jerry Zhu</dc:creator>
      <pubDate>Fri, 23 Sep 2022 00:32:54 +0000</pubDate>
      <link>https://dev.to/codedex/generate-a-qr-code-with-python-386m</link>
      <guid>https://dev.to/codedex/generate-a-qr-code-with-python-386m</guid>
      <description>&lt;p&gt;&lt;strong&gt;Prerequisites:&lt;/strong&gt; Python fundamentals&lt;br&gt;
&lt;strong&gt;Versions:&lt;/strong&gt; Python 3.10, qrcode 7.3.1, Pillow 9.2.0&lt;br&gt;
&lt;strong&gt;Read Time:&lt;/strong&gt; 40 minutes&lt;/p&gt;
&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Have you ever wondered how &lt;a href="https://en.wikipedia.org/wiki/QR_code" rel="noopener noreferrer"&gt;QR codes&lt;/a&gt; work or how procedural images are generated? Have you ever wanted to send someone a website link in a much cooler way? If you said yes to any of these questions, you're in luck!&lt;/p&gt;

&lt;p&gt;In this quick tutorial, we will learn how to create a QR code in Python with &lt;a href="https://pypi.org/project/qrcode" rel="noopener noreferrer"&gt;qrcode&lt;/a&gt;, &lt;a href="https://pypi.org/project/Pillow" rel="noopener noreferrer"&gt;pillow&lt;/a&gt;, and just five lines of code.&lt;/p&gt;

&lt;p&gt;Let's jump in!&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%2F9p58dzejhpx2zuhd9gf6.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%2F9p58dzejhpx2zuhd9gf6.png" alt="QR example" width="215" height="215"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  What Is a QR Code?
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://en.wikipedia.org/wiki/QR_code" rel="noopener noreferrer"&gt;QR code&lt;/a&gt;, short for Quick Response code, was originally invented in 1994 by a Japanese tech company. It is a 2D barcode containing black patterns on a white background. However, this is no ordinary scribble: QR codes are capable of storing huge amounts of data in a deceivingly small amount of space. These black rectangles can store links, text, basically anything you want... and can be accessed simply by scanning from any mobile device!&lt;/p&gt;

&lt;p&gt;A QR code is important since it gives users a simple way to access something on a non-conventional source (e.g., on a piece of paper). Putting a QR code on a piece of paper is a far better and faster experience for the user than placing a website link. Due to this, QR codes are now becoming more commonly used than &lt;a href="https://en.wikipedia.org/wiki/Universal_Product_Code" rel="noopener noreferrer"&gt;UPC barcodes&lt;/a&gt; and are found on restaurant menus, business cards, and even &lt;a href="https://www.youtube.com/watch?v=1zLsUhOCqyU" rel="noopener noreferrer"&gt;Superbowl ads&lt;/a&gt;!&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/eIUD_NE1BDo"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Enough about QR codes, let's learn how to create one!&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting Up
&lt;/h2&gt;

&lt;p&gt;First, go to the Python code editor of your choice (we recommend &lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer"&gt;VS Code&lt;/a&gt;), and create a new file called &lt;strong&gt;qr_code.py&lt;/strong&gt;. This is where we will be writing our code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; You can call your file any name except &lt;strong&gt;qrcode.py&lt;/strong&gt;. This is because &lt;strong&gt;qrcode.py&lt;/strong&gt; is a file that already exists as part of the &lt;code&gt;qrcode&lt;/code&gt; library that we will use, and calling your file that will overwrite the library functions.&lt;/p&gt;

&lt;p&gt;To start, we need to install the two libraries:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;a href="https://pypi.org/project/qrcode" rel="noopener noreferrer"&gt;&lt;code&gt;qrcode&lt;/code&gt;&lt;/a&gt; library: This library lets us perform all of our QR code related operations.&lt;/li&gt;
&lt;li&gt;The &lt;a href="https://pypi.org/project/Pillow" rel="noopener noreferrer"&gt;&lt;code&gt;pillow&lt;/code&gt;&lt;/a&gt; library: This library helps us process and save images.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To install &lt;code&gt;qrcode&lt;/code&gt; and &lt;code&gt;pillow&lt;/code&gt;, run this command inside the VS Code terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;qrcode pillow
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For this tutorial, we are using qrcode version 7.3.1 and Pillow version 9.2.0.&lt;/p&gt;

&lt;p&gt;Next, add this line of code to the first line of &lt;strong&gt;qr_code.py&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;qrcode&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This line of code makes sure that the two libraries can be used in the rest of our code, since Python code runs from top to bottom in a file. We just need to import &lt;code&gt;qrcode&lt;/code&gt;, because &lt;code&gt;pillow&lt;/code&gt; is implicitly imported.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating the QR Code
&lt;/h2&gt;

&lt;p&gt;First, we want a link that we want to showcase. Let's use a &lt;a href="https://www.youtube.com/watch?v=dQw4w9WgXcQ" rel="noopener noreferrer"&gt;classic YouTube video&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We can store this YouTube URL into a variable called &lt;code&gt;website_link&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;website_link&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;https://www.youtube.com/watch?v=dQw4w9WgXcQ&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we want to create an instance of &lt;code&gt;qrcode&lt;/code&gt;. Since it's a Python library, we can call the package constructor to create a &lt;code&gt;qrcode&lt;/code&gt; object, customized to our specifications. &lt;/p&gt;

&lt;p&gt;In this example, we will create a QR code with a version of 1, and a box size and border size of 5.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;qr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;qrcode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;QRCode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;box_size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;border&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;version&lt;/code&gt; parameter is an integer from 1 to 40 that controls the size of the QR code.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;box_size&lt;/code&gt; parameter controls how many pixels each “box” of the QR code is.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;border&lt;/code&gt; parameter controls how many boxes thick the border should be.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As an exercise, try taking in these parameters as input, and explaining to the user how to set this up, so they can create the QR code to their own specifications.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/lincolnloop/python-qrcode" rel="noopener noreferrer"&gt;Visit documentation&lt;/a&gt; for more information about the parameters in &lt;code&gt;qrcode.QRCode(...)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Then, the data (specifically, the link we specified before) is added to the QR code, using &lt;code&gt;.add_data()&lt;/code&gt;. The QR code is then generated using &lt;code&gt;.make()&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;qr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;website_link&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;qr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;make&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, we save this created QR code in an &lt;code&gt;img&lt;/code&gt; pillow object using &lt;code&gt;qr.make_image()&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;img&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;qr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;make_image&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fill_color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;black&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;back_color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;white&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Setting the line color &lt;code&gt;fill_color&lt;/code&gt; to black.&lt;/li&gt;
&lt;li&gt;Setting the background color &lt;code&gt;back_color&lt;/code&gt; to white.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finally, we have to store and save the file. We can do this using pillow's &lt;code&gt;save()&lt;/code&gt; command. We specify the file name inside the brackets, which is &lt;code&gt;youtube_qr.png&lt;/code&gt; in our case.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;img&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;youtube_qr.png&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we are done! Here’s the whole code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;qrcode&lt;/span&gt;

&lt;span class="n"&gt;website_link&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;https://www.youtube.com/watch?v=dQw4w9WgXcQ&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;

&lt;span class="n"&gt;qr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;qrcode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;QRCode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;box_size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;border&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;qr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;website_link&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;qr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;make&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;img&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;qr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;make_image&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fill_color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;black&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;back_color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;white&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;img&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;youtube_qr.png&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should see the &lt;strong&gt;youtube_qr.png&lt;/strong&gt; image pop up on the left-hand side of VS Code, and you can open it to see what it looks 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%2F9p58dzejhpx2zuhd9gf6.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%2F9p58dzejhpx2zuhd9gf6.png" alt="QR example" width="215" height="215"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can add this QR code to anywhere you like, on your website or in an email!&lt;/p&gt;

&lt;h2&gt;
  
  
  Improvements
&lt;/h2&gt;

&lt;p&gt;To improve this, we could do a couple of things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Allow the website link to be typed in using &lt;code&gt;input()&lt;/code&gt; function.&lt;/li&gt;
&lt;li&gt;Allow users to customize the QR code generated.&lt;/li&gt;
&lt;li&gt;Automate the process to create multiple QR codes. &lt;/li&gt;
&lt;li&gt;Include more functions (or object parameters) of the &lt;a href="https://pypi.org/project/qrcode/" rel="noopener noreferrer"&gt;&lt;code&gt;qrcode&lt;/code&gt;&lt;/a&gt; library.&lt;/li&gt;
&lt;li&gt;Try changing the colors and styles of the generated QR codes using different drawer modules and fill colors.&lt;/li&gt;
&lt;li&gt;Use an application library (like &lt;a href="https://docs.python.org/3/library/tkinter.html" rel="noopener noreferrer"&gt;Tkinter&lt;/a&gt;) to add a user interface.&lt;/li&gt;
&lt;li&gt;Check out other QR code libraries like &lt;a href="https://pypi.org/project/PyQRCode/" rel="noopener noreferrer"&gt;&lt;code&gt;pyqrcode&lt;/code&gt;&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  More Resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/codedex-io/projects/blob/main/projects/generate-a-qr-code-with-python/qr_code.py" rel="noopener noreferrer"&gt;Solution on GitHub&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pypi.org/project/qrcode/" rel="noopener noreferrer"&gt;Documentation: qrcode&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pypi.org/project/PyQRCode/" rel="noopener noreferrer"&gt;Documentation: pyqrcode&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pypi.org/project/Pillow/" rel="noopener noreferrer"&gt;Documentation: Pillow&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>beginners</category>
      <category>python</category>
      <category>tutorial</category>
      <category>codedex</category>
    </item>
    <item>
      <title>Build a Discord Bot with Python</title>
      <dc:creator>Sonny Li</dc:creator>
      <pubDate>Thu, 01 Sep 2022 20:13:27 +0000</pubDate>
      <link>https://dev.to/codedex/build-a-discord-bot-with-python-1lm3</link>
      <guid>https://dev.to/codedex/build-a-discord-bot-with-python-1lm3</guid>
      <description>&lt;p&gt;&lt;strong&gt;Prerequisites:&lt;/strong&gt; Python fundamentals&lt;br&gt;
&lt;strong&gt;Versions:&lt;/strong&gt; Python 3.10&lt;br&gt;
&lt;strong&gt;Read Time:&lt;/strong&gt; 45 minutes&lt;/p&gt;
&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In this tutorial, we'll learn how to set up your Python programming environment, create and register a bot using &lt;a href="https://discord.com/developers/" rel="noopener noreferrer"&gt;Discord Developer Portal&lt;/a&gt;, and write a few lines of Python code to respond to users' messages in Discord. We'll also provide a breakdown of each line of code for those who want to get a deeper understanding of how it all works.&lt;/p&gt;

&lt;p&gt;The Discord bot that we are going to build will listen for the keyword &lt;code&gt;$meme&lt;/code&gt; and responds with a random meme from Reddit. &lt;/p&gt;

&lt;p&gt;The final result will look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fcodedex-io%2Fprojects%2Fmain%2Fprojects%2Fbuild-a-discord-bot-with-python%2Fdemo.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fcodedex-io%2Fprojects%2Fmain%2Fprojects%2Fbuild-a-discord-bot-with-python%2Fdemo.gif" alt="Example meme"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's dive in!&lt;/p&gt;
&lt;h2&gt;
  
  
  Requirements
&lt;/h2&gt;

&lt;p&gt;Before we get started, you will need a few things if you don't have them already:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.python.org/downloads/" rel="noopener noreferrer"&gt;Python 3&lt;/a&gt; installed.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://pip.pypa.io/en/stable/installation/" rel="noopener noreferrer"&gt;pip&lt;/a&gt; (package installer) installed.&lt;/li&gt;
&lt;li&gt;A Discord account.&lt;/li&gt;
&lt;li&gt;A Discord server with "Manager Server" permissions.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  How Does a Discord Bot Work
&lt;/h2&gt;

&lt;p&gt;First, let's zoom out a bit and think about this question: "What does it mean to code a Discord Bot?" Simply put, a bot is nothing more than a computer program that performs some useful actions. &lt;/p&gt;

&lt;p&gt;Because Discord wants bots to be able to do useful things, they have allowed developers to access parts of its system in their code, such as automatically responding to messages or helping with the server's admin functions (e.g. check out these &lt;a href="https://top.gg/list/top" rel="noopener noreferrer"&gt;popular Discord Bots&lt;/a&gt;). &lt;/p&gt;

&lt;p&gt;Today, we're focused on getting our bot to read and write messages, so let's see how that works.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fcodedex-io%2Fprojects%2Fmain%2Fprojects%2Fbuild-a-discord-bot-with-python%2Fsketch.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fcodedex-io%2Fprojects%2Fmain%2Fprojects%2Fbuild-a-discord-bot-with-python%2Fsketch.png" alt="Drawing of server diagram"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From the crude drawing above (I'm an engineer, not an artist), we can see how users and bots connect to the Discord backend. Each user interacts with the Discord backend to write and read messages. Then, the Discord backend servers will broadcast an event to any program listening that a new message has been posted. All we have to do is write our program to respond to message events (named &lt;strong&gt;bot.py&lt;/strong&gt; here) and connect it to the Discord backend by using their API.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;API (Application Programming Interface) is just a fancy terminology to define how one program talks to another program. In our case, Discord's API allows us to read and send messages to its backend servers.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;First, we'll have to create and register our bot from Discord's Developer Portal.&lt;/p&gt;
&lt;h2&gt;
  
  
  Setting Up Discord Developer Portal
&lt;/h2&gt;

&lt;p&gt;We'll be creating and registering our Discord Application inside the Developer Portal. Then, we will create a Bot for the application and get the required permissions.&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 1. Go to Developer Portal
&lt;/h3&gt;

&lt;p&gt;Visit &lt;a href="https://discord.com/developers/applications" rel="noopener noreferrer"&gt;https://discord.com/developers/applications&lt;/a&gt; and sign in if you haven't already.&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 2. Create a New Application
&lt;/h3&gt;

&lt;p&gt;To let Discord know we're going to be connecting to their backend servers, we need to register an application and set up the required permissions. &lt;/p&gt;

&lt;p&gt;Click "New Application" on the top right.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimgur.com%2FpvjFmDB.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimgur.com%2FpvjFmDB.png" alt="Create Application"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, give your application a name. Here, we are calling ours MemeBot:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimgur.com%2FIXbC5RB.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimgur.com%2FIXbC5RB.png" alt="Create new bot"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 3. Add a Bot to Your Application
&lt;/h3&gt;

&lt;p&gt;Find the "Bot" tab in the left panel:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimgur.com%2FF2t6hQh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimgur.com%2FF2t6hQh.png" alt="Add Bot"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And click "Add Bot":&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimgur.com%2FMGGg2bP.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimgur.com%2FMGGg2bP.png" alt="Add Bot 2"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 4. Get the Token for Your Application
&lt;/h3&gt;

&lt;p&gt;This step is important because we'll need this Token for later. This Token is similar to a password for your application. Anybody who has this can control it. (So make sure to never share your bot token with anyone, or upload it on GitHub by mistake!)&lt;/p&gt;

&lt;p&gt;Click "Reset Token", write it down somewhere safe and definitely don't put it in any public place.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimgur.com%2FdJ3OhpB.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimgur.com%2FdJ3OhpB.png" alt="Reset Token"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 5. Invite Application to Your Server
&lt;/h3&gt;

&lt;p&gt;Now that we've set up our first application, we have to create an invite, to get our bot into our Discord server. To do that we can click URL Generator and select the following permissions. In the screenshot below, we are telling Discord to create an invitation link for our application with the scope "Bot" and that bot should be able to "Send Messages." (Note: Be careful with granting the all-powerful "Administrator" permission to your bot).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimgur.com%2FFAwczVR.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimgur.com%2FFAwczVR.png" alt="set up permissions"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimgur.com%2FZyDCKhl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimgur.com%2FZyDCKhl.png" alt="get the invite link"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is the link to invite our application (Discord bot) into a Discord server. Copy and paste this link into another tab in your browser and invite the bot into the Discord server that you want.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimgur.com%2FS5zVxzP.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimgur.com%2FS5zVxzP.png" alt="invite the bot"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can confirm if this worked by checking for this message in your Discord server.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimgur.com%2FJRlzfIS.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimgur.com%2FJRlzfIS.png" alt="invite success"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Alright, now that we’ve set up our coding environment and got the necessary permissions from Discord for our new application, let’s write some code.&lt;/p&gt;
&lt;h2&gt;
  
  
  Writing the Python Code
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Installing discord.py
&lt;/h3&gt;

&lt;p&gt;The code that we will write, will be responsible for the creation of the bot that will connect to the Discord backend using Discord APIs.&lt;/p&gt;

&lt;p&gt;The complicated part about writing this program is how we're going to interact with the Discord API. While we can read the &lt;a href="https://discord.com/developers/docs/intro" rel="noopener noreferrer"&gt;API documentation&lt;/a&gt; and send HTTP requests directly to Discord, we have an easier way.&lt;/p&gt;

&lt;p&gt;We'll be using a Python package called &lt;a href="https://discordpy.readthedocs.io/en/stable/" rel="noopener noreferrer"&gt;discord.py&lt;/a&gt; which is a simple wrapper around the Discord API. A wrapper provides a way to access an API through a particular programming language, in this case, Python!&lt;/p&gt;

&lt;p&gt;If you are using Mac, run the following command in the Terminal to install it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python3 &lt;span class="nt"&gt;-m&lt;/span&gt; pip &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-U&lt;/span&gt; discord.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you are using Windows, then the following command should be used instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;py &lt;span class="nt"&gt;-3&lt;/span&gt; &lt;span class="nt"&gt;-m&lt;/span&gt; pip &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-U&lt;/span&gt; discord.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What did we just do here? We just installed a Python package from &lt;a href="https://pypi.org/project/pip" rel="noopener noreferrer"&gt;pip&lt;/a&gt; the package installer, which is just another way of saying we downloaded code that someone kindly wrote and uploaded to a public repository.&lt;/p&gt;

&lt;h3&gt;
  
  
  Log Into the Discord Server with the Bot
&lt;/h3&gt;

&lt;p&gt;Let’s create a new file called &lt;strong&gt;bot.py&lt;/strong&gt; in a new directory. This is the main file where we'll code the logic to make our Discord bot. Now paste the following code into it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;discord&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;discord&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;on_ready&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Logged on as {0}!&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MyClient&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Your Token Here&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# Replace with your own token.
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s go over this block of code line by line.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;discord&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;discord&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;on_ready&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Logged on as {0}!&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First, we've imported the &lt;code&gt;discord.py&lt;/code&gt; package that we just installed and created our own class &lt;code&gt;MyClient&lt;/code&gt; which we will use to interact with the Discord API. We create this class by extending from the base class &lt;code&gt;discord.Client&lt;/code&gt;. This base class already has methods to respond to common events. For example, the &lt;code&gt;on_ready()&lt;/code&gt; function above will be called when the Discord bot's login is successful.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;discord.py&lt;/code&gt; works around the concept of events. There are other types of events (e.g. messages) that we will see later, but for now here's the definition of events from their website:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;An event is something you listen to and then respond to. For example, when a message happens, you will receive an event about it that you can respond to.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Finally, our last two lines of code instantiates the &lt;code&gt;MyClient&lt;/code&gt; class and calls &lt;code&gt;run&lt;/code&gt;, which is the main way to start the client. The client will use the given token (which you should have saved from before) to authenticate itself to the Discord backend servers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MyClient&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Your Token Here&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# Replace with your own token
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Running the Bot
&lt;/h3&gt;

&lt;p&gt;You can run this code by typing in the following in your terminal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python3 bot.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As long as you keep this program running, your Discord bot will be online.&lt;/p&gt;

&lt;p&gt;If you see this output in your terminal, then your Discord bot has successfully logged into your Discord server.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Logged on as MemeBot#3995!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Responding to Messages
&lt;/h3&gt;

&lt;p&gt;Next, we want our bot to read messages in a Discord channel and respond to them.&lt;/p&gt;

&lt;p&gt;Add the &lt;code&gt;.on_message()&lt;/code&gt; method to the &lt;code&gt;MyClient&lt;/code&gt; class like the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;on_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;author&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;startswith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;$hello&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Hello World!&lt;/span&gt;&lt;span class="sh"&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 &lt;code&gt;.on_message()&lt;/code&gt; method gets called automatically anytime there is a new message in a channel where our bot is located. In our method:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We are first checking &lt;code&gt;if message.author == self.user&lt;/code&gt;, if the bot is the one sending the message in the chat. We don’t want the bot to keep responding to its own messages.&lt;/li&gt;
&lt;li&gt;Then, we have some code to respond to a special keyword &lt;code&gt;$hello&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's try it out. Run the command &lt;code&gt;python3 bot.py&lt;/code&gt; to start our program.&lt;/p&gt;

&lt;p&gt;Now, add Memebot to any channel and type the word &lt;code&gt;$hello&lt;/code&gt; in that channel and see what happens:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimgur.com%2FGMfqJ8g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimgur.com%2FGMfqJ8g.png" alt="Discord example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We've successfully programmed our bot to log into Discord and respond to our messages. But can we go further?&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Respond with Memes
&lt;/h2&gt;

&lt;p&gt;What if instead of responding with a text message, the bot responds with memes? Well that sounds like a lot of work.. buuut the cool thing about programming is that we can reuse other people’s work (yay for open source).&lt;/p&gt;

&lt;p&gt;Check out this Github repository: &lt;a href="https://github.com/D3vd/Meme_Api" rel="noopener noreferrer"&gt;https://github.com/D3vd/Meme_Api&lt;/a&gt;. This API returns a &lt;a href="https://en.wikipedia.org/wiki/JSON" rel="noopener noreferrer"&gt;JSON&lt;/a&gt; response containing information about random memes from Reddit. Hmm, let’s see how it works by visiting this URL: &lt;a href="https://meme-api.herokuapp.com/gimme" rel="noopener noreferrer"&gt;https://meme-api.herokuapp.com/gimme&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Here is the response that I got:&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="p"&gt;{&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;postLink&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://redd.it/w0teq0&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;subreddit&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;me_irl&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;title&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;me_irl&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;url&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://i.redd.it/ztpkxx6yl0c91.png&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;nsfw&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;spoiler&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;author&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ozgonngu&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ups&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;110&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;preview&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://preview.redd.it/ztpkxx6yl0c91.png?width=108&lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;&lt;span class="s2"&gt;u0026crop=smart&lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;&lt;span class="s2"&gt;u0026auto=webp&lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;&lt;span class="s2"&gt;u0026s=e3db2a1ed5ee73480224b55b16fcb38925ea9095&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://preview.redd.it/ztpkxx6yl0c91.png?width=216&lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;&lt;span class="s2"&gt;u0026crop=smart&lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;&lt;span class="s2"&gt;u0026auto=webp&lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;&lt;span class="s2"&gt;u0026s=aaec73a28c4c0b2bc47f4f9722be2fdb8ba95893&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://preview.redd.it/ztpkxx6yl0c91.png?width=320&lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;&lt;span class="s2"&gt;u0026crop=smart&lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;&lt;span class="s2"&gt;u0026auto=webp&lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;&lt;span class="s2"&gt;u0026s=90495a39d05f6d9d8975fb776887c08f65ece2c4&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://preview.redd.it/ztpkxx6yl0c91.png?width=640&lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;&lt;span class="s2"&gt;u0026crop=smart&lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;&lt;span class="s2"&gt;u0026auto=webp&lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;&lt;span class="s2"&gt;u0026s=b291abfb8f85b5826f001dee881570e694bdb46c&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://preview.redd.it/ztpkxx6yl0c91.png?width=960&lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;&lt;span class="s2"&gt;u0026crop=smart&lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;&lt;span class="s2"&gt;u0026auto=webp&lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;&lt;span class="s2"&gt;u0026s=7afc3f2532c7017a0045ae50777a18a2e452183e&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;It looks like the &lt;code&gt;url&lt;/code&gt; field here contains the link for the meme's image. How about we have our bot respond to user's messages with this image?&lt;/p&gt;

&lt;h3&gt;
  
  
  Integrating the Meme API
&lt;/h3&gt;

&lt;p&gt;Let's paste these lines of code to the top of the file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_meme&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
  &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;https://meme-api.herokuapp.com/gimme&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;json_data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;loads&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;json_data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;url&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;requests&lt;/code&gt; package allows us to make HTTP requests to any URL. In this case we're calling the GET method to the URL that will give us the meme data.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;json&lt;/code&gt; package allows us to read JSON data. This is useful since most data passed around on the web is in the JSON format, like the JSON response we saw above.&lt;/p&gt;

&lt;p&gt;We need to install the &lt;code&gt;requests&lt;/code&gt; package to import it. Run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python3 &lt;span class="nt"&gt;-m&lt;/span&gt; pip &lt;span class="nb"&gt;install &lt;/span&gt;requests
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now all we need to do is call &lt;code&gt;get_meme()&lt;/code&gt; inside our bot's &lt;code&gt;on_message()&lt;/code&gt; method. Let's also change the keyword from &lt;code&gt;$hello&lt;/code&gt; to &lt;code&gt;$meme&lt;/code&gt; since it's more fitting. Your code should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;discord&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_meme&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
  &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;https://meme-api.herokuapp.com/gimme&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;json_data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;loads&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;json_data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;url&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;discord&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;on_ready&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Logged on as {0}!&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;on_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;author&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;startswith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;$meme&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
      &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;get_meme&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;

&lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MyClient&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Your Token Here&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# Replace with your own token
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Time to test it out!&lt;/p&gt;

&lt;p&gt;Just remember you need to close and restart your program. Press control+c to close the currently running program in the terminal and then, run &lt;code&gt;python3 bot.py&lt;/code&gt; again.&lt;/p&gt;

&lt;p&gt;The moment of truth...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fcodedex-io%2Fprojects%2Fmain%2Fprojects%2Fbuild-a-discord-bot-with-python%2Ffinal.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fcodedex-io%2Fprojects%2Fmain%2Fprojects%2Fbuild-a-discord-bot-with-python%2Ffinal.gif" alt="Toad Meme"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Success! 🎉&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Words
&lt;/h2&gt;

&lt;p&gt;That's all it takes to set up and code a Discord bot in Python! Just a note: the bot will respond to you as long as the program is kept running. If you close your terminal or turn off your computer, it will no longer be running. If you want to keep the program running forever, we’ll have to deploy it to another computer in the cloud. However, that is a lesson for another day.&lt;/p&gt;

&lt;p&gt;Hope you enjoyed the tutorial. If you have any questions, you can DM me on twitter at &lt;a href="https://twitter.com/hongjjeon" rel="noopener noreferrer"&gt;@hongjjeon&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  More Resources
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/codedex-io/projects/blob/main/projects/build-a-discord-bot-with-python/bot.py" rel="noopener noreferrer"&gt;Solution on GitHub&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://discord.com/developers/docs/intro" rel="noopener noreferrer"&gt;Discord Developer Portal&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;About the Author:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Hong Jeon is a Software Engineer at Waymo (formerly known as the Google self-driving car project). Hong discovered his passion for sharing knowledge in college during his time as a Teaching Assistant in several programming classes. You can catch him on Twitter at &lt;a href="https://twitter.com/hongjjeon" rel="noopener noreferrer"&gt;@hongjjeon&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>python</category>
      <category>discord</category>
      <category>tutorial</category>
      <category>codedex</category>
    </item>
  </channel>
</rss>
