Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uploaded Photos appear in IRC #38

Merged
merged 3 commits into from
Oct 3, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 110 additions & 13 deletions lib/libteleirc.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ class TeleIrc {
this.defaultIrcSuffix = "";
this.defaultShowJoinMessage = false;
this.defaultShowLeaveMessage = false;

}

// ---------------- Functions ----------------
Expand Down Expand Up @@ -244,7 +243,10 @@ class TeleIrc {
});

if (matchedNames.length <= 0) {
this.queueTelegramMessage(this.config.tg.chatId, "<" + from + "> " + message);
this.queueTelegramMessage(
this.config.tg.chatId,
"<" + from + "> " + message
);
}
});

Expand All @@ -260,15 +262,21 @@ class TeleIrc {
});

if (matchedNames.length <= 0) {
this.queueTelegramMessage(this.config.tg.chatId, from + " " + message);
this.queueTelegramMessage(
this.config.tg.chatId,
from + " " + message
);
}
});
}

if (this.config.tg.showJoinMessage) {
// Let the telegram chat know when a user joins the IRC channel
this.ircbot.addListener('join', (channel, username) => {
this.queueTelegramMessage(this.config.tg.chatId, username + " has joined " + channel + " channel.");
this.queueTelegramMessage(
this.config.tg.chatId,
username + " has joined " + channel + " channel."
);
});
}

Expand All @@ -278,7 +286,10 @@ class TeleIrc {
if (typeof reason != "string") {
reason = "Parting...";
}
this.queueTelegramMessage(this.config.tg.chatId, username + " has left " + channel + ": " + reason + ".");
this.queueTelegramMessage(
this.config.tg.chatId,
username + " has left " + channel + ": " + reason + "."
);
});
}

Expand All @@ -288,7 +299,10 @@ class TeleIrc {
if (typeof reason != "string") {
reason = "Kicked";
}
this.queueTelegramMessage(this.config.tg.chatId, username + " was kicked by " + by + " from " + channel + ": " + reason + ".");
this.queueTelegramMessage(
this.config.tg.chatId,
username + " was kicked by " + by + " from " + channel + ": " + reason + "."
);
});
}
}
Expand Down Expand Up @@ -350,12 +364,27 @@ class TeleIrc {
// Check if this message is a new user joining the group chat:
let username = msg.new_chat_member.username;
let first_name = msg.new_chat_member.first_name;
this.ircbot.say(this.config.irc.channel, this.getTelegramToIrcJoinLeaveMsg(first_name, username, "has joined the Telegram Group!"));

this.ircbot.say(
this.config.irc.channel,
this.getTelegramToIrcJoinLeaveMsg(
first_name,
username,
"has joined the Telegram Group!"
)
);
} else if (msg.left_chat_member && teleirc.config.irc.showLeaveMessage) {
// Check if this message is a user leaving the telegram group chat:
let username = msg.left_chat_member.username;
let first_name = msg.left_chat_member.first_name;
this.ircbot.say(this.config.irc.channel, this.getTelegramToIrcJoinLeaveMsg(first_name, username, "has left the Telegram Group."));

this.ircbot.say(this.config.irc.channel,
this.getTelegramToIrcJoinLeaveMsg(
first_name,
username,
"has left the Telegram Group."
)
);
} else if (msg.sticker !== undefined) {
// If we have a sticker, we should send it to IRC... sort of...
// The best way to get a sticker to IRC would be to send a URL to the sticker, but that doesn't
Expand All @@ -367,22 +396,34 @@ class TeleIrc {
// That is, if the config allows us to do that of course.
let emoji = msg.sticker.emoji;
if (emoji !== undefined && this.config.irc.sendStickerEmoji) {
this.ircbot.say(this.config.irc.channel, this.config.irc.prefix + from + this.config.irc.suffix + " " + emoji);
this.sendUserMessageToIrc(from, emoji);
}
} else {
// Check if this message contains a document/file to display in irc
if (msg.document) {
this.tgbot.getFileLink(msg.document.file_id).then((url) => {
let newmessage = sg.document.file_name + " (" + msg.document.mime_type + ", " + filemsg.document.file_size_size + " bytes) : " + url;
this.ircbot.say(this.config.irc.channel, this.config.irc.prefix + from + this.config.irc.suffix + " " + newmessage);
let newmessage = msg.document.file_name + " (" + msg.document.mime_type + ", " + msg.document.file_size_size + " bytes) : " + url;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change sg => msg and filemsg => msg?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sg I think was a typo... it should have been msg all along. If we tried uploading a document, teleirc crashed. I have no idea where filemsg came from.. my guess is that was also a typo.

this.sendUserMessageToIrc(from, newmessage);
});
} else {
} else if(msg.photo) {
let photo = this.pickPhoto(msg.photo);
if (photo !== null) {
this.tgbot.getFileLink(photo.file_id).then((url) => {
let theMessage = this.getPhotoMessage(msg.caption, from, url);
this.ircbot.say(
this.config.irc.channel,
theMessage
);
});
}
}
else{
console.log("Ignoring non-text message: " + JSON.stringify(msg));
}
}
} else {
// Relay all text messages into IRC
this.ircbot.say(this.config.irc.channel, this.config.irc.prefix + from + this.config.irc.suffix + " " + message);
this.sendUserMessageToIrc(from, message);
}
} else {
// Messages that are sent to the bot outside of the group chat should just be dumped
Expand Down Expand Up @@ -415,6 +456,49 @@ class TeleIrc {
return option;
}

/**
* Picks the best photo from the photo array
* to send to the IRC channel.
* @param {Array} photoInfo
* @returns The best photo, or null if there are none (e.g. empty array passed in).
*/
pickPhoto(photoInfo) {
var photo = null;

// When we upload a photo to telegram,
// Telegram has several versions of the photos
// of various sizes. We'll
// pick the biggest one (more resolution is always
// best!).
for(var i = 0; i < photoInfo.length; ++i) {
var innerPhoto = photoInfo[i];
if(photo === null) {
photo = innerPhoto;
}
else if (photo.file_size < innerPhoto.file_size) {
photo = innerPhoto;
}
}

return photo;
}

getPhotoMessage(caption, from, fileUrl) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 💯 🎉

var message;
if (
(caption !== null) &&
(caption !== undefined)
) {
message = "'" + caption + "'";
}
else{
message = "'Untitled Image'"
}
message += " uploaded by " + from + ": " + fileUrl;

return message;
}

/**
* Generates the "User has joined" or "User has left" message that goes from telegram
* to IRC in a way that does not cause undefines to appear from an undefined username.
Expand All @@ -433,6 +517,19 @@ class TeleIrc {
return firstName + " (@" + userName + ") " + suffix;
}
}

/**
* Sends the given Telegram message from a USER to the IRC channel.
* This includes the IRC prefix and the suffix.
* @param {String} from - Who the message was from on the Telegram Side.
* @param {String} message - The message to send to the IRC channel.
*/
sendUserMessageToIrc(from, message) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering where this method came from, didn't see it until I worked my way to the bottom. Nice idea, this is a little more clean. 👍

this.ircbot.say(
this.config.irc.channel,
this.config.irc.prefix + from + this.config.irc.suffix + " " + message
);
}
}

module.exports = TeleIrc, TeleIrcException;