Skip to content
This repository was archived by the owner on Aug 25, 2023. It is now read-only.

Commit c1f9bdc

Browse files
committed
Code clean up, added README.md, consistency for chat_id arguments
1 parent c583083 commit c1f9bdc

File tree

7 files changed

+787
-25
lines changed

7 files changed

+787
-25
lines changed

LICENSE

Lines changed: 674 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Nodeogram
2+
Nodeogram is a simple yet complete Node.JS module for Telegram bots.
3+
4+
## Features
5+
* **Not a wrapper**. Nodeogram takes the Telegram API to the next level and allows you to create your bots while handling all the boring stuff for you
6+
* **Complete**. Nodeogram features all of the most recent API updates and it's designed to not block you from using right away the not yet implemented ones
7+
* **Promises**. The entire library is promise-based
8+
* **Event based**. Nodeogram provides an event-based handling of updates, allowing you to interact more easily with message, commands, callback and line queries.
9+
10+
## Getting started
11+
12+
First, you'll need to install the module
13+
14+
```bash
15+
npm i --save nodeogram
16+
```
17+
18+
Once the packages is installed, you can start working on you bot.
19+
20+
```javascript
21+
const nodeogram = require('nodeogram'),
22+
bot = new nodeogram.Bot('your-token-goes-here');
23+
24+
bot.init();
25+
bot.on('message', message => message.reply("Hello World!"));
26+
```
27+
28+
And you are done.
29+
30+
## License
31+
32+
Nodeogram - A Node.JS Telegram bots API library
33+
Copyright (C) 2016 Alberto Coscia <[email protected]>
34+
35+
This program is free software: you can redistribute it and/or modify
36+
it under the terms of the GNU General Public License as published by
37+
the Free Software Foundation, either version 3 of the License, or
38+
(at your option) any later version.
39+
40+
This program is distributed in the hope that it will be useful,
41+
but WITHOUT ANY WARRANTY; without even the implied warranty of
42+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43+
GNU General Public License for more details.
44+
45+
You should have received a copy of the GNU General Public License
46+
along with this program. If not, see <http://www.gnu.org/licenses/>.

