Skip to content

Commit

Permalink
fixed #275
Browse files Browse the repository at this point in the history
  • Loading branch information
windka committed Oct 14, 2022
1 parent a4d6aec commit 71e61fe
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Changelog
All notable changes to this project will be documented in this file.

# [14.4.0] - 2022-10-14
### fileName can be specified when downloading file - [#275](https://github.com/windkh/node-red-contrib-telegrambot/issues/275)

# [14.3.0] - 2022-09-20
### improved deuplicate token usage detection - [#272](https://github.com/windkh/node-red-contrib-telegrambot/issues/272)

Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1502,14 +1502,15 @@ See the example flow [**copy message**](examples/copymessage.json) in the exampl
Remark: You need to have sufficient permissions to be able to do this message forwarding.


## Dwonloading files manually
## Downloading files manually
The receiver node can automatically download files into the configured download directory.
You can also download the files manually using a fileId by passing the following message to the sender node:

```javascript
msg.payload.download = {
fileId : "<yourfileidhere>",
filePath : "c:\\downloaddirectory"
filePath : "c:\\downloaddirectory",
fileName : "foo.jpg" // this is optional
};
return msg;
```
Expand Down
2 changes: 1 addition & 1 deletion examples/downloadfile.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"id":"75c35f9c5a3311e2","type":"telegram receiver","z":"ec1cf3fd9654e7e5","name":"","bot":"3b6bfbc0.423a04","saveDataDir":"","filterCommands":false,"x":210,"y":140,"wires":[["33b83c10.13de64"],[]]},{"id":"1bdded1.fbdcf13","type":"telegram sender","z":"ec1cf3fd9654e7e5","name":"","bot":"3b6bfbc0.423a04","haserroroutput":false,"outputs":1,"x":730,"y":140,"wires":[["886ef434a337ce60"]]},{"id":"33b83c10.13de64","type":"function","z":"ec1cf3fd9654e7e5","name":"download first photo manually","func":"if(msg.payload.type === 'photo'){\n \n // manually download the first photo using the fileId.\n msg.payload.download = {\n fileId : msg.payload.photos[0].file_id,\n filePath : \"c:\\\\temp\"\n }\n return msg;\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":470,"y":140,"wires":[["1bdded1.fbdcf13"]]},{"id":"886ef434a337ce60","type":"debug","z":"ec1cf3fd9654e7e5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":950,"y":140,"wires":[]},{"id":"3b6bfbc0.423a04","type":"telegram bot","botname":"HeinzBot","usernames":"","chatids":"","baseapiurl":"","updatemode":"polling","pollinterval":"1000","usesocks":true,"sockshost":"","socksport":"","socksusername":"","sockspassword":"","bothost":"","botpath":"","localbotport":"8443","publicbotport":"8443","privatekey":"","certificate":"","useselfsignedcertificate":false,"sslterminated":false,"verboselogging":true}]
[{"id":"5b61e831f6a6d74d","type":"telegram receiver","z":"ec1cf3fd9654e7e5","name":"","bot":"65ca12172854cc2d","saveDataDir":"","filterCommands":false,"x":110,"y":120,"wires":[["7781831794d26351"],[]]},{"id":"e701c4fe8f5db67f","type":"telegram sender","z":"ec1cf3fd9654e7e5","name":"","bot":"65ca12172854cc2d","haserroroutput":false,"outputs":1,"x":630,"y":120,"wires":[["f7fd168fb734c5cb"]]},{"id":"7781831794d26351","type":"function","z":"ec1cf3fd9654e7e5","name":"download first photo manually","func":"if(msg.payload.type === 'photo'){\n \n // manually download the first photo using the fileId.\n msg.payload.download = {\n fileId : msg.payload.photos[0].file_id,\n filePath : \"c:\\\\temp\",\n // fileName : \"foo.jpg\" // optional\n }\n return msg;\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":370,"y":120,"wires":[["e701c4fe8f5db67f"]]},{"id":"f7fd168fb734c5cb","type":"debug","z":"ec1cf3fd9654e7e5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":850,"y":120,"wires":[]},{"id":"65ca12172854cc2d","type":"telegram bot","botname":"HeinzBot","usernames":"","chatids":"","baseapiurl":"","updatemode":"polling","pollinterval":"3000","usesocks":false,"sockshost":"192.168.178.200","socksprotocol":"socks5","socksport":"1080","socksusername":"","sockspassword":"","bothost":"","botpath":"","localbotport":"8443","publicbotport":"8443","privatekey":"","certificate":"","useselfsignedcertificate":false,"sslterminated":false,"verboselogging":true}]
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": "14.3.0",
"version": "14.4.0",
"description": "Telegram bot nodes for Node-RED",
"dependencies": {
"bluebird": "^3.7.2",
Expand Down
39 changes: 37 additions & 2 deletions telegrambot/99-telegrambot.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ process.env['NTBA_FIX_319'] = 1;
module.exports = function (RED) {
'use strict';

const path = require('path');
const pump = require('pump');
const fs = require('fs');
let Promise = require('bluebird');
Promise.config({
Expand Down Expand Up @@ -1895,9 +1897,9 @@ module.exports = function (RED) {
} else if (msg.payload.download) {
let fileId = msg.payload.download.fileId;
let filePath = msg.payload.download.filePath;
let fileName = msg.payload.download.fileName;

node.telegramBot
.downloadFile(fileId, filePath)
node.downloadFile(fileId, filePath, fileName)
.catch(function (ex) {
node.processError(ex, msg, nodeSend, nodeDone);
})
Expand Down Expand Up @@ -2438,6 +2440,39 @@ module.exports = function (RED) {
} // forward
};

// Derived from original code but with optional fileName
this.downloadFile = function(fileId, downloadDir, fileName) {
let resolve;
let reject;
const promise = new Promise((a, b) => {
resolve = a;
reject = b;
});

let form = {};
const fileStream = node.telegramBot.getFileStream(fileId, form);
fileStream.on('info', (info) => {
if(fileName === undefined) {
fileName = info.uri.slice(info.uri.lastIndexOf('/') + 1);
}

const filePath = path.join(downloadDir, fileName);
pump(fileStream, fs.createWriteStream(filePath), (error) => {
if (!error) {
return resolve(filePath);
}
else {
return reject(error);
}
});
});
fileStream.on('error', (err) => {
reject(err);
});
return promise;
};


// TODO: https://github.com/windkh/node-red-contrib-telegrambot/issues/178
// TODO: https://github.com/yagop/node-telegram-bot-api/issues/876
this.editMessageMedia = function (media, form = {}) {
Expand Down

0 comments on commit 71e61fe

Please sign in to comment.