-
Notifications
You must be signed in to change notification settings - Fork 45
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,7 +77,6 @@ class TeleIrc { | |
this.defaultIrcSuffix = ""; | ||
this.defaultShowJoinMessage = false; | ||
this.defaultShowLeaveMessage = false; | ||
|
||
} | ||
|
||
// ---------------- Functions ---------------- | ||
|
@@ -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 | ||
); | ||
} | ||
}); | ||
|
||
|
@@ -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." | ||
); | ||
}); | ||
} | ||
|
||
|
@@ -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 + "." | ||
); | ||
}); | ||
} | ||
|
||
|
@@ -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 + "." | ||
); | ||
}); | ||
} | ||
} | ||
|
@@ -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 | ||
|
@@ -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; | ||
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 | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why change
sg
=>msg
andfilemsg
=>msg
?There was a problem hiding this comment.
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.