Utoljára aktív 1 month ago

Telegram <=> Slack Bot

racerxdl's Avatar Lucas Teske gist felülvizsgálása 10 years ago. Revízióhoz ugrás

1 file changed, 22 insertions, 6 deletions

huebot.js

@@ -18,7 +18,6 @@ var token = 'your-telegram-bot-token'; // Generate one with BotFather
18 18 var tBot = new TelegramBot(token, {polling: true});
19 19 var tChatId = -123456; // Your telegram group ID
20 20 var slackBotRunning = false;
21 -
22 21 // Any kind of message
23 22 tBot.on('message', function (msg) {
24 23 var chatId = msg.chat.id;
@@ -43,8 +42,8 @@ function sendSlackMessage(name, message, image) {
43 42 if (image !== undefined)
44 43 params.icon_url = image;
45 44 else
46 - params.icon_emoji = ':cat:';
47 - sBot.postMessageToGroup(slackChannelName, message, params);
45 + params.icon_url = 'https://f2rank.noservidor.com.br/img/logo.png';
46 + sBot.postMessageToGroup('huebr_international', message, params);
48 47 }
49 48
50 49 sBot.on('start', function() {
@@ -52,7 +51,7 @@ sBot.on('start', function() {
52 51 console.log("SlackBot running");
53 52 slackBotRunning = true;
54 53 var params = {
55 - icon_emoji: ':cat:'
54 + icon_url: 'https://f2rank.noservidor.com.br/img/logo.png'
56 55 };
57 56
58 57 sendSlackMessage("Telegram Bot", "I'm online! YEY!");
@@ -69,12 +68,29 @@ sBot.on('start', function() {
69 68 });
70 69
71 70 sBot.on('message', function(data) {
72 - if (data.type === "message" && data.subtype !== 'bot_message') {
71 + if (data.type === "message" && data.subtype !== 'bot_message' && data.subtype !== 'file_share') {
73 72 var username = "Unknown";
74 73 if (data.user in users)
75 74 username = users[data.user].real_name + "[" + users[data.user].name + "]";
76 - console.log("(Telegram) "+username+": "+data.text);
75 + console.log("(Telegram) ["+data.subtype+"] "+username+": "+data.text);
77 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 + }
78 94 } else {
79 95 console.log("Generic Message: ",data);
80 96 }

racerxdl's Avatar Lucas Teske gist felülvizsgálása 10 years ago. Revízióhoz ugrás

1 file changed, 83 insertions

huebot.js(fájl létrehozva)

@@ -0,0 +1,83 @@
1 + #!/usr/bin/env node
2 +
3 + // npm install slackbots node-telegram-bot-api
4 +
5 + var TelegramBot = require('node-telegram-bot-api');
6 + var SlackBot = require('slackbots');
7 +
8 + // create a bot
9 + var 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 +
14 + var slackChannelName = 'your_slack_channel';
15 +
16 + var token = 'your-telegram-bot-token'; // Generate one with BotFather
17 + // Setup polling way
18 + var tBot = new TelegramBot(token, {polling: true});
19 + var tChatId = -123456; // Your telegram group ID
20 + var slackBotRunning = false;
21 +
22 + // Any kind of message
23 + tBot.on('message', function (msg) {
24 + var chatId = msg.chat.id;
25 + if (slackBotRunning && msg.chat.id === tChatId) {
26 + tBot.getUserProfilePhotos(msg.from.id).then(function(data) {
27 + if (data.total_count > 0) {
28 + var f = data.photos[0][0].file_id;
29 + tBot.getFileLink(f).then(function(fileURI) {
30 + sendSlackMessage(msg.from.first_name + " " + msg.from.last_name, msg.text, fileURI);
31 + });
32 + } else {
33 + sendSlackMessage(msg.from.first_name + " " + msg.from.last_name, msg.text);
34 + }
35 + });
36 + }
37 + });
38 +
39 + function sendSlackMessage(name, message, image) {
40 + console.log("(Slack) "+name+": "+message);
41 + var params = {};
42 + sBot.name = name + " (Telegram)";
43 + if (image !== undefined)
44 + params.icon_url = image;
45 + else
46 + params.icon_emoji = ':cat:';
47 + sBot.postMessageToGroup(slackChannelName, message, params);
48 + }
49 +
50 + sBot.on('start', function() {
51 + sBot.on('open', function() {
52 + console.log("SlackBot running");
53 + slackBotRunning = true;
54 + var params = {
55 + icon_emoji: ':cat:'
56 + };
57 +
58 + sendSlackMessage("Telegram Bot", "I'm online! YEY!");
59 + tBot.sendMessage(tChatId, "I'm online. YEY!");
60 +
61 + var users = {};
62 +
63 + sBot.getUsers().then(function(userlist) {
64 + console.log("Loaded "+userlist.members.length+" users.");
65 + for (var userC in userlist.members) {
66 + var user = userlist.members[userC];
67 + users[user.id] = user;
68 + }
69 + });
70 +
71 + sBot.on('message', function(data) {
72 + if (data.type === "message" && data.subtype !== 'bot_message') {
73 + var username = "Unknown";
74 + if (data.user in users)
75 + username = users[data.user].real_name + "[" + users[data.user].name + "]";
76 + console.log("(Telegram) "+username+": "+data.text);
77 + tBot.sendMessage(tChatId, username+": "+data.text);
78 + } else {
79 + console.log("Generic Message: ",data);
80 + }
81 + });
82 + });
83 + });
Újabb Régebbi