Skip to content

Commit

Permalink
Add implementation of telegram bot (#2889)
Browse files Browse the repository at this point in the history
* Add implementation of telegram bot

* Fix linting

* More linting issues

* Fixed linting
  • Loading branch information
hous3m4ster authored Jan 14, 2023
1 parent d53f824 commit b76decc
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions examples/telegram.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* This example is a very simple way of connecting to a telegram group
* For this example you'll need Telegraf installed. This can be done with `npm install telegraf`
* You need to do this before running this example:
* - You need to get a telegram bot token from @botfather
* - You need to get the id of the group you want to use
*/

if (process.argv.length < 6 || process.argv.length > 8) {
console.log('Usage : node telegram.js <telegram bot token> <group id> <host> <port> [<name>] [<password>]')
process.exit(1)
}

// Load Telegraf
const { Telegraf } = require('telegraf')
const telegram = new Telegraf(process.argv[2])

// Load mineflayer
const mineflayer = require('mineflayer')
const bot = mineflayer.createBot({
host: process.argv[4],
port: parseInt(process.argv[5]),
username: process.argv[6] || 'telegram',
password: process.argv[7]
})

telegram.on('text', async (ctx) => {
// check if message was reveived from chosen group
if (ctx.update.message.chat.id.toString() === process.argv[3]) {
// send message to mc server
bot.chat(`${ctx.update.message.from.first_name} ${ctx.update.message.from.last_name}: ${ctx.update.message.text}`)
}
})

// Redirect in-game messages to telegram group
bot.on('chat', (username, message) => {
// Ignore messages from the bot itself
if (username === bot.username) return
telegram.telegram.sendMessage(process.argv[3], username + ': ' + message)
})

// Login telegram bot
telegram.launch()

0 comments on commit b76decc

Please sign in to comment.