{"componentChunkName":"component---src-pages-markdown-remark-fields-slug-js","path":"/engineering/how-to-make-telegram-bot/","result":{"data":{"markdownRemark":{"id":"a5a7078d-d20d-5fcb-81ed-3c3d7dd4072f","excerpt":"What is Bot? According to Wikipedia, It is a software application that runs automated tasks (scripts) over the Internet. In other terms, It is a program written…","html":"<h2 id=\"what-is-bot\" style=\"position:relative;\"><a href=\"#what-is-bot\" aria-label=\"what is bot permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What is Bot?</h2>\n<p>According to Wikipedia, It is a software application that runs automated tasks (scripts) over the Internet. In other terms, It is a program written to mimic human behaviour in order to perform some tasks. Bots take input and do simple and repetitive tasks, which is much faster than humans. </p>\n<p>There are two types of bots:</p>\n<ul>\n<li>Good Bots: Bots that are  beneficial to organizations as well as individuals such as ChatBots, Social Media bots, etc</li>\n<li>Bad Bots: These are the bots that are used to perform malicious activities such as Scraping and Spamming.</li>\n</ul>\n<p>In this, We will make Telegram Chatbot, which would send a copy of the input that the user has sent.</p>\n<h2 id=\"making-our-telegram-chatbot\" style=\"position:relative;\"><a href=\"#making-our-telegram-chatbot\" aria-label=\"making our telegram chatbot permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Making our Telegram Chatbot</h2>\n<p>We will be using <strong>Python language</strong> to make the bot and will be using <strong>Telegram package</strong> for our bot.\nYou can know more about the telegram package from <a href=\"https://python-telegram-bot.readthedocs.io/en/stable/telegram.html\">here</a></p>\n<p>Firstly, we will generate our telegram token. Below are the steps to follow to generate your own token.</p>\n<ol>\n<li>Search BotFather on Telegram.</li>\n<li>Type <strong>/start</strong> to get started.</li>\n<li>Type <strong>/newbot</strong> to get a bot.</li>\n<li>Enter your Bot name and unique Username, which should end with the bot.</li>\n<li>Then, you would get your Bot token.</li>\n</ol>\n<p>After generating our token, we will make a python program to create a Telegram bot that will send the Text, Emojis, and Stickers similar to the user's input.</p>\n<ul>\n<li>Importing libraries required.</li>\n</ul>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk15\">import</span><span class=\"mtk1\"> logging</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">from</span><span class=\"mtk1\"> telegram.ext </span><span class=\"mtk15\">import</span><span class=\"mtk1\"> Updater, Filters, CommandHandler, MessageHandler</span></span></code></pre>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">logging.basicConfig(</span><span class=\"mtk12\">format</span><span class=\"mtk1\">=</span><span class=\"mtk8\">&#39;</span><span class=\"mtk4\">%(asctime)s</span><span class=\"mtk8\"> - </span><span class=\"mtk4\">%(name)s</span><span class=\"mtk8\"> - </span><span class=\"mtk4\">%(levelname)s</span><span class=\"mtk8\"> - </span><span class=\"mtk4\">%(message)s</span><span class=\"mtk8\">&#39;</span><span class=\"mtk1\">,     </span><span class=\"mtk3\">#take time,level,name</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                    </span><span class=\"mtk12\">level</span><span class=\"mtk1\">=logging.INFO)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">logger = logging.getLogger(</span><span class=\"mtk12\">__name__</span><span class=\"mtk1\">)</span></span></code></pre>\n<p>This script does a basic configuration for the logging system. It takes time, level, and name.</p>\n<ul>\n<li>Now, we will make a variable Token, which will take your Telegram token.</li>\n</ul>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">TOKEN = </span><span class=\"mtk8\">&quot;ENTER YOUR TOKEN&quot;</span><span class=\"mtk1\">  </span></span></code></pre>\n<ul>\n<li>After writing the token, we will make some functions that our bot will perform.</li>\n</ul>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk4\">def</span><span class=\"mtk1\"> </span><span class=\"mtk11\">start</span><span class=\"mtk1\">(</span><span class=\"mtk12\">bot</span><span class=\"mtk1\">,</span><span class=\"mtk12\">update</span><span class=\"mtk1\">):</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    name  = update.message.from_user.first_name  </span><span class=\"mtk3\">#first name of the user messaging</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    reply = </span><span class=\"mtk8\">&quot;Hi!! </span><span class=\"mtk4\">{}</span><span class=\"mtk8\">&quot;</span><span class=\"mtk1\">.format(name)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    bot.send_message(</span><span class=\"mtk12\">chat_id</span><span class=\"mtk1\"> = update.message.chat_id, </span><span class=\"mtk12\">text</span><span class=\"mtk1\"> = reply)      </span><span class=\"mtk3\">#sending message</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">def</span><span class=\"mtk1\"> </span><span class=\"mtk11\">help</span><span class=\"mtk1\">(</span><span class=\"mtk12\">bot</span><span class=\"mtk1\">,</span><span class=\"mtk12\">update</span><span class=\"mtk1\">):</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    reply = </span><span class=\"mtk8\">&quot;How can I help You&quot;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    bot.send_message(</span><span class=\"mtk12\">chat_id</span><span class=\"mtk1\"> = update.message.chat_id, </span><span class=\"mtk12\">text</span><span class=\"mtk1\"> = reply)  </span><span class=\"mtk3\">#sending message</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">def</span><span class=\"mtk1\"> </span><span class=\"mtk11\">echo_text</span><span class=\"mtk1\">(</span><span class=\"mtk12\">bot</span><span class=\"mtk1\">,</span><span class=\"mtk12\">update</span><span class=\"mtk1\">):</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    reply = update.message.text</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    bot.send_message(</span><span class=\"mtk12\">chat_id</span><span class=\"mtk1\"> = update.message.chat_id, </span><span class=\"mtk12\">text</span><span class=\"mtk1\"> = reply)</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">def</span><span class=\"mtk1\"> </span><span class=\"mtk11\">sticker</span><span class=\"mtk1\">(</span><span class=\"mtk12\">bot</span><span class=\"mtk1\">,</span><span class=\"mtk12\">update</span><span class=\"mtk1\">):</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    reply = update.message.sticker.file_id</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    bot.send_sticker(</span><span class=\"mtk12\">chat_id</span><span class=\"mtk1\"> = update.message.chat_id, </span><span class=\"mtk12\">sticker</span><span class=\"mtk1\"> = reply)</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">def</span><span class=\"mtk1\"> </span><span class=\"mtk11\">error</span><span class=\"mtk1\">(</span><span class=\"mtk12\">bot</span><span class=\"mtk1\">,</span><span class=\"mtk12\">update</span><span class=\"mtk1\">):</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    logger.error(</span><span class=\"mtk8\">&quot;Shit!! Update </span><span class=\"mtk4\">{}</span><span class=\"mtk8\"> caused error </span><span class=\"mtk4\">{}</span><span class=\"mtk8\">&quot;</span><span class=\"mtk1\">.format(update,update.error))</span></span></code></pre>\n<p><strong>start</strong> function would be taking the user's input and would be sending, Hi, with the user's name.</p>\n<p><strong>help</strong> function will prompt the message of how can I help you.</p>\n<p><strong>echo_text</strong> and <strong>sticker</strong> function will send the same message or the stickers which the user has sent.</p>\n<p><strong>error</strong> function would be printing the error message on the command prompt.</p>\n<ul>\n<li>Finally, we will make the main function, which would be executed first on running the program. It will take the updates and handle the updates.</li>\n</ul>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk4\">def</span><span class=\"mtk1\"> </span><span class=\"mtk11\">main</span><span class=\"mtk1\">():</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    updater = Updater(TOKEN)  </span><span class=\"mtk3\">#take the updates</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    dp = updater.dispatcher   </span><span class=\"mtk3\">#handle the updates</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    dp.add_handler(CommandHandler(</span><span class=\"mtk8\">&quot;start&quot;</span><span class=\"mtk1\">, start))</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    dp.add_handler(CommandHandler(</span><span class=\"mtk8\">&quot;help&quot;</span><span class=\"mtk1\">, </span><span class=\"mtk11\">help</span><span class=\"mtk1\">))</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    dp.add_handler(MessageHandler(Filters.text, echo_text))   </span><span class=\"mtk3\">#if the user sends text</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    dp.add_handler(MessageHandler(Filters.sticker, sticker))  </span><span class=\"mtk3\">#if the user sends sticker</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    dp.add_error_handler(error)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    updater.start_polling()</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    logger.info(</span><span class=\"mtk8\">&quot;Started...&quot;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    updater.idle()</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">if</span><span class=\"mtk1\"> </span><span class=\"mtk12\">__name__</span><span class=\"mtk1\">==</span><span class=\"mtk8\">&quot;__main__&quot;</span><span class=\"mtk1\">:</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    main()</span></span></code></pre>\n<blockquote>\n<p>Save the program and execute it to check the working of the telegram bot on telegram.</p>\n</blockquote>\n<p>You can get the source code to make the Telegram bot from this Github Repository <a href=\"https://github.com/LoginRadius/engineering-blog-samples/tree/master/Telegram-Bot\">Bot :robot:</a>. You can also make some changes to the code to make the bot do cool stuff such as Getting News, Articles, or Movies recommendations.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n  .dark-default-dark .mtk15 { color: #C586C0; }\n  .dark-default-dark .mtk1 { color: #D4D4D4; }\n  .dark-default-dark .mtk12 { color: #9CDCFE; }\n  .dark-default-dark .mtk8 { color: #CE9178; }\n  .dark-default-dark .mtk4 { color: #569CD6; }\n  .dark-default-dark .mtk3 { color: #6A9955; }\n  .dark-default-dark .mtk11 { color: #DCDCAA; }\n</style>","headings":[{"value":"What is Bot?","depth":2},{"value":"Making our Telegram Chatbot","depth":2}],"fields":{"slug":"/engineering/how-to-make-telegram-bot/"},"frontmatter":{"metatitle":null,"metadescription":null,"description":"What is a bot and How to make our own Telegram bot.","title":"How to make a telegram bot","canonical":null,"date":"October 20, 2020","updated_date":null,"tags":["Telegram Bot","Python"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.4814814814814814,"src":"/static/aceaa6a49195f6d0417cb80f93f56be4/2ad7f/bot.webp","srcSet":"/static/aceaa6a49195f6d0417cb80f93f56be4/1c9b5/bot.webp 200w,\n/static/aceaa6a49195f6d0417cb80f93f56be4/f1752/bot.webp 400w,\n/static/aceaa6a49195f6d0417cb80f93f56be4/2ad7f/bot.webp 800w,\n/static/aceaa6a49195f6d0417cb80f93f56be4/e7405/bot.webp 1200w,\n/static/aceaa6a49195f6d0417cb80f93f56be4/99b9e/bot.webp 1280w","sizes":"(max-width: 800px) 100vw, 800px"}}},"author":{"id":"Rajan Puri","github":"rjn01","bio":"Just a beginner trying to learn more and more","avatar":null}}}},"pageContext":{"id":"a5a7078d-d20d-5fcb-81ed-3c3d7dd4072f","fields__slug":"/engineering/how-to-make-telegram-bot/","__params":{"fields__slug":"engineering"}}},"staticQueryHashes":["1171199041","1384082988","1711371485","1753898100","2100481360","229320306","23180105","528864852"]}