- Rust 88.7%
- Fluent 11.3%
| .idea | Rename: remove "-rust" in name. | |
| locales | Fix status message. | |
| src | Update regex to support other server software than Paper. | |
| systemd-service | Introduce regex to sudoers file. Add new user for bot. | |
| .gitignore | Add example bot-config.json | |
| bot-config.json | Update README and bot-config to include information about localisation. | |
| Cargo.lock | Update dependencies. | |
| Cargo.toml | Update dependencies. | |
| LICENSE | Add licence. | |
| minecraft-server-telegram-bot.iml | Rename: remove "-rust" in name. | |
| README.md | Introduce regex to sudoers file. Add new user for bot. | |
Minecraft Server Telegram Bot
This software can be used to start and stop Minecraft servers via systemd and get the current status of servers, all using a Telegram bot. It can also mirror the chat messages in Minecraft to a Telegram chat and vice versa.
To be able to use the software, one has to have a setup similar to the one in explained here. Most importantly, the start and stop commands have to have the same structure as mentioned in the explanation. If this is not the case, you will have to edit the command syntax in the source code of this software.
Configuration
The setup is written as a follow-up to the explanation given in this repository. Especially the steps 1 and 3 of the section ‘Setting up the systemd-service’ are necessary.
Compile the project
The software is written in Rust. Since I do not provide any prebuilt binaries yet, you have to compile the software yourself. Hence, the Rust toolchain has to be installed on your device.
Use cargo build --release to compile the software.
The compiled binary can be found at target/release/minecraft-server-telegram-bot.
Setting up the bot
- Obtain a token from the BotFather. In the following, it is referred to as
<token>. - Create a new user for the bot:
# sudo useradd -r -g minecraft -d "/var/minecraft/telegram-bot" -s "/bin/zsh" minecraft-telegram
- Create a new folder called
/var/minecraft/telegram-bot. - Copy the compiled binary as well as the
bot-config.jsonto that folder and set the rights properly:
# chown -R root:root /var/minecraft/telegram-bot
# chmod 755 /var/minecraft/telegram-bot/minecraft-server-telegram-bot
# semanage fcontext -a -f bin_t '/var/minecraft/telegram-bot/minecraft-server-telegram-bot'
# restorecon -v /var/minecraft/telegram-bot/minecraft-server-telegram-bot
- Open the
bot-config.jsonand edit the configuration as follows:- Enter the obtained token.
- Enter the RCON password used for your servers.
- Edit the locale, if needed. The default is
en-UK. All possible locales are listed in thelocalesdirectory. Feel free to add new locales! Please have a look at the Contributions section. - Use the
chat_server_mapto set which chats may control which servers. Enter the chat id on the left, the server name on the right. It must be an n:1 relation, so one chat may control up to one server, but one server may be controlled by many chats. To see how a chat id for a particular chat can be obtained, see here.
- Create a new
sudoersfile usingvisudo, e.g. using
# visudo -f /etc/sudoers.d/80-minecraft
Then, put
# Allow user minecraft to start and stop systemd service for the minecraft server
minecraft-telegram ALL = NOPASSWD: /usr/bin/systemctl ^(start|is-active|stop) minecraft-server@([a-zA-Z_0-9\.\-]+).service,ドル /usr/bin/journalctl ^-f -u minecraft-server@([a-zA-Z_0-9\.\-]+).service,ドル /usr/bin/journalctl ^-f -n 0 -u minecraft-server@([a-zA-Z_0-9\.\-]+).service$
into this file.
In this sudoers file, regular expressions are used. This is only possible from sudo version 1.9.10 onwards. If your distribution has not received a sudo version ≥ 1.9.10 yet, you can use the legacy method.
Now you can run the server by using
$ sudo -u minecraft-telegram /var/minecraft/telegram-bot/minecraft-server-telegram-bot
You can also install a systemd-service for the bot by copying systemd-service/minecraft-telegram-bot.service to
/etc/systemd/system and executing
# systemctl daemon-reload
Legacy sudoers file
# Allow user minecraft-telegram to start and stop systemd service for the minecraft server
minecraft ALL = NOPASSWD: /usr/bin/systemctl start minecraft-server@<name>.service, /usr/bin/systemctl is-active minecraft-server@<name>.service, /usr/bin/systemctl stop minecraft-server@<name>.service, /usr/bin/journalctl -f -u minecraft-server@<name>.service, /usr/bin/journalctl -f -n 0 -u minecraft-server@<name>.service
In this file, <name> should be replaced by the name of your server, the same that you put into the bot-config.json.
You will have to add such a line for every server which you want to control using the Telegram bot.
Known issues
- The project currently lacks proper documentation.
Contribution
I am happy about any contribution you want to make to this project! If you want to do any major contribution, please open an issue before submitting a pull request, so we can coordinate it (so no work is done twice).
Licencing
The software is licenced under the terms of the GNU General Public License, Version 3 or later.
Copyright (C) 2022-2023 Joshua Noeske
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/.