Skip to content

Commit 89bf314

Browse files
author
windka
committed
initial version with readme
1 parent 18fc5fb commit 89bf314

File tree

6 files changed

+171
-44
lines changed

6 files changed

+171
-44
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Changelog
22
All notable changes to this project will be documented in this file.
33

4+
## [0.1.0]
5+
### Added readme and examples, tested echo flow
6+
47
## [0.0.5]
58
### Added sender node and examples
69

README.md

Lines changed: 118 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,118 @@
1-
# node-red-node-telegrambot
2-
Telegram bot and userbot nodes for node-red.
1+
# Telegram client nodes for Node-RED
2+
[![Platform](https://img.shields.io/badge/platform-Node--RED-red)](https://nodered.org)
3+
![License](https://img.shields.io/github/license/windkh/node-red-node-telegrambot.svg)
4+
[![Downloads](https://img.shields.io/npm/dm/node-red-node-telegrambot.svg)](https://www.npmjs.com/package/node-red-node-telegrambot)
5+
[![Total Downloads](https://img.shields.io/npm/dt/node-red-node-telegrambot.svg)](https://www.npmjs.com/package/node-red-node-telegrambot)
6+
[![NPM](https://img.shields.io/npm/v/node-red-node-telegrambot?logo=npm)](https://www.npmjs.org/package/node-red-node-telegrambot)
7+
[![Known Vulnerabilities](https://snyk.io/test/npm/node-red-node-telegrambot/badge.svg)](https://snyk.io/test/npm/node-red-node-telegrambot)
8+
[![Telegram](https://img.shields.io/badge/Join-Telegram%20Chat-blue.svg?logo=telegram)](https://t.me/nodered_telegrambot)
9+
[![Package Quality](https://packagequality.com/shield/node-red-node-telegrambot.svg)](https://packagequality.com/#?package=node-red-node-telegrambot)
10+
![Build](https://img.shields.io/github/actions/workflow/status/windkh/node-red-node-telegrambot/node.js.yml)
11+
[![Open Issues](https://img.shields.io/github/issues-raw/windkh/node-red-node-telegrambot.svg)](https://github.com/windkh/node-red-node-telegrambot/issues)
12+
[![Closed Issues](https://img.shields.io/github/issues-closed-raw/windkh/node-red-node-telegrambot.svg)](https://github.com/windkh/node-red-node-telegrambot/issues?q=is%3Aissue+is%3Aclosed)
13+
...
14+
15+
16+
This package contains a node which act as a Telegram Client. It is based on gramjs which implements the mtproto mobild protocol. (see https://core.telegram.org/mtproto). Unlike node-red-contrib-telegrambot it does not support the telegram bot api. The package can be used to create so-called userbots or selfbots which to automate things under your own user-name. However you should be aware of the fact, that if you cause flooding and other havoc telegram will quickly ban your account either for 24h or even forever. It is recommended to use a test account while developing.
17+
18+
19+
# Thanks for your donation
20+
If you want to support this free project. Any help is welcome. You can donate by clicking one of the following links:
21+
<a target="blank" href="https://blockchain.com/btc/payment_request?address=1PBi7BoZ1mBLQx4ePbwh1MVoK2RaoiDsp5"><img src="https://img.shields.io/badge/Donate-Bitcoin-green.svg"/></a>
22+
<a target="blank" href="https://www.paypal.me/windkh"><img src="https://img.shields.io/badge/Donate-PayPal-blue.svg"/></a>
23+
24+
<a href="https://www.buymeacoffee.com/windka" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee" height="41" width="174"></a>
25+
26+
27+
# Credits
28+
-
29+
30+
31+
# Installation
32+
[![NPM](https://nodei.co/npm/node-red-node-telegrambot.png?downloads=true)](https://nodei.co/npm/node-red-node-telegrambot/)
33+
34+
You can install the nodes using node-red's "Manage palette" in the side bar.
35+
36+
Or run the following command in the root directory of your Node-RED installation
37+
38+
npm install node-red-node-telegrambot --save
39+
40+
Note that the minimum node-red version 1.3.7 and minimum nodejs version is 12.x.
41+
42+
43+
# Dependencies
44+
The nodes are tested with `Node.js v18.12.1` and `Node-RED v3.0.2`.
45+
- [gramjs home](https://gram.js.org/)
46+
- [gramjs github](https://github.com/gram-js/gramjs)
47+
48+
# Changelog
49+
Changes can be followed [here](/CHANGELOG.md).
50+
51+
52+
# Usage
53+
## Basics
54+
### Authentication
55+
The *Telegram client receiver* node receives messages from like a telegram client. You need to login with a phone-number and an API ID and API Hash in order to be able to receive message under your own user name.
56+
57+
You can create an API ID and Hash when you login to your telegram account here https://my.telegram.org/auth
58+
Then go to 'API Development Tools' and create your API ID and API Hash. Both are required when configuring your nodes. The nodes login only once to create a so-called session string. This string can be created from within
59+
the config node or as an alternative you can also create it online here https://tgsnake.js.org/login
60+
This session string is used instead of interactive login (where you need to enter a phone-code and your password if set).
61+
62+
### Receiver Node
63+
The *Telegram client receiver* node receives message which are sent to your account. Just add a debug node to the
64+
output and investigate the objects in `msg.payload`.
65+
66+
### Sender Node
67+
The *Telegram client sender* node is able to call nearly all functions provides by gramjs.
68+
For a full list of methods please visit https://gram.js.org/ under TL.
69+
70+
### Examples
71+
#### Api.messages.SendMessage
72+
To call the [SendMessage](https://gram.js.org/tl/messages/SendMessage) function, you must do the following:
73+
Create a function node and enter 'messages' for the api property and 'SendMessage' for the func property.
74+
The arguments described in the api must be added to args. SendMessage contains a field randomId which must be
75+
set by the user to a random number to prevent message looping in the telegram server. Peer must be set to the
76+
name of the user you want to send the message to.
77+
78+
```javascript
79+
let randomId = BigInt(Math.floor(Math.random() * 1e15));
80+
let username = msg.payload;
81+
msg.payload = {
82+
api: 'messages',
83+
func: 'SendMessage',
84+
args: {
85+
peer: "to username",
86+
message: "Test1",
87+
randomId: randomId,
88+
noWebpage: true,
89+
noforwards: true,
90+
scheduleDate: 0,
91+
// sendAs: "from username",
92+
}
93+
}
94+
return msg;
95+
```
96+
97+
[**send message flow**](examples/Api.messages.SendMessage.json)
98+
99+
100+
#### Api.account.CheckUsername
101+
To call the [CheckUsername](https://gram.js.org/tl/account/CheckUsername) function, you must do the following:
102+
Create a function node and enter 'account' for the api property and 'CheckUsername' for the func property.
103+
The arguments described in the api must be added to args. In this case it is only the username property.
104+
105+
```javascript
106+
let username = msg.payload;
107+
msg.payload = {
108+
api: "account",
109+
func: "CheckUsername",
110+
args: {
111+
username: 'usernameToCheckHere'
112+
}
113+
}
114+
return msg;
115+
```
116+
117+
[**check username flow**](examples/Api.account.CheckUsername.json)
118+
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[{"id":"7f66f74d4da2179d","type":"telegram client sender","z":"3d8bc5187eb93fc0","name":"","bot":"b4217d5b3ad6f705","x":660,"y":640,"wires":[["fce968085d0d85fb"]]},{"id":"fce968085d0d85fb","type":"debug","z":"3d8bc5187eb93fc0","name":"debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":830,"y":640,"wires":[]},{"id":"48d2b7cfa10e35d4","type":"function","z":"3d8bc5187eb93fc0","name":"send message","func":"// see https://gram.js.org/tl/messages/SendMessage\nlet username = msg.payload;\nmsg.payload = {\n api: 'messages',\n func: 'SendMessage',\n args: {\n peer: username,\n message: \"Hello there!\",\n randomId: BigInt(\"-4156887774564\"),\n noWebpage: true,\n noforwards: true,\n scheduleDate: 0,\n // sendAs: \"???\",\n }\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":400,"y":640,"wires":[["7f66f74d4da2179d"]]},{"id":"57bcd2488838edef","type":"inject","z":"3d8bc5187eb93fc0","name":"","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":170,"y":640,"wires":[["48d2b7cfa10e35d4"]]},{"id":"b4217d5b3ad6f705","type":"telegram client config","botname":"Username","verboselogging":false}]
1+
[{"id":"7f66f74d4da2179d","type":"telegram client sender","z":"3d8bc5187eb93fc0","name":"","bot":"b4217d5b3ad6f705","x":640,"y":460,"wires":[["fce968085d0d85fb"]]},{"id":"fce968085d0d85fb","type":"debug","z":"3d8bc5187eb93fc0","name":"debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":830,"y":460,"wires":[]},{"id":"48d2b7cfa10e35d4","type":"function","z":"3d8bc5187eb93fc0","name":"send message","func":"// see https://gram.js.org/tl/messages/SendMessage\nlet randomId = BigInt(Math.floor(Math.random() * 1e15));\nlet username = msg.payload;\nmsg.payload = {\n api: 'messages',\n func: 'SendMessage',\n args: {\n peer: \"to username\",\n message: \"Test1\",\n randomId: randomId,\n noWebpage: true,\n noforwards: true,\n scheduleDate: 0,\n // sendAs: \"from username\",\n }\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":420,"y":460,"wires":[["7f66f74d4da2179d"]]},{"id":"57bcd2488838edef","type":"inject","z":"3d8bc5187eb93fc0","name":"","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":190,"y":460,"wires":[["48d2b7cfa10e35d4"]]},{"id":"b4217d5b3ad6f705","type":"telegram client config","botname":"Martina","verboselogging":false}]

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-red-node-telegrambot",
3-
"version": "0.0.5",
3+
"version": "0.1.0",
44
"description": "Telegram userbot nodes for Node-RED",
55
"dependencies": {
66
"telegram": "^2.18.38"

telegrambot/telegrambot.html

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,9 @@ <h3>Details</h3>
196196
inputs: 0,
197197
outputs: 1,
198198
icon: "telegram.png",
199-
paletteLabel: "client",
199+
paletteLabel: "client receiver",
200200
label: function () {
201-
return this.name || "Telegram client";
201+
return this.name || "Telegram client receiver";
202202
},
203203
labelStyle: function() {
204204
return this.name?"node_label_italic":"";
@@ -220,11 +220,11 @@ <h3>Details</h3>
220220
</script>
221221

222222
<script type="text/x-red" data-help-name="telegram client receiver">
223-
<p>A telegram node that triggers the output when some message is received from the chat.</p>
223+
<p>A telegram node that triggers the output when some message event is received from the chat.</p>
224224
<p>The node receives all messages received from the telegram server.</p>
225225

226226
<h3>Outputs</h3>
227-
<p>1. Standard Ouput: Message is sent to output.</p>
227+
<p>1. Standard Output: Event message is sent to output.</p>
228228
</script>
229229

230230
<!-- ------------------------------------------------------------------------------------------ -->
@@ -240,9 +240,9 @@ <h3>Outputs</h3>
240240
inputs: 1,
241241
outputs: 1,
242242
icon: "telegram.png",
243-
paletteLabel: "client",
243+
paletteLabel: "client sender",
244244
label: function () {
245-
return this.name || "Telegram client";
245+
return this.name || "Telegram client sender";
246246
},
247247
labelStyle: function() {
248248
return this.name?"node_label_italic":"";
@@ -268,9 +268,17 @@ <h3>Outputs</h3>
268268
<p>The node receives data and sends it to the telegram server.</p>
269269

270270
<h3>Inputs</h3>
271-
<p>TODO:</p>
272-
273-
<h3>Inputs</h3>
271+
<p>1. Standard Input: receives a message object containing the following fields:</p>
272+
<p>The <code>msg.payload</code> must be an object that contains the following properties:</p>
273+
<ul>
274+
<li><code>api</code> the api to call e.g. "messages" (see https://gram.js.org/tl/messages/SendMessage)</li>
275+
<li><code>func</code> the function of the api to call e.g. "SendMessage" (see https://gram.js.org/tl/messages/SendMessage)</li>
276+
<li><code>args</code> arguments of the function.</li>
277+
</ul>
278+
<h3>Outputs</h3>
279+
<p>1. Standard Output: returns the result of the api call.</p>
280+
<h3>Details</h3>
281+
<p>Please refer to the examples in the examples folder and the readme.</p>
274282
<p></p>
275283
</script>
276284

telegrambot/telegrambot.js

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -162,32 +162,40 @@ module.exports = function (RED) {
162162
this.phoneNumber = this.credentials.phonenumber || '';
163163
}
164164

165-
this.createTelegramClient = async function createTelegramClient(apiId, apiHash, session, logLevel) {
165+
this.createTelegramClient = async function createTelegramClient(node, apiId, apiHash, session, phoneNumber, logLevel) {
166166
let client;
167-
if (apiId !== undefined && apiId !== '') {
168-
const stringSession = new StringSession(session);
169-
const ID = Number(apiId);
170-
client = new TelegramClient(stringSession, ID, apiHash, {
171-
connectionRetries: 5,
172-
});
173-
174-
client.setLogLevel(logLevel);
175-
176-
await client.start({
177-
onError: (err) => {
178-
console.log(err);
179-
return true; // true means that it should be stopped.
180-
},
181-
});
167+
try {
168+
if (apiId !== undefined && apiId !== '' && session !== undefined && session !== '') {
169+
const stringSession = new StringSession(session);
170+
const ID = Number(apiId);
171+
client = new TelegramClient(stringSession, ID, apiHash, {
172+
connectionRetries: 5,
173+
});
174+
175+
client.setLogLevel(logLevel);
176+
177+
let authParams = {
178+
phoneNumber: phoneNumber,
179+
onError: (err) => {
180+
console.log(err);
181+
return true; // abort
182+
},
183+
};
184+
await client.start(authParams);
185+
} else {
186+
node.warn('No session: login first.');
187+
}
188+
} catch (error) {
189+
node.warn(error);
182190
}
183191

184192
return client;
185193
};
186194

187195
// Activates the client or returns the already activated bot.
188-
this.getTelegramClient = async function () {
196+
this.getTelegramClient = async function (node) {
189197
if (!this.client) {
190-
this.client = await this.createTelegramClient(this.apiId, this.apiHash, this.session, this.phoneNumber, this.logLevel);
198+
this.client = await this.createTelegramClient(node, this.apiId, this.apiHash, this.session, this.phoneNumber, this.logLevel);
191199
}
192200

193201
return this.client;
@@ -234,7 +242,7 @@ module.exports = function (RED) {
234242
};
235243

236244
const start = async () => {
237-
let client = await node.config.getTelegramClient();
245+
let client = await node.config.getTelegramClient(node);
238246
if (client) {
239247
node.status({
240248
fill: 'green',
@@ -243,15 +251,6 @@ module.exports = function (RED) {
243251
});
244252

245253
client.addEventHandler(eventHandler, new NewMessage({}));
246-
247-
// client.addEventHandler((update) => {
248-
// let msg = {
249-
// payload : {
250-
// update : update,
251-
// }
252-
// }
253-
// node.send(msg);
254-
// });
255254
}
256255
};
257256
start();
@@ -272,7 +271,7 @@ module.exports = function (RED) {
272271
this.config = RED.nodes.getNode(this.bot);
273272

274273
const start = async () => {
275-
let client = await node.config.getTelegramClient();
274+
let client = await node.config.getTelegramClient(node);
276275
if (client) {
277276
node.status({
278277
fill: 'green',
@@ -292,7 +291,8 @@ module.exports = function (RED) {
292291
if (api !== undefined && func !== undefined) {
293292
(async () => {
294293
try {
295-
const result = await client.invoke(new Api[api][func](args));
294+
let request = new Api[api][func](args);
295+
const result = await client.invoke(request);
296296
msg.payload = result;
297297
nodeSend(msg);
298298
} catch (error) {
@@ -314,7 +314,7 @@ module.exports = function (RED) {
314314

315315
this.on('input', async function (msg, nodeSend, nodeDone) {
316316
if (msg.payload) {
317-
let client = await node.config.getTelegramClient();
317+
let client = await node.config.getTelegramClient(node);
318318
if (client) {
319319
this.processMessage(client, msg, nodeSend, nodeDone);
320320
}

0 commit comments

Comments
 (0)