Last active
July 16, 2021 13:28
-
-
Save racerxdl/03416cc237e962287590 to your computer and use it in GitHub Desktop.
Telegram <=> Slack Bot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
// npm install slackbots node-telegram-bot-api | |
var TelegramBot = require('node-telegram-bot-api'); | |
var SlackBot = require('slackbots'); | |
// create a bot | |
var sBot = new SlackBot({ | |
token: 'your-slack-bot-token', // Add a bot https://my.slack.com/services/new/bot and put the token | |
name: 'Telegram Bot' | |
}); | |
var slackChannelName = 'your_slack_channel'; | |
var token = 'your-telegram-bot-token'; // Generate one with BotFather | |
// Setup polling way | |
var tBot = new TelegramBot(token, {polling: true}); | |
var tChatId = -123456; // Your telegram group ID | |
var slackBotRunning = false; | |
// Any kind of message | |
tBot.on('message', function (msg) { | |
var chatId = msg.chat.id; | |
if (slackBotRunning && msg.chat.id === tChatId) { | |
tBot.getUserProfilePhotos(msg.from.id).then(function(data) { | |
if (data.total_count > 0) { | |
var f = data.photos[0][0].file_id; | |
tBot.getFileLink(f).then(function(fileURI) { | |
sendSlackMessage(msg.from.first_name + " " + msg.from.last_name, msg.text, fileURI); | |
}); | |
} else { | |
sendSlackMessage(msg.from.first_name + " " + msg.from.last_name, msg.text); | |
} | |
}); | |
} | |
}); | |
function sendSlackMessage(name, message, image) { | |
console.log("(Slack) "+name+": "+message); | |
var params = {}; | |
sBot.name = name + " (Telegram)"; | |
if (image !== undefined) | |
params.icon_url = image; | |
else | |
params.icon_url = 'https://f2rank.noservidor.com.br/img/logo.png'; | |
sBot.postMessageToGroup('huebr_international', message, params); | |
} | |
sBot.on('start', function() { | |
sBot.on('open', function() { | |
console.log("SlackBot running"); | |
slackBotRunning = true; | |
var params = { | |
icon_url: 'https://f2rank.noservidor.com.br/img/logo.png' | |
}; | |
sendSlackMessage("Telegram Bot", "I'm online! YEY!"); | |
tBot.sendMessage(tChatId, "I'm online. YEY!"); | |
var users = {}; | |
sBot.getUsers().then(function(userlist) { | |
console.log("Loaded "+userlist.members.length+" users."); | |
for (var userC in userlist.members) { | |
var user = userlist.members[userC]; | |
users[user.id] = user; | |
} | |
}); | |
sBot.on('message', function(data) { | |
if (data.type === "message" && data.subtype !== 'bot_message' && data.subtype !== 'file_share') { | |
var username = "Unknown"; | |
if (data.user in users) | |
username = users[data.user].real_name + "[" + users[data.user].name + "]"; | |
console.log("(Telegram) ["+data.subtype+"] "+username+": "+data.text); | |
tBot.sendMessage(tChatId, username+": "+data.text); | |
} else if (data.type === "file_shared") { | |
var fileUrl = data.file.url; | |
var fileName = data.file.title; | |
var filemime = data.file.mimetype; | |
var comment = data.file.initial_comment.comment; | |
var username = "Unknown"; | |
if (data.file.initial_comment.user in users) | |
username = users[data.file.initial_comment.user].real_name + "[" + users[data.file.initial_comment.user].name + "]"; | |
console.log("(Telegram) "+username+" sent a file ("+fileName+"): "+comment); | |
if (filemime.indexOf("image") > -1) { | |
tBot.sendPhoto(tChatId, rq.get(fileUrl), { | |
caption: username + ": "+comment | |
}); | |
} else { | |
console.log("Unhandled mimetype: "+filemime); | |
} | |
} else { | |
console.log("Generic Message: ",data); | |
} | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to use this script ?