Modern programming: a look after 25 years of break or as I wrote the bot for Telegram

The task seems simple when you know nothing about it and when you decide.



On one “wonderful” day, I realized that I’m not interested in sitting in numerous forums, but I want to create my own channel and share “wisdom”.



I like Telegram features embedded in it including bots, so the channel in the form of a blog was created there. He began to look for bots to help make out messages .... and since I want to not find it. Well, write it yourself. Smart people are advised to write in Python.



I read the first tutorial on the Internet on 149 pages. I am familiar with Pascal, FoxPro, Interbase and even (ha ha 3 times) 20 years ago I sold my programs, and then somehow it did not work out, I went into trade. Working as a programmer helped me a lot in setting tasks for coders by the way. But back to Python, it seems, nothing complicated, because it was possible to program on BASIC as well, and this is not my institute diploma with a hardware-software complex of the MIG verification generator on Assembler. One problem, it was all a long time ago, so returning to the headline - it seems that it's simple, because I don’t know anything about the task yet, but it's worth a try.



I decided that it is most convenient to make a project from a smartphone to Android, because it is always at hand. So put:



  1. Pydroid 3 - IDE for Python 3 Actually Python for Android.
  2. @BotFather, In Telegram, we install the father of all bots - you will need it to get the identifier of your bot and make basic settings.
  3. Rebootr Application for launching a project on heroku.
  4. GIT An application for online hosting of repositories.
  5. Windscribe Without VPN anywhere?
  6. Termux Terminal emulator and Linux environment.


And what's the bottom line? Programming on a smartphone is possible in theory, but impossible in practice. Check the code as much as possible, fix the error, investigate the performance.



Such a long introduction, and what I wanted to get from my bot. At first it seemed to me enough to add a Hashtag at the end of the message. Then the thought came to “cover” it with a link to my channel, so with repost there will be an additional enticement to the channel source. Then, he added the automation of the source link, where I get the message from. Next, I will make a digression. Advertising, of course, is the engine of progress. But sometimes its amount goes through the roof. Telegram was chosen by me, among other things, because there is an opportunity to fight the latter. I respect the readers of my channel and therefore I clean the interesting news found on Ineta from advertising and send it to the channel. At the same time, I respect the authors and almost always give a link to the source in the form of “Read more ...”. Due to the dislike of advertising, I download content from YouTube and upload it in my channel as a video file. At one certain moment, the bot that downloaded the video went crazy and began to send me ads every hour. So in my bot there was an opportunity to download video from YouTube. Recently, by the way, I met the author of this bot, he was very surprised, because According to him, he sends ads “only” 20 times a month. The same thing happened with the bot that makes the watermark - it was sent to the furnace, and I got the opportunity to make a watermark.



In order to write a program practically from scratch, I had to search a lot of information on the Internet. I hope that those who follow in my footsteps will help. So where did I get the information from and what did I use:



  1. Articles on Habrr really helped me. Therefore, I will not rewrite how and what to install. Everything is there. By the way, I contacted the authors in a personal way and they never refused my help.
  2. github An online repository hosting service with all the features of distributed version control and source control functionality. Primer
  3. heroku is a cloud-based PaaS platform that supports a number of programming languages. Very quickly abandoned him.
  4. pyTelegramBotAPI - One of the main libraries when writing a bot for Telegrams.
  5. Bot Spelling Tutorial
  6. Python 3 for beginners
  7. Bots: information for developers
  8. HTML Reference
  9. Nowhere without a VPN?


A second digression or war plan will show. When I started writing the bot, the first thing I looked at was someone else's codes. If there are no kamenty, one can hardly understand:



The earth shook like our breasts

Horses mixed up in a bunch, people,

And volleys of thousands of guns

Merged into a long howl ...



Lermontov wrote about the code. The heap contains functions, decorators. There is no beauty of the code, nobody cares about resources. Although the beauty is most likely to be seen by the weirdo masochist. The meaning of a familiar programmer’s phrase “Look at the program, maybe you can figure it out” very quickly reached me. The most important brain explosion I had was when I finally realized that the code is event-driven, not sequential. This is a different level.



The second problem is the lack of good documentation. Even in Basurm. I will give an example. I send a picture to telegrams, and he shakes it shamelessly. It turned out that an invalid argument was written in the line:



file_info = bot.get_file(message.photo[-1].file_id)
      
      





And what is the documentation? We go to the source



image



image



This is an array. You can walk along it, study where what size comes back. Thank you, smart people suggested what to prescribe. Although what am I talking about, even if the gurus are at a loss.



