Skip to content

Commit 1273631

Browse files
committed
Merge branch 'release/3.38.0'
2 parents 0cb042a + 0d10c14 commit 1273631

24 files changed

+2436
-1616
lines changed

.mailmap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Wojciech Pawlik <[email protected]>

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

.prettierrc.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"insertPragma": true,
3+
"requirePragma": true,
4+
"semi": false,
5+
"singleQuote": true
6+
}

context.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,11 @@ class TelegrafContext {
330330
return this.telegram.kickChatMember(this.chat.id, ...args)
331331
}
332332

333+
unbanChatMember (...args) {
334+
this.assert(this.chat, 'unbanChatMember')
335+
return this.telegram.unbanChatMember(this.chat.id, ...args)
336+
}
337+
333338
restrictChatMember (...args) {
334339
this.assert(this.chat, 'restrictChatMember')
335340
return this.telegram.restrictChatMember(this.chat.id, ...args)

docs/README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ These accounts serve as an interface for code running somewhere on your server.
88

99
#### Features
1010

11-
- Full [Telegram Bot API 4.7](https://core.telegram.org/bots/api) support
11+
- Full [Telegram Bot API 4.8](https://core.telegram.org/bots/api) support
1212
- [Telegram Payment Platform](https://telegram.org/blog/payments)
1313
- [HTML5 Games](https://core.telegram.org/bots/api#games)
1414
- [Inline mode](https://core.telegram.org/bots/api#inline-mode)
@@ -33,7 +33,7 @@ $ yarn add telegraf
3333
#### Example
3434

3535
```js
36-
const Telegraf = require('telegraf')
36+
const { Telegraf } = require('telegraf')
3737

3838
const bot = new Telegraf(process.env.BOT_TOKEN)
3939
bot.start((ctx) => ctx.reply('Welcome'))
@@ -44,7 +44,7 @@ bot.launch()
4444
```
4545

4646
```js
47-
const Telegraf = require('telegraf')
47+
const { Telegraf } = require('telegraf')
4848

4949
const bot = new Telegraf(process.env.BOT_TOKEN)
5050
bot.command('oldschool', (ctx) => ctx.reply('Hello'))
@@ -281,6 +281,7 @@ Context shortcuts for **message** update:
281281
| `stopPoll` | [`telegram.stopPoll`](#stoppoll) |
282282
| `unpinChatMessage` | [`telegram.unpinChatMessage`](#unpinchatmessage) |
283283
| `uploadStickerFile` | [`telegram.uploadStickerFile`](#uploadstickerfile) |
284+
| `unbanChatMember` | [`telegram.unbanChatMember`](#unbanchatmember) |
284285

285286
Context shortcuts for **callback_query** update:
286287

@@ -332,6 +333,7 @@ Context shortcuts for **callback_query** update:
332333
| `stopPoll` | [`telegram.stopPoll`](#stoppoll) |
333334
| `unpinChatMessage` | [`telegram.unpinChatMessage`](#unpinchatmessage) |
334335
| `uploadStickerFile` | [`telegram.uploadStickerFile`](#uploadstickerfile) |
336+
| `unbanChatMember` | [`telegram.unbanChatMember`](#unbanchatmember) |
335337

336338
Context shortcuts for **inline_query** update:
337339

@@ -552,7 +554,7 @@ require('https')
552554
Express.js example integration
553555

554556
```js
555-
const Telegraf = require('telegraf')
557+
const { Telegraf } = require('telegraf')
556558
const express = require('express')
557559
const expressApp = express()
558560

@@ -572,7 +574,7 @@ expressApp.listen(3000, () => {
572574
Fastify example integration
573575

574576
```js
575-
const Telegraf = require('telegraf')
577+
const { Telegraf } = require('telegraf')
576578
const fastifyApp = require('fastify')()
577579

578580
const bot = new Telegraf(process.env.BOT_TOKEN)
@@ -591,7 +593,7 @@ fastifyApp.listen(3000, () => {
591593
Koa.js example integration
592594

593595
```js
594-
const Telegraf = require('telegraf')
596+
const { Telegraf } = require('telegraf')
595597
const Koa = require('koa')
596598
const koaBody = require('koa-body')
597599

@@ -660,7 +662,7 @@ bot.on('message', (ctx) => {
660662
To enable Telegram Passport support you can use [`telegram-passport`](https://www.npmjs.com/package/telegram-passport) package:
661663

662664
```js
663-
const Telegraf = require('telegraf')
665+
const { Telegraf } = require('telegraf')
664666
const TelegramPassport = require('telegram-passport')
665667

666668
const bot = new Telegraf(process.env.BOT_TOKEN)
@@ -742,7 +744,7 @@ $ telegraf -t "bot token" bot.js
742744
Telegraf API reference
743745

744746
```js
745-
const Telegraf = require('telegraf')
747+
const { Telegraf } = require('telegraf')
746748
```
747749

748750
##### Constructor
@@ -2153,7 +2155,7 @@ Telegram markup helper, [see examples](https://github.com/telegraf/telegraf/tree
21532155
Simple scene-based control flow middleware.
21542156

21552157
```js
2156-
const Telegraf = require('telegraf')
2158+
const { Telegraf } = require('telegraf')
21572159
const session = require('telegraf/session')
21582160
const Stage = require('telegraf/stage')
21592161
const Scene = require('telegraf/scenes/base')

markup.js

Lines changed: 85 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -164,15 +164,91 @@ class Markup {
164164
}
165165

166166
static formatHTML (text = '', entities = []) {
167-
const chars = ['', ...text.split(''), ''].map(escapeHTMLChar)
168-
entities.forEach(entity => {
169-
const tag = getHTMLTag(entity)
170-
const openPos = entity.offset
171-
const closePos = entity.offset + entity.length + 1
172-
chars[openPos] += tag.open
173-
chars[closePos] = tag.close + chars[closePos]
174-
})
175-
return chars.join('')
167+
const chars = [...text]
168+
const available = [...entities]
169+
const opened = []
170+
const result = []
171+
for (let offset = 0; offset < chars.length; offset++) {
172+
while (true) {
173+
const index = available.findIndex((entity) => entity.offset === offset)
174+
if (index === -1) {
175+
break
176+
}
177+
const entity = available[index]
178+
switch (entity.type) {
179+
case 'bold':
180+
result.push('<b>')
181+
break
182+
case 'italic':
183+
result.push('<i>')
184+
break
185+
case 'code':
186+
result.push('<code>')
187+
break
188+
case 'pre':
189+
if (entity.language) {
190+
result.push(`<pre><code class="language-${entity.language}">`)
191+
} else {
192+
result.push('<pre>')
193+
}
194+
break
195+
case 'strikethrough':
196+
result.push('<s>')
197+
break
198+
case 'underline':
199+
result.push('<u>')
200+
break
201+
case 'text_mention':
202+
result.push(`<a href="tg://user?id=${entity.user.id}">`)
203+
break
204+
case 'text_link':
205+
result.push(`<a href="${entity.url}">`)
206+
break
207+
}
208+
opened.unshift(entity)
209+
available.splice(index, 1)
210+
}
211+
212+
result.push(chars[offset])
213+
214+
while (true) {
215+
const index = opened.findIndex((entity) => entity.offset + entity.length - 1 === offset)
216+
if (index === -1) {
217+
break
218+
}
219+
const entity = opened[index]
220+
switch (entity.type) {
221+
case 'bold':
222+
result.push('</b>')
223+
break
224+
case 'italic':
225+
result.push('</i>')
226+
break
227+
case 'code':
228+
result.push('</code>')
229+
break
230+
case 'pre':
231+
if (entity.language) {
232+
result.push('</code></pre>')
233+
} else {
234+
result.push('</pre>')
235+
}
236+
break
237+
case 'strikethrough':
238+
result.push('</s>')
239+
break
240+
case 'underline':
241+
result.push('</u>')
242+
break
243+
case 'text_mention':
244+
case 'text_link':
245+
result.push('</a>')
246+
break
247+
}
248+
opened.splice(index, 1)
249+
}
250+
}
251+
return result.join('')
176252
}
177253
}
178254

@@ -203,40 +279,4 @@ function buildKeyboard (buttons, options) {
203279
return result
204280
}
205281

206-
function escapeHTMLChar (c) {
207-
switch (c) {
208-
case '&': return '&amp;'
209-
case '"': return '&quot;'
210-
case '\'': return '&#39;'
211-
case '<': return '&lt;'
212-
default : return c
213-
}
214-
}
215-
216-
function tag (name, params) {
217-
return {
218-
open: params
219-
? `<${name} ${Object.entries(params).map(([key, value]) => `${key}="${value.replace(/[<&"]/g, escapeHTMLChar)}"`).join(' ')}>`
220-
: `<${name}>`,
221-
close: `</${name}>`
222-
}
223-
}
224-
225-
const HTMLTags = new Map([
226-
['bold', tag('b')],
227-
['italic', tag('i')],
228-
['code', tag('code')],
229-
['pre', tag('pre')],
230-
['strikethrough', tag('s')],
231-
['underline', tag('u')],
232-
['text_link', ({ url }) => tag('a', { href: url })],
233-
['text_mention', ({ user }) => tag('a', { href: `tg://user?id=${user.id}` })]
234-
])
235-
236-
function getHTMLTag (entity) {
237-
const tag = HTMLTags.get(entity.type || 'unknown')
238-
if (!tag) return { open: '', close: '' }
239-
return typeof tag === 'function' ? tag(entity) : tag
240-
}
241-
242282
module.exports = Markup

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "telegraf",
3-
"version": "3.37.0",
3+
"version": "3.38.0",
44
"description": "Modern Telegram Bot Framework",
55
"license": "MIT",
66
"author": "Vitaly Domnikov <[email protected]>",
@@ -53,6 +53,7 @@
5353
"eslint-plugin-promise": "^4.0.0",
5454
"eslint-plugin-standard": "^4.0.0",
5555
"husky": "^4.2.0",
56+
"prettier": "^2.0.5",
5657
"typescript": "^3.0.1"
5758
},
5859
"keywords": [

readme.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
![Telegraf](docs/header.png)
2-
[![Bot API Version](https://img.shields.io/badge/Bot%20API-v4.7-f36caf.svg?style=flat-square)](https://core.telegram.org/bots/api)
2+
[![Bot API Version](https://img.shields.io/badge/Bot%20API-v4.8-f36caf.svg?style=flat-square)](https://core.telegram.org/bots/api)
33
[![NPM Version](https://img.shields.io/npm/v/telegraf.svg?style=flat-square)](https://www.npmjs.com/package/telegraf)
44
[![node](https://img.shields.io/node/v/telegraf.svg?style=flat-square)](https://www.npmjs.com/package/telegraf)
55
[![Build Status](https://img.shields.io/travis/telegraf/telegraf.svg?branch=master&style=flat-square)](https://travis-ci.org/telegraf/telegraf)
@@ -14,7 +14,7 @@ These accounts serve as an interface for code running somewhere on your server.
1414

1515
### Features
1616

17-
- Full [Telegram Bot API 4.7](https://core.telegram.org/bots/api) support
17+
- Full [Telegram Bot API 4.8](https://core.telegram.org/bots/api) support
1818
- [Telegram Payment Platform](https://telegram.org/blog/payments)
1919
- [HTML5 Games](https://core.telegram.org/bots/api#games)
2020
- [Inline mode](https://core.telegram.org/bots/api#inline-mode)
@@ -41,7 +41,7 @@ $ yarn add telegraf
4141
### Examples
4242

4343
```js
44-
const Telegraf = require('telegraf')
44+
const { Telegraf } = require('telegraf')
4545

4646
const bot = new Telegraf(process.env.BOT_TOKEN)
4747
bot.start((ctx) => ctx.reply('Welcome!'))
@@ -52,7 +52,7 @@ bot.launch()
5252
```
5353

5454
```js
55-
const Telegraf = require('telegraf')
55+
const { Telegraf } = require('telegraf')
5656

5757
const bot = new Telegraf(process.env.BOT_TOKEN)
5858
bot.command('oldschool', (ctx) => ctx.reply('Hello'))

telegraf.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ module.exports = Object.assign(Telegraf, {
213213
Extra,
214214
Markup,
215215
Router,
216+
Telegraf,
216217
Telegram,
217218
Stage,
218219
BaseScene,

test/markup.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,34 @@ test('should generate nested multi markup', (t) => {
221221
])
222222
t.deepEqual(markup, '<s>strike<b>bold<u>under</u></b></s>')
223223
})
224+
225+
test.only('should generate nested multi markup 2', (t) => {
226+
const markup = Markup.formatHTML('×11 22 333× ×С123456× ×1 22 333×', [
227+
{
228+
offset: 1,
229+
length: 9,
230+
type: 'bold'
231+
},
232+
{
233+
offset: 1,
234+
length: 9,
235+
type: 'italic'
236+
},
237+
{
238+
offset: 12,
239+
length: 7,
240+
type: 'italic'
241+
},
242+
{
243+
offset: 19,
244+
length: 36,
245+
type: 'italic'
246+
},
247+
{
248+
offset: 19,
249+
length: 8,
250+
type: 'bold'
251+
}
252+
])
253+
t.deepEqual(markup, '×<b><i>11 22 333</i></b>× <i> ×С</i><i><b>123456× </b> ×1 22 333×</i>')
254+
})

0 commit comments

Comments
 (0)