{"id":723,"date":"2023-12-09T17:58:39","date_gmt":"2023-12-09T17:58:39","guid":{"rendered":"https:\/\/avalon.land\/blog\/?p=723"},"modified":"2023-12-10T14:49:33","modified_gmt":"2023-12-10T14:49:33","slug":"telegram-bot-v2-0","status":"publish","type":"post","link":"https:\/\/avalon.land\/blog\/it\/telegram-bot-v2-0\/","title":{"rendered":"Telegram bot v2.0"},"content":{"rendered":"<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" width=\"360\" height=\"240\" class=\"size-full wp-image-732\" src=\"\/blog\/wp-content\/uploads\/2023\/12\/bender.webp\" alt=\"2\" srcset=\"https:\/\/avalon.land\/blog\/wp-content\/uploads\/2023\/12\/bender.webp 360w, https:\/\/avalon.land\/blog\/wp-content\/uploads\/2023\/12\/bender-300x200.webp 300w\" sizes=\"auto, (max-width: 360px) 100vw, 360px\" \/><\/p>\n<p>\u0410\u0441\u0442\u0440\u043e\u043b\u043e\u0433\u0438 \u043e\u0431\u044a\u044f\u0432\u0438\u043b\u0438 \u043c\u0435\u0441\u044f\u0446 \u0431\u043e\u044f\u043d\u043e\u0432 &#8211; \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0442\u0435\u043b\u0435\u0433\u0440\u0430\u043c \u0431\u043e\u0442\u043e\u0432 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u043b\u043e\u0441\u044c \u0432\u0434\u0432\u043e\u0435!<br \/>\n<!--more--><\/p>\n<p>\u0417\u0434\u0430\u0440\u043e\u0432\u0430 \u0449\u0435\u0433\u043b\u044b! \u0423\u0436\u0435 \u0431\u044b\u043b\u0430 \u0442\u0430\u043a\u0430\u044f \u0442\u0435\u043c\u0430, \u043d\u043e \u0432 \u043f\u0440\u043e\u0448\u043b\u044b\u0439 \u0440\u0430\u0437 \u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0435 \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u0438 \u0431\u043b\u043e\u0436\u0435\u043a\u0430 (\u0434\u0432\u0430 \u0440\u0430\u043d\u0434\u043e\u043c\u043d\u044b\u0445 \u0447\u0435\u043b\u0430) \u0443\u043a\u0430\u0437\u0430\u043b\u0438 \u0430\u0432\u0442\u043e\u0440\u0443 \u043d\u0430 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u0448\u0438\u0431\u043a\u0438 \u0432 \u0438\u0441\u0441\u043b\u0435\u0434\u0443\u0435\u043c\u043e\u0439 \u0442\u0435\u043c\u0435 &#8211; \u0433\u0430\u0439\u0434 \u043d\u0435\u043c\u043d\u043e\u0436\u043a\u043e \u0443\u0441\u0442\u0430\u0440\u0435\u043b. \u0421\u043f\u0443\u0441\u0442\u044f 6 \u043b\u0435\u0442. \u0423\u0441\u0442\u0430\u0440\u0435\u043b \u043e\u043d. \u0421\u043e\u0432\u0441\u0435\u043c \u0447\u0443\u0442\u044c-\u0447\u0443\u0442\u044c. \u041a\u0442\u043e \u0431\u044b \u043c\u043e\u0433 \u043f\u043e\u0434\u0443\u043c\u0430\u0442\u044c? \u041d\u0443 \u0440\u0430\u0437 \u043e\u0431\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u044f \u0431\u0430\u043d\u043a\u0435\u0442\u0430, \u0448\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u0434\u0435\u043b\u0430\u0442\u044c \u0441 \u044d\u0442\u0438\u043c \u0430\u0444\u0442\u043e\u0440? \u041f\u0440\u0438\u0434\u0451\u0442\u0441\u044f \u043f\u0438\u0441\u0430\u0442\u044c. \u0410 \u043f\u043e\u0442\u043e\u043c \u043f\u0438\u0441\u0410\u0442\u044c.<\/p>\n<p>\u041d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u044f\u0442\u0441\u044f:<br \/>\n0. \u0412\u0438\u0440\u0442\u0443\u0430\u043b\u043a\u0430 \u0441 \u0432\u0430\u0448\u0435\u0439 \u043b\u044e\u0431\u0438\u043c\u043e\u0439 \u041e\u0421 \u0438 \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u043e\u043c (\u043e\u0434\u043d\u0430 \u0448\u0442\u0443\u043a\u0430)<br \/>\n1. \u0414\u043e\u043a\u0435\u0440 (\u043e\u0434\u043d\u0430 \u0448\u0442\u0443\u043a\u0430)<br \/>\n2. \u041f\u0440\u043e\u043a\u043b\u0430\u0434\u043a\u0430 \u043c\u0435\u0436\u0434\u0443 \u043a\u0430\u043c\u043f\u043b\u044e\u043a\u0442\u0435\u0440\u043e\u043c \u0438 \u043a\u0440\u0435\u0441\u043b\u043e\u043c (\u043c\u043e\u0436\u043d\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e)<\/p>\n<p>\u0417\u0430 \u0442\u0435 6 \u043b\u0435\u0442 \u0432 \u0442\u043e\u043f\u044b \u0433\u0438\u0442\u0445\u0430\u0431\u0430 \u0432\u044b\u0431\u0438\u043b\u0441\u044f \u043f\u0440\u043e\u0435\u043a\u0442 <strong><a href=\"https:\/\/github.com\/python-telegram-bot\/python-telegram-bot\" rel=\"noopener\" target=\"_blank\">python-telegram-bot<\/a><\/strong> \u0435\u0433\u043e \u0438 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c. \u0411\u0435\u0440\u0451\u043c \u0441\u0430\u043c\u044b\u0439 \u0431\u0430\u043d\u0430\u043b\u044c\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u044d\u0445\u043e\u0431\u043e\u0442\u0430 \u0438 \u0434\u0435\u043b\u0430\u0435\u043c \u0438\u0437 \u043d\u0435\u0433\u043e \u044d\u0445\u043e-\u0445\u0430\u043c\u0430 \u043c\u0438\u0437\u0430\u043d\u0442\u0440\u043e\u043f\u0430. <\/p>\n<p>\u041f\u043e\u0434\u0433\u043e\u0442\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u0435, \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u0438 \u043f\u0438\u0448\u0435\u043c dockerfile:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nmkdir -p docker\/bot_v2\r\ncd docker\/bot_v2\r\n\r\necho &quot;python-telegram-bot&#x5B;all]==20.7&quot; &gt; requirements.txt\r\n\r\ncat &lt;&lt;EOF &gt;Dockerfile\r\nFROM python:3.12\r\n\r\nRUN mkdir \/app\r\nWORKDIR \/app\r\nCOPY . \/app\r\nRUN pip install -r \/app\/requirements.txt\r\n\r\nCMD python \/app\/chamlo.py\r\nEOF\r\n<\/pre>\n<p>\u0417\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u0432 \u0444\u0430\u0439\u043b <strong>.env<\/strong> \u0442\u043e\u043a\u0435\u043d \u043a \u0441\u0432\u043e\u0435\u043c\u0443 \u0431\u043e\u0442\u0443 (\u0433\u0434\u0435 \u044d\u0442\u043e \u0432\u0437\u044f\u0442\u044c &#8211; \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432 <strong><a href=\"https:\/\/avalon.land\/blog\/it\/telegram-bot-on-centos7\/\" rel=\"noopener\" target=\"_blank\">\u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435<\/a><\/strong>)<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\necho &quot;TOKEN=\u0422\u0423\u0422_\u0422\u0412\u041e\u0419_\u0422\u041e\u041a\u0415\u041d:\u041d\u0415_\u041a\u041e\u041f\u0418\u0420\u0423\u0419_\u0422\u0423\u041f\u041e_\u042d\u0422\u041e_\u0413\u0410\u0412\u041d\u041e&quot; &gt; .env\r\n<\/pre>\n<p>\u041f\u0438\u0448\u0435\u043c \u0432\u0438\u043d\u043e\u0432\u043d\u0438\u043a\u0430 \u0442\u043e\u0440\u0436\u0435\u0441\u0442\u0432\u0430:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\ncat &lt;&lt;EOF &gt;chamlo.py\r\n#!\/usr\/bin\/env python\r\n# -*- coding: utf-8 -*-\r\n# pylint: disable=unused-argument\r\n# This program is dedicated to the public domain under the CC0 license.\r\n\r\n&quot;&quot;&quot;\r\nSimple Bot to reply to Telegram messages.\r\n\r\nFirst, a few handler functions are defined. Then, those functions are passed to\r\nthe Application and registered at their respective places.\r\nThen, the bot is started and runs until we press Ctrl-C on the command line.\r\n\r\nUsage:\r\nBasic Echobot example, repeats messages.\r\nPress Ctrl-C on the command line or send a signal to the process to stop the\r\nbot.\r\n&quot;&quot;&quot;\r\n\r\nimport logging\r\nimport os\r\n\r\nfrom telegram import ForceReply, Update\r\nfrom telegram.ext import Application, CommandHandler, ContextTypes, MessageHandler, filters\r\n\r\n# Enable logging\r\nlogging.basicConfig(\r\n    format=&quot;%(asctime)s - %(name)s - %(levelname)s - %(message)s&quot;, level=logging.INFO\r\n)\r\n# set higher logging level for httpx to avoid all GET and POST requests being logged\r\nlogging.getLogger(&quot;httpx&quot;).setLevel(logging.WARNING)\r\nlogger = logging.getLogger(__name__)\r\n\r\nTOKEN = os.getenv(&quot;TOKEN&quot;)\r\n\r\n# Define a few command handlers. These usually take the two arguments update and\r\n# context.\r\nasync def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -&gt; None:\r\n    &quot;&quot;&quot;Send a message when the command \/start is issued.&quot;&quot;&quot;\r\n    user = update.effective_user\r\n    logger.info(&quot;%s started the conversation.&quot;, user)\r\n    await update.message.reply_html(\r\n        rf&quot;\u0418\u0434\u0438 \u043d\u0430\u0445\u0435\u0440 {user.mention_html()}! \u042f \u0442\u0435\u0431\u0435 \u043d\u0435 \u0437\u0432\u0430\u043b!&quot;,\r\n        reply_markup=ForceReply(selective=True),\r\n    )\r\n\r\nasync def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -&gt; None:\r\n    &quot;&quot;&quot;Send a message when the command \/help is issued.&quot;&quot;&quot;\r\n    await update.message.reply_text(&quot;\u0414\u0430 \u0442\u0435\u0431\u0435 \u0434\u0430\u0436\u0435 \u043c\u0435\u0434\u0438\u0446\u0438\u043d\u0430 \u043d\u0435 \u043f\u043e\u043c\u043e\u0436\u0435\u0442!&quot;)\r\n\r\n\r\nasync def echo(update: Update, context: ContextTypes.DEFAULT_TYPE) -&gt; None:\r\n    &quot;&quot;&quot;Echo the user message.&quot;&quot;&quot;\r\n    user = update.effective_user\r\n    logger.info(&quot;%s said: %s&quot;, user, update.message.text)\r\n    await update.message.reply_text(f&quot;\u0412\u0441\u0435 \u0433\u043e\u0432\u043e\u0440\u044f\u0442 \\&quot;{update.message.text}\\&quot;, \u0430 \u0442\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u043e\u0437\u044c\u043c\u0438 \u0434\u0430 \u043f\u043e\u0439\u0434\u0438 \u043d\u0430\u0445\u0435\u0440!&quot;)\r\n\r\ndef main() -&gt; None:\r\n    &quot;&quot;&quot;Start the bot.&quot;&quot;&quot;\r\n    # Create the Application and pass it your bot&#039;s token.\r\n    application = Application.builder().token(TOKEN).build()\r\n\r\n    # on different commands - answer in Telegram\r\n    application.add_handler(CommandHandler(&quot;start&quot;, start))\r\n    application.add_handler(CommandHandler(&quot;help&quot;, help_command))\r\n\r\n    # on non command i.e message - echo the message on Telegram\r\n    application.add_handler(MessageHandler(filters.TEXT &amp; ~filters.COMMAND, echo))\r\n\r\n    # Run the bot until the user presses Ctrl-C\r\n    application.run_polling(allowed_updates=Update.ALL_TYPES)\r\n\r\nif __name__ == &quot;__main__&quot;:\r\n    main()\r\nEOF\r\n<\/pre>\n<p>\u0421\u043e\u0431\u0438\u0440\u0430\u0435\u043c \u0438\u0437 <del datetime=\"2023-12-09T16:36:07+00:00\">\u0433\u043e\u0432\u043d\u0430 \u0438 \u043f\u0430\u043b\u043e\u043a<\/del> &#8220;\u0438\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u043e\u0432&#8221;:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\ndocker build --tag chamlo:v2 .\r\ndocker create -m 128M --env-file .env --name bot-chamlo chamlo:v2\r\n<\/pre>\n<p>\u0418, \u0441\u043e\u0431\u0441\u043d\u043e, <del datetime=\"2023-12-09T16:36:07+00:00\">\u0432\u044b\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0433\u0443\u0441\u044f<\/del> \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0447\u0443\u0434\u043e\u0432\u0438\u0449\u0435 <\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">docker start bot-chamlo<\/pre>\n<p><a href=\"https:\/\/avalon.land\/blog\/wp-content\/uploads\/2016\/06\/224896081.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/avalon.land\/blog\/wp-content\/uploads\/2016\/06\/224896081.jpg\" alt=\"\" width=\"400\" height=\"400\" class=\"aligncenter size-full wp-image-293\" srcset=\"https:\/\/avalon.land\/blog\/wp-content\/uploads\/2016\/06\/224896081.jpg 400w, https:\/\/avalon.land\/blog\/wp-content\/uploads\/2016\/06\/224896081-150x150.jpg 150w, https:\/\/avalon.land\/blog\/wp-content\/uploads\/2016\/06\/224896081-300x300.jpg 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/a><\/p>\n<p>\u041a\u0430\u043a \u0432\u0438\u0434\u0438\u043c, \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0438\u0437\u0443\u043c\u0438\u0442\u0435\u043b\u044c\u043d\u043e:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/avalon.land\/blog\/wp-content\/uploads\/2023\/12\/bot-chamlo.png\" alt=\"\" width=\"1336\" height=\"592\" class=\"aligncenter size-full wp-image-728\" srcset=\"https:\/\/avalon.land\/blog\/wp-content\/uploads\/2023\/12\/bot-chamlo.png 1336w, https:\/\/avalon.land\/blog\/wp-content\/uploads\/2023\/12\/bot-chamlo-300x133.png 300w, https:\/\/avalon.land\/blog\/wp-content\/uploads\/2023\/12\/bot-chamlo-1024x454.png 1024w, https:\/\/avalon.land\/blog\/wp-content\/uploads\/2023\/12\/bot-chamlo-768x340.png 768w\" sizes=\"auto, (max-width: 1336px) 100vw, 1336px\" \/><\/p>\n<p>\u0412\u043e\u0442 \u0442\u0430\u043a\u0438\u043c \u043d\u0435\u0445\u0438\u0442\u0440\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043c\u044b \u0443\u043c\u043d\u043e\u0436\u0438\u043b\u0438: \u044d\u043d\u0442\u0440\u043e\u043f\u0438\u044e \u0432\u043e \u0432\u0441\u0435\u043b\u0435\u043d\u043d\u043e\u0439, \u043e\u0441\u043a\u043e\u0440\u0431\u043b\u0435\u043d\u0438\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0435, \u0441\u043b\u0451\u0437\u044b \u0430\u0444\u0442\u043e\u0440\u0430 \u043f\u043e \u043f\u043e\u0432\u043e\u0434\u0443 \u043f\u043e\u0442\u0440\u0430\u0447\u0435\u043d\u043d\u043e\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438. \u0425\u042d\u041f\u0418 \u042d\u041d\u0414!<br \/>\nP.S. \u041d\u0430 \u0447\u0442\u043e \u0436\u0435 \u044f \u0442\u0440\u0430\u0447\u0443 \u0441\u0432\u043e\u044e \u0436\u0438\u0437\u043d\u044c, \u043f\u0430\u043c\u0430\u0433\u0438\u0442\u0438 \ud83d\ude41 <\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u0410\u0441\u0442\u0440\u043e\u043b\u043e\u0433\u0438 \u043e\u0431\u044a\u044f\u0432\u0438\u043b\u0438 \u043c\u0435\u0441\u044f\u0446 \u0431\u043e\u044f\u043d\u043e\u0432 &#8211; \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0442\u0435\u043b\u0435\u0433\u0440\u0430\u043c \u0431\u043e\u0442\u043e\u0432 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u043b\u043e\u0441\u044c \u0432\u0434\u0432\u043e\u0435!<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[],"class_list":["post-723","post","type-post","status-publish","format-standard","hentry","category-it","entry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/avalon.land\/blog\/wp-json\/wp\/v2\/posts\/723","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/avalon.land\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/avalon.land\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/avalon.land\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/avalon.land\/blog\/wp-json\/wp\/v2\/comments?post=723"}],"version-history":[{"count":12,"href":"https:\/\/avalon.land\/blog\/wp-json\/wp\/v2\/posts\/723\/revisions"}],"predecessor-version":[{"id":739,"href":"https:\/\/avalon.land\/blog\/wp-json\/wp\/v2\/posts\/723\/revisions\/739"}],"wp:attachment":[{"href":"https:\/\/avalon.land\/blog\/wp-json\/wp\/v2\/media?parent=723"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/avalon.land\/blog\/wp-json\/wp\/v2\/categories?post=723"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/avalon.land\/blog\/wp-json\/wp\/v2\/tags?post=723"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}