萊姆
萊姆

我是萊姆,現居台北的軟體工程師,曾經在北京生活兩年,旅遊魂在我內心,喜歡跟不同城市不同地區的人們一起交流。世界之大,我想去看看。

Build my Telegram translation bot from scratch

(edited)
The smoothness of Telegram in large groups makes me like it. The information of the currency circle is very dependent on the community platform. How to automate the filtering and streaming of information will be the key to the timing. The deployment of automatic translation robots will be the solution.

The summary of the previous situation, such as the sub-label, wants to automate the first-hand news of the currency circle at a unified endpoint, so I have an idea (connecting Twitter crawler --> Telegram --> Automatic translation robot --> Telegram ), you You may be curious, why not Twitter crawler --> Backedn Process --> Telegram, to make the process more concise, because I currently use the ready-made free service IFTTT to complete the crawler in the previous paragraph, because it saves the investment required by the crawler and is lazy. The added complexity is acceptable, so the final tuning is for this process.

flowchart for twitterInfoStreamer

IFTTT snoops on Twitter celebrities

Originally, I wanted to write my own code to crawl twitter, but it is not a small effort for high-frequency crawlers to implement and maintain their own efforts. After investigation, I found that IFTTT can provide crawler services from five sources, which means that the free version can subscribe to five Twitter celebrities, and you can pay for it. Unlocking more depends on your need for information acquisition.

In terms of service connotation, since it is a crawler service, the content of the crawler and the time difference of the crawler should be concerned. In the content part, we have not encountered any data loss yet. Basically, after tweeting by Twitter celebrities, you can get relevant information on Telegram; the time difference of the crawler is about 1 minute. I am personally very satisfied. To sum up, IFTTT is finally used to replace the original idea of building wheels. Unless you want to tune in the time difference in the future, you may choose paid services instead of building your own.

In this paragraph, I did not explain the setting process of IFTTT in detail. If readers are interested, I can explain the details of this service in the next article, or communicate in the message area.

Good information should be close to the user

After the first-hand information is entered into Telegram, in fact, if the students are good in English, basically the follow-up translation connection is not necessary, but because the crawler has a little time difference in the early stage, if you encounter difficulties in English reading, if you want to use this information to do The trading partners in the currency circle may have missed a lot of time, so then I want to connect the automatic translation service to make the information easier to read, and also allow non-native English speakers to grasp the key points of the information faster.

Telegram bot settings

I connect Telegram messages through the Telegram Bot API, get the message translation and then send it back to the group, so there are two pieces of content that need to be processed here, that is, the acquisition of the message, and the incoming of the message.

1. Create a robot and obtain authorization

botfather telegram

(1) Robot creation: The creation of Telegram robots is created through the BotFather channel. You can directly search for BotFather, /newbot , and enter the name of the robot you want to create to complete the creation. It is worth noting that the suffix of the robot must be the bottom line bot , or Bot is over.

(2) Get bot_token: BotFather will tell you his bot_token immediately after creation. If you forget it, you can ask BotFather again. I will copy it to my notebook immediately~

(3) Add the robot to the specified group: In the specified group, select Join Member, enter the name you just gave the robot, and click Join to see the robot~

(4) Get chat_id: This Id is the Id of your group, which can be found directly in the browser through the following URL, https://api.telegram.org/botTOKEN/getUpdates , please change the TOKEN to what BotFather told you just above that bot_token. You should see the following information, chat_id is the id that starts with a minus sign, for example, it is -6XXXXXXXX .

 {"ok":true,"result":[{"update_id":376020027,"message":{"message_id":11,"from":{"id":212XXXXXX,"is_bot":false},"chat ":{"id":-6XXXXXXXX,"type":"group","all_members_are_administrators":true},"date":1645942772,"text":"test"}},...

2. Obtain conversation information through Bot API

This part is easier, as long as you go through the above https://api.telegram.org/botTOKEN/getUpdates , you can get the group chat after joining from the bot, but he has a cache clearing time, which will be refreshed every day , so the information of the next day should be saved in your local, otherwise it will be broken.

3. Incoming conversation information through the Bot API

Here, you can also directly type https://api.telegram.org/botTOKEN/sendMessage?chat_id=CHAT_ID&parse_mode=Markdown&text=what you want to say , replace TOKEN with bot_token, and replace CHAT_ID with chat_id, what you want to say is what you want to say XD , you can send "what you want to say" to the group just now.

Complete translation service with Python

The road is open, and the last part is to translate the text. In this case, I will use the python three-party library and translate-api to assist me in translation. The translation service can be completed with a few lines of code. It feels very convenient. I haven't stepped on the thunder, maybe because my usage scenario is not high traffic! If you encounter difficulties, please leave a message and let us take a look.

1. Install translate-api

 pip install translate-api

2. Translate the original text into traditional Chinese through google translation: here is just an example of google, translate-api is very powerful and supports many translation engines, and can specify a lot of language directions, if you want to study in depth, or use it in different scenarios , you can refer to this document .

 import translators as ts
ts.google(text, to_language="zh-TW")

Summarize

Seeing this, I believe you have also completed this Twitter celebrity subscription translation service. The article is not long, and many details may only be realized through hands-on work. If there is anything unclear in my article, I look forward to your message to communicate with me. Telegram bots are very diverse, this is just the beginning, I hope this article will help you who need to build your first bot, and you are welcome to share your creations with me.

Feedback can motivate me to continue to create, if you think the writing is good, I will be very grateful; if there is anything that can be adjusted, please let me know directly, and finally, thank you / you who took the time to read this article .

CC BY-NC-ND 2.0

Like my work?
Don't forget to support or like, so I know you are with me..

Loading...

Comment