lib/Bot.js

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ function Bot(token) {
2525
this.emitter = new events.EventEmitter();
2626

2727
this.handleAPIError = (err) => {
28-
console.log(`An error occurred while querying the Telegram API - ${JSON.parse(err.response.text).error_code} ${JSON.parse(err.response.text).description}`)
28+
if (err.response) { console.log(`An error occurred while querying the Telegram API - ${err.response.text ? JSON.parse(err.response.text).error_code : ""} ${err.response.text ? JSON.parse(err.response.text).description : ""}`)}
29+
else console.log(err)
2930
};
3031

3132
this.get = (path, query) => {
@@ -93,14 +94,15 @@ function Bot(token) {
9394
});
9495
}
9596
if (result.inline_query) this.emitter.emit('inline_query', new InlineQuery(result.inline_query, this));
96-
if (result.callback_query) this.emitter.emit('callback_query', new CallbackQuery(result.callback_query, this))
97-
if (result.edited_message) this.emitter.emit('edited_message', new Message(result.edited_message, this))
97+
if (result.callback_query) this.emitter.emit('callback_query', new CallbackQuery(result.callback_query, this));
98+
if (result.edited_message) this.emitter.emit('edited_message', new Message(result.edited_message, this));
99+
if (result.chosen_inline_result) this.emitter.emit('chosen_inline_result', new ChosenInlineResult(result.chosen_inline_result, this))
98100
});
99101
}
100102
this.getUpdates();
101103
})
102104
.catch((err) => {
103-
console.log(err)
105+
this.handleAPIError(err);
104106
// Wait before retrying in order not to flood Telegram's servers
105107
setTimeout(() => {
106108
this.getUpdates();
@@ -113,19 +115,21 @@ function Bot(token) {
113115
this.commands.push({command: command, description: description, callback: callback});
114116
};
115117

116-
this.getChat = (id) => {
117-
return this.call('getChat', {chat_id: id}).then((res) => {
118+
this.getChat = (chat_id) => {
119+
return this.call('getChat', {chat_id: chat_id}).then((res) => {
118120
return new Chat(res.body.result, this)
119121
})
120122
};
121123

122-
this.leaveChat = (id) => {
123-
return this.call('leaveChat', {chat_id: id}).then((res) => {
124+
this.leaveChat = (chat_id) => {
125+
if (chat_id instanceof Chat) chat_id = chat_id.id;
126+
return this.call('leaveChat', {chat_id: chat_id}).then((res) => {
124127
return res.body.result;
125128
})
126129
};
127130

128131
this.sendMessage = (chat_id, text, options) => {
132+
if (chat_id instanceof Chat) chat_id = chat_id.id;
129133
options = options || {};
130134
options.chat_id = chat_id;
131135
options.text = text;
@@ -135,6 +139,7 @@ function Bot(token) {
135139
};
136140

137141
this.sendLocation = (chat_id, longitude, latitude, options) => {
142+
if (chat_id instanceof Chat) chat_id = chat_id.id;
138143
options = options || {};
139144
options.chat_id = chat_id;
140145
options.longitude = longitude;
@@ -145,6 +150,7 @@ function Bot(token) {
145150
};
146151

147152
this.sendVenue = (chat_id, longitude, latitude, title, address, options) => {
153+
if (chat_id instanceof Chat) chat_id = chat_id.id;
148154
options = options || {};
149155
options.chat_id = chat_id;
150156
options.longitude = longitude;
@@ -157,6 +163,7 @@ function Bot(token) {
157163
};
158164

159165
this.sendContact = (chat_id, phone_number, first_name, options) => {
166+
if (chat_id instanceof Chat) chat_id = chat_id.id;
160167
options = options || {};
161168
options.chat_id = chat_id;
162169
options.phone_number = phone_number;
@@ -168,6 +175,7 @@ function Bot(token) {
168175

169176

170177
this.forwardMessage = (chat_id, from_chat_id, message_id, options) => {
178+
if (chat_id instanceof Chat) chat_id = chat_id.id;
171179
options = options || {};
172180
options.chat_id = chat_id;
173181
options.from_chat_id = from_chat_id;
@@ -181,9 +189,10 @@ function Bot(token) {
181189
return this.call('answerCallbackQuery', {callback_query_id: id, text: text, alert: alert});
182190
};
183191

184-
this.getUserProfilePhotos = (id, options) => {
192+
this.getUserProfilePhotos = (user_id, options) => {
193+
if (user_id instanceof User) user_id = user_id.id;
185194
options = options || {};
186-
options.user_id = id;
195+
options.user_id = user_id;
187196
return this.call('getUserProfilePhotos', options).then((res) => {
188197
return new UserProfilePhotos(res.body.result, this);
189198
});
@@ -196,6 +205,7 @@ function Bot(token) {
196205
};
197206

198207
this.sendFile = (chat_id, type, path, options) => {
208+
if (chat_id instanceof Chat) chat_id = chat_id.id;
199209
options = options || {};
200210
options.chat_id = chat_id;
201211
var method;
@@ -310,6 +320,7 @@ function Bot(token) {
310320
};
311321

312322
this.getChatAdministrators = (chat_id) => {
323+
if (chat_id instanceof Chat) chat_id = chat_id.id;
313324
return this.call('getChatAdministrators', {chat_id: chat_id}).then((res) => {
314325
var administrators = res.body.result;
315326
var processed = [];
@@ -321,32 +332,37 @@ function Bot(token) {
321332
};
322333

323334
this.getChatMember = (chat_id, user_id) => {
335+
if (chat_id instanceof Chat) chat_id = chat_id.id;
324336
if (user_id instanceof User) user_id = user_id.id;
325337
return this.call('getChatMember', {chat_id: chat_id, user_id: user_id}).then((res) => {
326338
return new User(res.body.result, this);
327339
})
328340
};
329341

330342
this.kickChatMember = (chat_id, user_id) => {
343+
if (chat_id instanceof Chat) chat_id = chat_id.id;
331344
if (user_id instanceof User) user_id = user_id.id;
332345
return this.call('kickChatMember', {chat_id: chat_id, user_id: user_id}).then((res) => {
333346
return res.body.result;
334347
})
335348
};
336349
this.unbanChatMember = (chat_id, user_id) => {
350+
if (chat_id instanceof Chat) chat_id = chat_id.id;
337351
if (user_id instanceof User) user_id = user_id.id;
338352
return this.call('unbanChatMember', {chat_id: chat_id, user_id: user_id}).then((res) => {
339353
return res.body.result;
340354
})
341355
};
342356

343357
this.getChatMembersCount = (chat_id) => {
358+
if (chat_id instanceof Chat) chat_id = chat_id.id;
344359
return this.call('getChatMembersCount', {chat_id: chat_id}).then((res) => {
345360
return res.body.result;
346361
})
347362
};
348363

349364
this.sendChatAction = (chat_id, action) => {
365+
if (chat_id instanceof Chat) chat_id = chat_id.id;
350366
return this.call('sendChatAction', {chat_id: chat_id, action: action}).then((res) => {
351367
return res.body.result;
352368
})
@@ -364,11 +380,23 @@ function Bot(token) {
364380
};
365381

366382
this.handleMessage = (message) => {
383+
if (message.new_chat_member) this.emitter.emit('new_chat_member', message.new_chat_member, message);
384+
if (message.left_chat_member) this.emitter.emit('left_chat_member', message.left_chat_member, message);
385+
if (message.new_chat_title) this.emitter.emit('new_chat_title', message.new_chat_title, message);
386+
if (message.new_chat_photo) this.emitter.emit('new_chat_photo', message.new_chat_photo, message);
387+
if (message.delete_chat_photo) this.emitter.emit('delete_chat_photo ', message.delete_chat_photo, message);
388+
if (message.group_chat_created) this.emitter.emit('group_chat_created', message.group_chat_created, message);
389+
if (message.supergroup_chat_created) this.emitter.emit('supergroup_chat_created', message.supergroup_chat_created, message);
390+
if (message.channel_chat_created) this.emitter.emit('channel_chat_created', message.channel_chat_created, message);
391+
if (message.migrate_to_chat_id) this.emitter.emit('migrate_to_chat_id', message.migrate_to_chat_id, message);
392+
if (message.migrate_from_chat_id) this.emitter.emit('migrate_from_chat_id', message.migrate_from_chat_id, message);
393+
if (message.pinned_message) this.emitter.emit('pinned_message', message.pinned_message, message);
394+
367395
this.emitter.emit('message', message);
368396
};
369397

370398
this.init = () => {
371-
if (!this.token) throw "Unvalid token!";
399+
if (!this.token) throw "Invalid token!";
372400
this.messageHandlers = [
373401
this.handleCommands,
374402
this.handleMessage
@@ -384,8 +412,9 @@ function Bot(token) {
384412
};
385413

386414
this.call('getMe', {}).then((res) => {
387-
this.me = res.body.result;
388-
});
415+
if (res) this.me = res.body.result;
416+
else throw "Unable to retrieve informations about the bot!"
417+
}).catch(err => this.handleAPIError(err));
389418

390419
// For more complex event handling use this.emitter
391420
this.on = (event, listener) => {

lib/CallbackQuery.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22
const User = require('./User.js'),
3-
Chat = require('./Chat.js'),
43
Message = require('./Message.js'),
54
extend = require('util')._extend;
65

lib/ChosenInlineResult.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ function ChosenInlineResult(object, bot) {
88
this.bot = bot;
99
this.user = new User(this.user, this.bot);
1010
}
11+
12+
module.exports = ChosenInlineResult;

lib/Message.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ function Message(object, bot) {
1111
this.from = new User(this.from, bot);
1212
this.chat = new Chat(this.chat, bot);
1313
this.forward_from = new User(this.forward_from, bot);
14+
this.forward_from_chat = new Chat(this.forward_from_chat, bot);
1415
this.reply_to_this = module.exports(this.reply_to_this);
1516
this.new_chat_member = new User(this.new_chat_member, bot);
1617
this.left_chat_member = new User(this.left_chat_member, bot);
@@ -54,35 +55,39 @@ function Message(object, bot) {
5455
this.links.push({type: 'link', url: entity.url, text: text});
5556
break;
5657
case 'text_mention':
57-
this.text_mentions.push({text: text, user: new User(entity.user, this.bot)})
58+
this.text_mentions.push({text: text, user: new User(entity.user, this.bot)});
5859
break;
5960
}
6061
})
6162
}
6263

6364
if (this.photo) {
64-
var sizes = [];
65+
let sizes = [];
6566
this.photo.forEach((size) => {
6667
sizes.push(new PhotoSize(size, this.bot))
6768
});
6869
this.photo = sizes;
6970
}
7071

72+
if (this.new_chat_photo) {
73+
let sizes = [];
74+
this.new_chat_photo.forEach((size) => {
75+
sizes.push(new PhotoSize(size, this.bot))
76+
});
77+
this.new_chat_photo = sizes;
78+
}
79+
7180
this.reply = (text, options) => {
7281
options = options || {};
7382
options.reply_to_message_id = this.message_id;
7483
return this.chat.sendMessage(text, options)
7584
};
7685

77-
this.forward = (chat, options) => {
78-
// A chat id was passed
79-
if (typeof chat == "number" || typeof chat == "string") {
80-
return this.bot.forwardMessage(chat, this.chat.id, this.message_id, options)
81-
}
82-
// A Chat object was passed
83-
if (typeof chat == "object") {
84-
return chat.forwardMessage(this.chat.id, this.message_id, options)
85-
}
86+
this.forward = (chat_id, options) => {
87+
if (chat_id instanceof Chat) chat_id = chat_id.id;
88+
if (chat_id instanceof User) chat_id = chat_id.id;
89+
90+
return this.bot.forwardMessage(chat_id, this.chat.id, this.message_id, options)
8691
};
8792

8893
this.editText = (text, inline, options) => {

package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010
"telegram",
1111
"bot"
1212
],
13+
"repository" : {
14+
"type": "git",
15+
"url": "https://github.com/ALCC01/nodeogram.git"
16+
},
17+
"bugs": {
18+
"url": "https://github.com/ALCC01/nodeogram/issues"
19+
},
1320
"author": "Alberto Coscia",
1421
"license": "GPL-3.0",
1522
"dependencies": {

0 commit comments

Comments
 (0)