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

Added feature sendMediaGroup #68

Merged
merged 6 commits into from
Apr 2, 2019
Merged
Show file tree
Hide file tree
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
45 changes: 43 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ MatthiasHunziker for extending the callback query node to support generic events

Skiepp for providing the send chat action idea.

MK-2001 for providing the sendMediaGroup function.


# Dependencies
The nodes are a simple wrapper around the [node-telegram-bot-api](https://github.com/yagop/node-telegram-bot-api)
It also depends on [socks5-https-client](https://github.com/mattcg/socks5-https-client)
Expand Down Expand Up @@ -64,7 +67,12 @@ This version contains SOCKS5 support: see https://github.com/windkh/node-red-con
Version 5.3.0

Improved configuration node: grouped properties. Added support for custom and non custom certificates in webhook mode: see https://github.com/windkh/node-red-contrib-telegrambot/issues/66



Version 5.4.0

Added sendMediaGroup function: see https://github.com/windkh/node-red-contrib-telegrambot/pull/68


# Warning
The nodes are tested with nodejs 8.11.1 and node-red 0.18.7. This version is not tested with node-red 0.19.x and above.
Expand Down Expand Up @@ -140,6 +148,7 @@ The following types can be received (see type in output object):
- location - content is an object with latitude and longitude
- venue - content is the venue object
- contact - content is the contact information object
Note that media groups are received not as group, but as separate messages of type photo and video.

The following types indicate changes in the group or channel itself.
- new_chat_title - content is the new chat title
Expand Down Expand Up @@ -363,11 +372,43 @@ The following types require a special content format to be used. See the underly
- location
- contact
- venue

- mediaGroup

![Alt text](images/TelegramBotSendPhoto.png?raw=true "Sending a photo")
![Alt text](images/TelegramBotSendPhoto2.png?raw=true "Setting the correct content type.")

### Sending a mediaGroup as album

To send several photos as an album you can use the mediaGroup. For the type of media group you have to set the content to an array of object type [InputMediaPhoto](https://core.telegram.org/bots/api#inputmediaphoto).
Please review the Json below.

```javascript
msg.payload = {
"chatId": 123456789,
"messageId": 1,
"type": "mediaGroup",
"content": [
{
"type": "photo",
"media": "/pic/frame_1.jpg"
},
{
"type": "photo",
"media": "/pic/frame_2.jpg"
},
{
"type": "photo",
"media": "/pic/frame_3.jpg"
},
{
"type": "photo",
"media": "/pic/frame_4.jpg"
}
]
}
```
[sendmediagroup flow](examples/sendmediagroup.json)


## Sending contact
Sending a contact is limited to the fields supported by the underlying API to "phone_number" and "first_name".
Expand Down
1 change: 1 addition & 0 deletions examples/sendMediaGroup.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"id":"409e1419.30961c","type":"telegram receiver","z":"3068ac13.951064","name":"","bot":"6cc03fc7.9b8fb","saveDataDir":"c:\\Temp","x":119,"y":132,"wires":[["fdc09042.ec252"],[]]},{"id":"43b875cb.e6353c","type":"telegram sender","z":"3068ac13.951064","name":"","bot":"6cc03fc7.9b8fb","x":470,"y":43,"wires":[[]]},{"id":"f4383c1b.82d6f","type":"telegram command","z":"3068ac13.951064","name":"","command":"/send","bot":"6cc03fc7.9b8fb","strict":false,"x":79.5,"y":50,"wires":[["a93a639a.b8a88"],[]]},{"id":"fdc09042.ec252","type":"debug","z":"3068ac13.951064","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":322,"y":126,"wires":[]},{"id":"a93a639a.b8a88","type":"function","z":"3068ac13.951064","name":"create media group","func":"// sendMediaGroup example: send between 2 and 10 media.\n// Note that type can also be video.\n// and the caption property is optional.\n// see https://core.telegram.org/bots/api#inputmediaphoto\n// see https://core.telegram.org/bots/api#inputmediavideo\n\nmsg.payload.type = \"mediaGroup\";\nmsg.payload.content = [\n {\n type : \"photo\",\n media : \"c:\\\\Temp\\\\1.jpg\",\n caption : \"Photo 1\"\n },\n {\n type : \"photo\",\n media : \"c:\\\\Temp\\\\2.jpg\",\n caption : \"Photo 2\"\n }\n];\n\nreturn msg;","outputs":1,"noerr":0,"x":254,"y":43,"wires":[["43b875cb.e6353c"]]},{"id":"6cc03fc7.9b8fb","type":"telegram bot","z":"","botname":"HeinzBot","usernames":"Windhose","chatids":"","baseapiurl":"","updatemode":"polling","pollinterval":"300","usesocks":false,"sockshost":"","socksport":"6667","socksusername":"anonymous","sockspassword":"","bothost":"","localbotport":"8443","publicbotport":"8443","privatekey":"","certificate":"","useselfsignedcertificate":false,"verboselogging":false}]
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-red-contrib-telegrambot",
"version": "5.3.0",
"version": "5.4.0",
"description": "Telegram bot nodes.",
"dependencies": {
"node-telegram-bot-api" : "^0.30.0",
Expand Down
70 changes: 53 additions & 17 deletions telegrambot/99-telegrambot.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,9 @@ module.exports = function (RED) {
self.users.push(id);
}
else {
self.warn("Node " + id + " registered twice at the configuration node: ignoring.");
if(self.verbose) {
self.warn("Node " + id + " registered twice at the configuration node: ignoring.");
}
}
}

Expand Down Expand Up @@ -380,21 +382,23 @@ module.exports = function (RED) {
// creates the message details object from the original message
function getMessageDetails(botMsg) {

// Note that photos and videos can be sent as media group. The bot will receive the contents as single messages.
// Therefore the photo and video messages can contain a mediaGroupId if so....
var messageDetails;
if (botMsg.text) {
messageDetails = { chatId: botMsg.chat.id, messageId: botMsg.message_id, type: 'message', content: botMsg.text };
} else if (botMsg.photo) {
// photos are sent using several resolutions. Therefore photo is an array. We choose the one with the highest resolution in the array.
var index = getPhotoIndexWithHighestResolution(botMsg.photo);
messageDetails = { chatId: botMsg.chat.id, messageId: botMsg.message_id, type: 'photo', content: botMsg.photo[index].file_id, caption: botMsg.caption, date: botMsg.date, blob: true, photos: botMsg.photo };
messageDetails = { chatId: botMsg.chat.id, messageId: botMsg.message_id, type: 'photo', content: botMsg.photo[index].file_id, caption: botMsg.caption, date: botMsg.date, blob: true, photos: botMsg.photo, mediaGroupId: botMsg.media_group_id };
} else if (botMsg.audio) {
messageDetails = { chatId: botMsg.chat.id, messageId: botMsg.message_id, type: 'audio', content: botMsg.audio.file_id, caption: botMsg.caption, date: botMsg.date, blob: true };
} else if (botMsg.document) {
messageDetails = { chatId: botMsg.chat.id, messageId: botMsg.message_id, type: 'document', content: botMsg.document.file_id, caption: botMsg.caption, date: botMsg.date, blob: true };
} else if (botMsg.sticker) {
messageDetails = { chatId: botMsg.chat.id, messageId: botMsg.message_id, type: 'sticker', content: botMsg.sticker.file_id, date: botMsg.date, blob: true };
} else if (botMsg.video) {
messageDetails = { chatId: botMsg.chat.id, messageId: botMsg.message_id, type: 'video', content: botMsg.video.file_id, caption: botMsg.caption, date: botMsg.date, blob: true };
messageDetails = { chatId: botMsg.chat.id, messageId: botMsg.message_id, type: 'video', content: botMsg.video.file_id, caption: botMsg.caption, date: botMsg.date, blob: true, mediaGroupId: botMsg.media_group_id };
} else if (botMsg.video_note) {
messageDetails = { chatId: botMsg.chat.id, messageId: botMsg.message_id, type: 'video_note', content: botMsg.video_note.file_id, caption: botMsg.caption, date: botMsg.date, blob: true };
} else if (botMsg.voice) {
Expand Down Expand Up @@ -489,12 +493,13 @@ module.exports = function (RED) {
if (node.config.isAuthorized(chatid, username)) {
// downloadable "blob" message?
if (messageDetails.blob) {
node.telegramBot.getFileLink(messageDetails.content).then(function (weblink) {
var fileId = msg.payload.content;
node.telegramBot.getFileLink(fileId).then(function (weblink) {
msg.payload.weblink = weblink;

// download and provide with path
if (config.saveDataDir) {
node.telegramBot.downloadFile(messageDetails.content, config.saveDataDir).then(function (path) {
node.telegramBot.downloadFile(fileId, config.saveDataDir).then(function (path) {
msg.payload.path = path;
node.send([msg, null]);
});
Expand Down Expand Up @@ -855,16 +860,17 @@ module.exports = function (RED) {
// type : string type of message to send
// content : message content
// The type is a string can be any of the following:
// message content is String
// photo content is String|stream.Stream|Buffer
// audio content is String|stream.Stream|Buffer
// document content is String|stream.Stream|Buffer
// sticker content is String|stream.Stream|Buffer
// video content is String|stream.Stream|Buffer
// voice content is String|stream.Stream|Buffer
// location content is an object that contains latitude and logitude
// contact content is full contact object
// action content is one of the following:
// message content is String
// photo content is String|stream.Stream|Buffer
// audio content is String|stream.Stream|Buffer
// document content is String|stream.Stream|Buffer
// sticker content is String|stream.Stream|Buffer
// video content is String|stream.Stream|Buffer
// voice content is String|stream.Stream|Buffer
// location content is an object that contains latitude and logitude
// contact content is full contact object
// mediaGroup content is array of mediaObject
// action content is one of the following:
// typing, upload_photo, record_video, upload_video, record_audio, upload_audio,
// upload_document, find_location, record_video_note, upload_video_note
function TelegramOutNode(config) {
Expand Down Expand Up @@ -969,7 +975,38 @@ module.exports = function (RED) {
});
}
break;

case 'mediaGroup':
if(this.hasContent(msg)) {
if(Array.isArray(msg.payload.content)) {
for (var i = 0; i < msg.payload.content.length; i++) {
var mediaItem = msg.payload.content[i];
if(typeof mediaItem.type !== "string") {
node.warn("msg.payload.content[" + i + "].type is not a string it is " + typeof mediaItem.type);
break;
}
if(mediaItem.media === undefined) {
node.warn("msg.payload.content[" + i + "].media is not defined");
break;
}
if(mediaItem.caption === undefined || typeof mediaItem.caption !== "string") {
node.warn("msg.payload.content[" + i + "].caption is not a string");
break;
}
if(mediaItem.parse_mode === undefined || typeof mediaItem.parse_mode !== "string") {
node.warn("msg.payload.content[" + i + "].parse_mode is not a string");
break;
}
}
node.telegramBot.sendMediaGroup(chatId, msg.payload.content, msg.payload.options).then(function (result) {
msg.payload.content = result;
msg.payload.sentMessageId = result.message_id;
node.send(msg);
});
} else {
node.warn("msg.payload.content for mediaGroup is not an array of mediaItem");
}
}
break;
case 'audio':
if (this.hasContent(msg)) {
node.telegramBot.sendAudio(chatId, msg.payload.content, msg.payload.options).then(function (result) {
Expand Down Expand Up @@ -1195,7 +1232,6 @@ module.exports = function (RED) {
// sendGame, setGameScore, getGameHighScores
// sendInvoice, answerShippingQuery, answerPreCheckoutQuery
// getStickerSet, uploadStickerFile, createNewStickerSet, addStickerToSet, setStickerPositionInSet, deleteStickerFromSet
// sendMediaGroup

default:
// unknown type nothing to send.
Expand Down