Toolkit and Telegram/Discord Bot to enforce one language in the group chat. How does it do it?
If LangPolizei detects that the language doesn't match the one specified in .env
file, it rebukes and/or mutes the user for the specified period of time.
The bot, once added to the group chat, should be granted with admin privileges. Admins can also add particular words as "exceptions". The bot will be relaxed about them in the future.
To add or remove an exception
To suppress bot warnings for "yeah boii" string in the future, write the following in the group chat.
/except yeah boii
or
/remove yeah boii
Do git clone
and npm install
. You'll also need to set a few environment variables and projects.
- Create a new Firebase project
- Enable Cloud Firestore
- Go to Project Settings -> Overview -> Service accounts and "Generate a new private key". A
.json
file will be downloaded - That
.json
contains a lot of stuff. Findproject_id
,client_email
andprivate_key
and copy them to the their respective environment variables in.env
file.
TELEGRAM_TOKEN
- Telegram Bot API key
and/or
DISCORD_TOKEN
- Discord Bot API key
GCP_API_KEY
- API key to the Google Translate API. Enable this API and use it (it always starts with "Alza")
REQUIRED_LANG
- two-digit language code which the bot will consider as the correct language. English is en
, French is fr
, German is de
, Polish is pl
and so on...
PROJECT_ID
- Firebase Project ID
CLIENT_EMAIL
- Firebase Service Account's email address
PRIVATE_KEY
- Firebase Service Account's private key
BE_HELPFUL
- true
if bot should translate messages, false
otherwise
MUTE_PEOPLE
– true
if the bot should mute people for using bad language, false
otherwise
MUTE_TIMEOUT
– mute duration, in milliseconds (default 30 000)
It uses Cloud Firestore, so you'll need to create a new Firestore project.
Finally, run npm run [PLATFORM NAME]
to start the bot
This project emerged from a Telegram group of Google Code-in 2019 winners. A first working version was created in less than 1 day by me with help of some other people. Initially, it was just another fun project written in JavaScript. As it grew and features were added, I rewrote it TypeScript and modularized to make it possible to add support for other messaging platforms.
This project is basically a toolkit containing some useful methods (gathered in src/core.ts
) and actual
bot implementations for particular platforms (currently only for Telegram – bot_telegram.ts
and Discord bot_discord.ts
).
core.ts
in completely platform-independent.
It uses:
- Google Translate API to detect the message's language
- Cloud Firestore to save exceptions
The Telegram Bot implementation takes advantage of the Telegram Bot API, and the Discord Bot implementation of the Discord Bot API.
If you want to create a similar bot for some other messenger service (IRC, Slack, etc), we'll be happy to help you. Only one very smart person has done that before (@diogoscf), so you'll be our second early adopter :)
- Take a look at how
bot_telegram.ts
andbot_discord.ts
are written - You'll see that they do platform-specific integration stuff and delegate the business logic to
core.ts
- Try to somehow replicate the behaviour of the Telegram/Discord Bot with your messenger platform of choice
PS If you find any bugs or have some ingenious idea on how something could be improved, we'll be happy to accept your Pull Requests.
- ban only if
confidence
is big enough. If it isn't, don't mute, just send a message like "What you sent doesn't seem to be in English..." (after evaluation: not a good solution, confidence is often wrong) - make the bot truly helpful (suggested by @MatejMecka). Make it translate the messages instead of banning people
- rewrite it to a real and serious object-oriented programming language (like Kotlin). We could create a interface for the bot, an abstract class implementing the basic behaviours and let the actual bot implementations handle platform-specific stuff.
Example response from Google Translate API:
{
"data": {
"detections": [
[
{
"confidence": 0.7859922051429749,
"isReliable": false,
"language": "en"
}
]
]
}
}