2
0
mirror of https://github.com/yagop/node-telegram-bot-api synced 2025-08-30 13:58:27 +00:00

src/telegram: Add metadata argument in message event (and friends)

References:

  * FR: https://github.com/yagop/node-telegram-bot-api/issues/409
  * PR: https://github.com/yagop/node-telegram-bot-api/pull/413
This commit is contained in:
GochoMugo
2017-11-18 21:44:06 +03:00
parent 3722c7182c
commit 49df0c6e02
3 changed files with 16 additions and 11 deletions

View File

@@ -7,7 +7,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
Added:
* Add support for Node.js v9
1. Add `metadata` argument in `message` event (and
friends e.g. `text`, `audio`, etc.) (#409) (by @jlsjonas, @GochoMugo)
1. Add support for Node.js v9 (by @GochoMugo)
Changed:

View File

@@ -8,17 +8,19 @@
<a name="events"></a>
## Events
*TelegramBot* is an event-emitter that emits the following events:
*TelegramBot* is an [EventEmitter](https://nodejs.org/api/events.html#events_class_eventemitter)
that emits the following events:
1. `message`: Received a new incoming [Message][message] of any kind
1. Depending on the properties of the [Message][message], one of these
events will **ALSO** be emitted: `text`, `audio`, `document`, `photo`,
events may **ALSO** be emitted: `text`, `audio`, `document`, `photo`,
`sticker`, `video`, `voice`, `contact`, `location`,
`new_chat_members`, `left_chat_member`, `new_chat_title`,
`new_chat_photo`, `delete_chat_photo`, `group_chat_created`,
`game`, `pinned_message`, `migrate_from_chat_id`, `migrate_to_chat_id`,
`channel_chat_created`, `supergroup_chat_created`,
`successful_payment`, `invoice`, `video_note`
1. **Arguments**: `message` ([Message][message]), `metadata` (`{ type?:string }`)
1. `new_chat_participant`, `left_chat_participant` are **deprecated**
1. `callback_query`: Received a new incoming [Callback Query][callback-query]
1. `inline_query`: Received a new incoming [Inline Query][inline-query]

View File

@@ -531,14 +531,15 @@ class TelegramBot extends EventEmitter {
if (message) {
debug('Process Update message %j', message);
this.emit('message', message);
const processMessageType = messageType => {
if (message[messageType]) {
debug('Emitting %s: %j', messageType, message);
this.emit(messageType, message);
const metadata = {};
metadata.type = TelegramBot.messageTypes.find((messageType) => {
return message[messageType];
});
this.emit('message', message, metadata);
if (metadata.type) {
debug('Emitting %s: %j', metadata.type, message);
this.emit(metadata.type, message, metadata);
}
};
TelegramBot.messageTypes.forEach(processMessageType);
if (message.text) {
debug('Text message');
this._textRegexpCallbacks.some(reg => {