Skip to content
Open
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
62 changes: 55 additions & 7 deletions context.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ const UpdateTypes = [
'pre_checkout_query',
'message',
'poll',
'poll_answer'
'poll_answer',
'my_chat_member',
'chat_member'
]

const MessageSubTypes = [
Expand Down Expand Up @@ -43,7 +45,12 @@ const MessageSubTypes = [
'connected_website',
'passport_data',
'poll',
'forward_date'
'forward_date',
'message_auto_delete_timer_changed',
'voice_chat_started',
'voice_chat_ended',
'voice_chat_participants_invited',
'voice_chat_scheduled'
]

const MessageSubTypesMapping = {
Expand Down Expand Up @@ -120,12 +127,22 @@ class TelegrafContext {
return this.update.poll_answer
}

get myChatMember () {
return this.update.my_chat_member
}

get chatMember () {
return this.update.chat_member
}

get chat () {
return (this.message && this.message.chat) ||
(this.editedMessage && this.editedMessage.chat) ||
(this.callbackQuery && this.callbackQuery.message && this.callbackQuery.message.chat) ||
(this.channelPost && this.channelPost.chat) ||
(this.editedChannelPost && this.editedChannelPost.chat)
(this.editedChannelPost && this.editedChannelPost.chat) ||
(this.myChatMember && this.myChatMember.chat) ||
(this.chatMember && this.chatMember.chat)
}

get from () {
Expand All @@ -137,7 +154,9 @@ class TelegrafContext {
(this.editedChannelPost && this.editedChannelPost.from) ||
(this.shippingQuery && this.shippingQuery.from) ||
(this.preCheckoutQuery && this.preCheckoutQuery.from) ||
(this.chosenInlineResult && this.chosenInlineResult.from)
(this.chosenInlineResult && this.chosenInlineResult.from) ||
(this.myChatMember && this.myChatMember.from) ||
(this.chatMember && this.chatMember.from)
}

get inlineMessageId () {
Expand Down Expand Up @@ -325,6 +344,11 @@ class TelegrafContext {
return this.telegram.exportChatInviteLink(this.chat.id, ...args)
}

banChatMember (...args) {
this.assert(this.chat, 'banChatMember')
return this.telegram.banChatMember(this.chat.id, ...args)
}

kickChatMember (...args) {
this.assert(this.chat, 'kickChatMember')
return this.telegram.kickChatMember(this.chat.id, ...args)
Expand Down Expand Up @@ -407,7 +431,12 @@ class TelegrafContext {

getChatMembersCount (...args) {
this.assert(this.chat, 'getChatMembersCount')
return this.telegram.getChatMembersCount(this.chat.id, ...args)
return this.telegram.getChatMemberCount(this.chat.id, ...args)
}

getChatMemberCount (...args) {
this.assert(this.chat, 'getChatMemberCount')
return this.telegram.getChatMemberCount(this.chat.id, ...args)
}

setPassportDataErrors (errors) {
Expand Down Expand Up @@ -551,14 +580,18 @@ class TelegrafContext {
return this.telegram.addStickerToSet(this.from.id, ...args)
}

getMyCommands () {
return this.telegram.getMyCommands()
getMyCommands (...args) {
return this.telegram.getMyCommands(...args)
}

setMyCommands (...args) {
return this.telegram.setMyCommands(...args)
}

deleteMyCommands (...args) {
return this.telegram.deleteMyCommands(...args)
}

replyWithMarkdown (markdown, extra) {
return this.reply(markdown, { parse_mode: 'Markdown', ...extra })
}
Expand Down Expand Up @@ -605,6 +638,21 @@ class TelegrafContext {
this.assert(message, 'copyMessage')
return this.telegram.copyMessage(chatId, message.chat.id, message.message_id, extra)
}

createChatInviteLink (...args) {
this.assert(this.chat, 'createChatInviteLink')
return this.telegram.createChatInviteLink(this.chat.id, ...args)
}

editChatInviteLink (...args) {
this.assert(this.chat, 'editChatInviteLink')
return this.telegram.editChatInviteLink(this.chat.id, ...args)
}

revokeChatInviteLink (...args) {
this.assert(this.chat, 'revokeChatInviteLink')
return this.telegram.revokeChatInviteLink(this.chat.id, ...args)
}
}

module.exports = TelegrafContext
75 changes: 62 additions & 13 deletions docs/telegram.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@ Use this method to delete bot messages.
| chatId | `number/string` | Chat id |
| messageId | `string` | Message id |

## deleteMyCommands

Use this method to delete the list of the bot's commands for the given scope and user language.

`telegram.deleteMyCommands([extra]) => Promise`

| Param | Type | Description |
| --- | --- | --- |
| [extra] | `object` | [Extra parameters](https://core.telegram.org/bots/api#deletemycommands) |

## setStickerSetThumb

Use this method to set the thumbnail of a sticker set.
Expand Down Expand Up @@ -352,12 +362,12 @@ Use this method to get information about a member of a chat.
| chatId | `number/string` | Chat id |
| userId | `number` | User identifier |

## getChatMembersCount
## getChatMemberCount

Use this method to get the number of members in a chat.

`telegram.getChatMembersCount(chatId) => Promise`
[Official documentation](https://core.telegram.org/bots/api#getchatmemberscount)
`telegram.getChatMemberCount(chatId) => Promise`
[Official documentation](https://core.telegram.org/bots/api#getchatmembercount)

| Param | Type | Description |
| --- | --- | --- |
Expand Down Expand Up @@ -393,10 +403,13 @@ Returns basic information about the bot.

## getMyCommands

Use this method to get the current list of the bot's commands. Requires no parameters. Returns Array of BotCommand on success.
Use this method to get the current list of the bot's commands for the given scope and user language.

`telegram.getMyCommands() => Promise`
[Official documentation](https://core.telegram.org/bots/api#getmycommands)
`telegram.getMyCommands([extra]) => Promise`

| Param | Type | Description |
| --- | --- | --- |
| [extra] | `object` | [Extra parameters](https://core.telegram.org/bots/api#getmycommands) |

## getStickerSet

Expand Down Expand Up @@ -433,17 +446,17 @@ Use this method to set default chat permissions for all members.
| chatId | `number/string` | Chat id |
| permissions | `object` | [New default chat permissions](https://core.telegram.org/bots/api#chatpermissions)|

## kickChatMember
## banChatMember

Use this method to kick a user from a group or a supergroup.
Use this method to ban a user in a group, a supergroup or a channel.

`telegram.kickChatMember(chatId, userId, [extra]) => Promise`
`telegram.banChatMember(chatId, userId, [extra]) => Promise`

| Param | Type | Description |
| --- | --- | --- |
| chatId | `number/string` | Chat id |
| userId | `number` | User id |
| [extra] | `object` | [Extra parameters](https://core.telegram.org/bots/api#kickchatmember)|
| [extra] | `object` | [Extra parameters](https://core.telegram.org/bots/api#banchatmember)|

## restrictChatMember

Expand Down Expand Up @@ -492,6 +505,41 @@ Use this method to export an invite link to a supergroup or a channel.
| --- | --- | --- |
| chatId | `number/string` | Chat id |

## createChatInviteLink

Use this method to create an additional invite link for a chat.

`telegram.createChatInviteLink(chatId, [extra]) => Promise`

| Param | Type | Description |
| --- | --- | --- |
| chatId | `number/string` | Chat id |
| [extra] | `object` | [Extra parameters](https://core.telegram.org/bots/api#createchatinvitelink) |

## editChatInviteLink

Use this method to edit a non-primary invite link created by the bot.

`telegram.editChatInviteLink(chatId, inviteLink, [extra]) => Promise`

| Param | Type | Description |
| --- | --- | --- |
| chatId | `number/string` | Chat id |
| inviteLink | `string` | The invite link to edit |
| [extra] | `object` | [Extra parameters](https://core.telegram.org/bots/api#editchatinvitelink) |

## revokeChatInviteLink

Use this method to revoke an invite link created by the bot.

`telegram.revokeChatInviteLink(chatId, inviteLink) => Promise`
[Official documentation](https://core.telegram.org/bots/api#revokechatinvitelink)

| Param | Type | Description |
| --- | --- | --- |
| chatId | `number/string` | Chat id |
| inviteLink | `string` | The invite link to revoke |

## setChatPhoto

Use this method to set a new profile photo for the chat.
Expand Down Expand Up @@ -842,11 +890,12 @@ Sends a poll.

Use this method to change the list of the bot's commands

`telegram.setMyCommands(commands) => Promise`
`telegram.setMyCommands(commands, [extra]) => Promise`

| Param | Type | Description |
| --- | --- | --- |
| commands | `object[]` | [List of bot commands](https://core.telegram.org/bots/api#setmycommands) |
| commands | `object[]` | [List of bot commands](https://core.telegram.org/bots/api#botcommand) |
| [extra] | `object` | [Extra parameters](https://core.telegram.org/bots/api#setmycommands) |

## sendQuiz

Expand Down Expand Up @@ -914,7 +963,7 @@ Specifies an url to receive incoming updates via an outgoing webhook.

## unbanChatMember

Use this method to unban a previously kicked user in a supergroup.
Use this method to unban a previously banned user in a supergroup or channel.

`telegram.unbanChatMember(chatId, userId) => Promise`
[Official documentation](https://core.telegram.org/bots/api#unbanchatmember)
Expand Down
26 changes: 23 additions & 3 deletions markup.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ class Markup {
return this
}

inputFieldPlaceholder (placeholder) {
this.input_field_placeholder = placeholder
return this
}

selective (value = true) {
this.selective = value
return this
Expand Down Expand Up @@ -111,6 +116,10 @@ class Markup {
return new Markup().resize(value)
}

static inputFieldPlaceholder (placeholder) {
return new Markup().inputFieldPlaceholder(placeholder)
}

static selective (value = true) {
return new Markup().selective(value)
}
Expand Down Expand Up @@ -168,11 +177,10 @@ class Markup {
}

static formatHTML (text = '', entities = []) {
const chars = [...text]
const available = [...entities]
const opened = []
const result = []
for (let offset = 0; offset < chars.length; offset++) {
for (let offset = 0; offset < text.length; offset++) {
while (true) {
const index = available.findIndex((entity) => entity.offset === offset)
if (index === -1) {
Expand Down Expand Up @@ -213,7 +221,7 @@ class Markup {
available.splice(index, 1)
}

result.push(chars[offset])
result.push(escapeHTML(text[offset]))

while (true) {
const index = opened.findIndex((entity) => entity.offset + entity.length - 1 === offset)
Expand Down Expand Up @@ -256,6 +264,18 @@ class Markup {
}
}

const escapedChars = {
'"': '&quot;',
'&': '&amp;',
'<': '&lt;',
'>': '&gt;'
}

function escapeHTML (string) {
const chars = [...string]
return chars.map(char => escapedChars[char] || char).join('')
}

function buildKeyboard (buttons, options) {
const result = []
if (!Array.isArray(buttons)) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"module-alias": "^2.2.2",
"node-fetch": "^2.2.0",
"sandwich-stream": "^2.0.1",
"typegram": "^3.1.5"
"typegram": "^3.4.3"
},
"devDependencies": {
"@types/node": "^13.1.0",
Expand Down
Loading