When I wrote the watermark code, I used a free font and to make it bold I applied the solution to display the message three times with a pixel offset, I don’t know how beautiful the solution is, but it works.



 font = ImageFont.truetype("Pillow/Tests/fonts/FreeMono.ttf", width//20) pos = (width//4, height - height//10) text = skanal drawing.text(pos, text, fill=black, font=font) pos = (1 + width // 4, 1 + height - height // 10) drawing.text(pos, text, fill=black, font=font) pos = (2 + width // 4, 2 + height - height // 10) drawing.text(pos, text, fill=black, font=font)
      
      





The position, as you see, is selected depending on the size of the image, the font height, too. I immediately came across an interesting point: although the font is an integral part of the PIL library, since the first line says it works locally, but in Docker it doesn’t. The way out is to download it to the repository, add the path to the environment file and write another path in the program.



Another incomprehensible thing for me happened with the picture after processing using the PIL library (immediately after the watermark). I send it to my bot:



  with open(photo_path, 'rb') as fi: bot.send_photo(message.chat.id, fi)
      
      





Everything is great, I like the picture. Then I need to add a comment to the picture and see if everything looks beautiful together? We write:



 bot.send_photo(message.chat.id, message.photo[-1].file_id, caption=' ')
      
      





For some reason, the original, unprocessed picture goes into the bot. Well, let's try to fool: getupdates.offset -1 no sense, Telegram is sure that this is the same photo. Ok, we do this:



  with open(photo_path, 'rb') as fi: info = bot.send_photo(message.chat.id, fi)
      
      





Rewrite:



 bot.send_photo(message.chat.id, info.photo[-1].file_id, caption=' ')
      
      





The result is the same - the original picture is displayed. And only replacing message in the first argument with info gave the desired result.



I’ll also give you a piece of interesting entry-level code from YouTube (NB: without error checking):



 elif message.entities: #    pkanal = 6 for item in message.entities: if item.type == "url" and message.text.find(' ') == -1: if 'youtube.com' in message.text or 'youtu.be' in message.text: #    ydl_opts = {'outtmpl': '/tmp/f.mp3', 'preferredcodec': 'mp3', 'max_filesize': 60000000} link_of_the_video = message.text with youtube_dl.YoutubeDL(ydl_opts) as ydl: ydl.download([link_of_the_video]) bot.delete_message(message.chat.id, message.message_id) if os.path.exists('/tmp/f.mp3'): #   video = open('/tmp/f.mp3', 'rb') bot.send_video(message.chat.id, video) os.remove('/tmp/f.mp3') pkanal = 6 else: #   bot.send_message(message.chat.id, '  ', parse_mode='html', disable_web_page_preview=True)
      
      





For me, the stumbling block was that entities is an array of arrays and you need to “go over” the entire array so that the bot understands that we are working with a link. It also turned out that users send the link to the bot as “Share” from YouTube, so “youtu.be” is also written in the example. I did not come up with a way to immediately send a file to Telegram, so we save it, send it and then delete it. During testing, I was immediately informed that people would start downloading giant files - later I had to introduce a restriction.



Before the bot was released, it suddenly became clear that I did not have a check for access rights to the channel. For example, knowing my channel, any bot user could send a message there, because The bot is an Administrator. I had to urgently do a check:



 if message.from_user.id in [adm_obj.user.id for adm_obj in bot.get_chat_administrators(chat_id)]:
      
      





Here we get the list of Administrators from the channel and see if the author of the message is also an Administrator.



A few words about how you had to fight with Roskomnadzor. Since access to the API is blocked, to develop the bot locally you need to somehow pass traffic through our valiant defenders. You can do this in two ways - through a VPN or through a proxy. The simplest and fastest and “normal” way is the ssh tunnel: we establish a connection between the client and the proxy server, we get a port on the local host where traffic can be sent from our side, and it will come out from the other side (already somewhere in Germany). For convenience, you can add some semblance of automation of this connection - a script and a shortcut on the desktop, which will launch it, if necessary. By the “normal” method, here I mean the situation where we control the entry and exit points ourselves — on the left is our laptop, on the right is the VPS in Germany. in the middle, traffic can be intercepted as much as desired, it is encrypted, and at the exit we find ourselves in the jurisdiction of another state, and the laws of the Russian Federation do not apply to it. As a plus, we get an additional guarantee that the proxy server will not be changed or turned off until we do it on our own. Those. unlike any uncle VPN, we have peace of mind and confidence in the future.



Script



 /home/user/proxy.sh
      
      





Script code



 #!/bin/bash ssh -f -D 1080 user@12.34.56.78 sleep 72000
      
      





After we have established a connection with the server and the port is open, we need to somehow direct traffic there. Walking along the path of least resistance and in order not to think about how to configure proxies in IDE \ docker \ python, you can make one setting at all, proxychains will be such a setting. If you run any software using this utility, then it will redirect traffic through the proxy chain that are registered in the config.



 /etc/proxychains.conf
      
      





In our case, this is one chain and writing it is not difficult.



 socks5 127.0.0.1 1080
      
      





and another crutch that was taken not to configure VPN (sarcasm) is a way to launch a Python application from PyCharm. In the simple case, to run through the proxychains application, just write proxychains app.py and that's it. But the IDE must specify an interpreter. It’s easy to get around this - create a new “run configuration”, select a shell script. and fill in the fields



image



After that, the launch from the point of view of the IDE takes on this form: proxychains python3 app.py - we actually need this. Thus, when we want to start a project, all we need to do is run the script on the desktop and then click the “play” button in ide to launch the bot. All the rest of the magic will be done by proxychains and ssh.



Finally, the bot is written, tested. What's next? On the one hand, I want to declare myself, on the other hand, I think he can really come in handy for someone. And here we are faced with yet another Telegram problem; it does not have a single catalog of channels and bots. It is quite possible a bot similar to mine exists, but I repeat I did not find it. And somewhere in the tram a man sits and suffers, who wants to beautifully arrange a message in the channel and he does everything with pens.



If you already ran my bot, then you saw that there is a hashtag #Advertising. How so? - you ask. And here I went about the issue with end users - many post ads on their channels and quickly got used to writing messages using a bot, they asked me to add. You may not like advertising, fight it, but these are harsh and necessary realities for users.



The project is non-profit, so I think you can call the bot itself @SGK_espace_bot.



And here is the video how to use

I would be grateful for any constructive criticism.



All Articles