Utoljára aktív 1 month ago

Telegram <=> Slack Bot

Revízió 84e47c778e5c40ddfed58d1305e08a432747a138

huebot.js Eredeti
1#!/usr/bin/env node
2
3// npm install slackbots node-telegram-bot-api
4
5var TelegramBot = require('node-telegram-bot-api');
6var SlackBot = require('slackbots');
7
8// create a bot
9var sBot = new SlackBot({
10 token: 'your-slack-bot-token', // Add a bot https://my.slack.com/services/new/bot and put the token
11 name: 'Telegram Bot'
12});
13
14var slackChannelName = 'your_slack_channel';
15
16var token = 'your-telegram-bot-token'; // Generate one with BotFather
17// Setup polling way
18var tBot = new TelegramBot(token, {polling: true});
19var tChatId = -123456; // Your telegram group ID
20var slackBotRunning = false;
21// Any kind of message
22tBot.on('message', function (msg) {
23 var chatId = msg.chat.id;
24 if (slackBotRunning && msg.chat.id === tChatId) {
25 tBot.getUserProfilePhotos(msg.from.id).then(function(data) {
26 if (data.total_count > 0) {
27 var f = data.photos[0][0].file_id;
28 tBot.getFileLink(f).then(function(fileURI) {
29 sendSlackMessage(msg.from.first_name + " " + msg.from.last_name, msg.text, fileURI);
30 });
31 } else {
32 sendSlackMessage(msg.from.first_name + " " + msg.from.last_name, msg.text);
33 }
34 });
35 }
36});
37
38function sendSlackMessage(name, message, image) {
39 console.log("(Slack) "+name+": "+message);
40 var params = {};
41 sBot.name = name + " (Telegram)";
42 if (image !== undefined)
43 params.icon_url = image;
44 else
45 params.icon_url = 'https://f2rank.noservidor.com.br/img/logo.png';
46 sBot.postMessageToGroup('huebr_international', message, params);
47}
48
49sBot.on('start', function() {
50 sBot.on('open', function() {
51 console.log("SlackBot running");
52 slackBotRunning = true;
53 var params = {
54 icon_url: 'https://f2rank.noservidor.com.br/img/logo.png'
55 };
56
57 sendSlackMessage("Telegram Bot", "I'm online! YEY!");
58 tBot.sendMessage(tChatId, "I'm online. YEY!");
59
60 var users = {};
61
62 sBot.getUsers().then(function(userlist) {
63 console.log("Loaded "+userlist.members.length+" users.");
64 for (var userC in userlist.members) {
65 var user = userlist.members[userC];
66 users[user.id] = user;
67 }
68 });
69
70 sBot.on('message', function(data) {
71 if (data.type === "message" && data.subtype !== 'bot_message' && data.subtype !== 'file_share') {
72 var username = "Unknown";
73 if (data.user in users)
74 username = users[data.user].real_name + "[" + users[data.user].name + "]";
75 console.log("(Telegram) ["+data.subtype+"] "+username+": "+data.text);
76 tBot.sendMessage(tChatId, username+": "+data.text);
77 } else if (data.type === "file_shared") {
78 var fileUrl = data.file.url;
79 var fileName = data.file.title;
80 var filemime = data.file.mimetype;
81 var comment = data.file.initial_comment.comment;
82 var username = "Unknown";
83 if (data.file.initial_comment.user in users)
84 username = users[data.file.initial_comment.user].real_name + "[" + users[data.file.initial_comment.user].name + "]";
85
86 console.log("(Telegram) "+username+" sent a file ("+fileName+"): "+comment);
87 if (filemime.indexOf("image") > -1) {
88 tBot.sendPhoto(tChatId, rq.get(fileUrl), {
89 caption: username + ": "+comment
90 });
91 } else {
92 console.log("Unhandled mimetype: "+filemime);
93 }
94 } else {
95 console.log("Generic Message: ",data);
96 }
97 });
98 });
99});