21.13.8 • Published 4 years ago

@darkwolf/telegram-bot v21.13.8

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

Telegram Bot API

Install

npm i --save @darkwolf/telegram-bot

Using

import TelegramBot, {
  types,
  helper,
  errors,
  constants
} from '@darkwolf/telegram-bot'
const {
  InlineKeyboardMarkup,
  InlineKeyboardButton,
  InlineKeyboardPagination,
  InlineKeyboardCursorPagination,
  InputFile,
  InputMediaPhoto,
  MarkdownV2Text
} = types

const bot = new TelegramBot(token)
// Send message
// You can use both constructor and chain builder
const markup = new InlineKeyboardMarkup([
  [new InlineKeyboardButton('Ave', {callbackData: 'ave'})],
])
  .addButton(
    new InlineKeyboardButton()
      .setText('Darkwolf')
      .setCallbackData('darkwolf')
  )
  // Custom smart pagination
  .addPagination(
    new InlineKeyboardPagination({
      pageCount: dataConnection.pageInfo.pageCount, // 100
      pageNumber: dataConnection.pageInfo.pageNumber, // 69
      pageStep, // 1
      maxPageButtons: 7,
      onPageButtonCreate: (button, index, paginator, pagination) => button.setCallbackData(`data:page:${button.pageNumber}:step:${pagination.pageStep}`)
    })
    // ['• 1 •', '5', '10', '25']
    // ['«', '1...', '68', '• 69 •', '70', '...100', '»']
  )
  .addPagination(
    new InlineKeyboardPagination()
      .setMode('cursor')
      .setPageCount(dataConnection.pageInfo.pageCount)
      .setPageNumber(dataConnection.pageInfo.pageNumber)
      .setShowStepper(false)
      .onPageButtonCreate(button => button.setCallbackData(`data:page:${button.pageNumber}`))
    // ['« 1', '‹ 68', '• 69 •', '70 ›', '100 »']
  )
  // Cursor pagination
  .addPagination(
    new InlineKeyboardCursorPagination()
      .setHasPreviousPage(dataConnection.pageInfo.hasPreviousPage)
      .setHasNextPage(dataConnection.pageInfo.hasNextPage)
      .onPreviousPageButtonCreate(button => button.setCallbackData(`data:${dataConnection.pageInfo.startCursor}`))
      .onNextPageButtonCreate(button => button.setCallbackData(`data:${dataConnection.pageInfo.endCursor}`))
    // ['«', '»']
  )
const message = await bot.sendMessage(chatId, new MarkdownV2Text()
  .addTitle('Title')
  .addItem('ID', 1)
  .addItem('First Name', 'Pavel')
  .addItem('Last Name', 'Wolf')
  .addItem('Username', '@PavelWolfDark')
  .addText('Underscore line:')
  .addNewLine()
  .addUnderscoreLine()
  .addBoldText('Bold text')
  .addNewLine()
  .addItalicText('Italic text')
  .addNewLine()
  .addUnderlinedText('Underlined text')
  .addNewLine()
  .addUnderlinedItalicText('Underlined italic text')
  .addNewLine()
  .addStrikethroughText('Strikethrough text')
  .addNewLine()
  .addText(
    new MarkdownV2Text('Bold italic underlined strikethrough text')
      .makeBold()
      .makeUnderlineItalic()
      .makeStrikethrough()
  )
  .addNewLine()
  .addText('Dash line:')
  .addNewLine()
  .addDashLine()
  .addText('Inline code: ')
  .addInlineCode('Inline code')
  .addNewLine()
  .addCodeBlock('Code block')
  .addNewLine()
  .addCodeBlock('JavaScript code block', {language: 'javascript'})
  .addNewLine()
  .addText('Deep text link: ')
  .addTextLink('Pavel Wolf', 't.me/PavelWolfDark')
  .addNewLine()
  .addText('Escaped text: ')
  .addEscapedText('Ave, Darkwolf!')
  .addNewLine()
  .addText('Bullet line:')
  .addNewLine()
  .addBulletLine()
  .addText('List:')
  .addNewLine()
  .addList([
    'First',
    'Second',
    'Third',
    [
      'First',
      [
        'First'
      ]
    ]
  ])
  .addNewLine()
  .addText('Ordered list:')
  .addNewLine()
  .addOrderedList([
    'First',
    'Second',
    'Third',
    [
      'First',
      [
        'First'
      ]
    ]
  ]), {
  parseMode: constants.ParseMode.MARKDOWN_V2,
  replyMarkup: markup
})
// Send photo
const photo = new InputFile(fs.createReadStream('photo.jpeg'))
const message = await bot.sendPhoto(chatId, photo, {
  caption: 'Ave, Darkwolf!',
  parseMode: constants.ParseMode.MARKDOWN,
  replyMarkup: markup
})
// Upload photos
const messages = await bot.sendMediaGroup(chatId, [
  new InputMediaPhoto('attach://photo1'),
  new InputMediaPhoto()
    .setMedia('attach://photo2')
], {
  attachedFiles: [
    new InputFile(fs.createReadStream('photo1.jpeg'))
      .setAttachName('photo1'),
    new InputFile(fs.createReadStream('photo2.jpeg'))
      .setAttachName('photo2')
  ]
})
// You can also download uploaded photos from Telegram
const blobs = await Promise.all(
  messages.map(message => {
    const photo = message.photo[0]
    return photo.download()
  })
)

API Documentation

Donate

You can contribute to the development of open source projects by making your donation

Bitcoin (BTC): 15sjjAUtJdB1ncsxKK7KtyJPtF46UhXWo4

Ethereum (ETH): 0xF02F68eb33E9eC73b8E9c3c0953E6782E2376867

Bitcoin Cash (BCH): qq6h3a78h3wlt04eyp9ydht94r6guzjdps9hqwgfzw

Contact Me

GitHub: @PavelWolfDark

Telegram: @PavelWolfDark

Email: PavelWolfDark@gmail.com

21.13.8

4 years ago

21.13.7

4 years ago

21.13.6

4 years ago

21.13.5

4 years ago

21.13.4

4 years ago

21.13.3

4 years ago

21.13.2

4 years ago

21.13.1

4 years ago

21.13.0

4 years ago

13.22.4

4 years ago

13.22.3

4 years ago

13.22.2

4 years ago

13.22.1

4 years ago

13.22.0

4 years ago

13.21.86

4 years ago

13.21.84

4 years ago

13.21.85

4 years ago

13.21.83

4 years ago

13.21.82

4 years ago

13.21.81

4 years ago

13.21.80

4 years ago

13.21.79

4 years ago

13.21.78

4 years ago

13.21.77

4 years ago

13.21.76

4 years ago

13.21.75

4 years ago

13.21.74

4 years ago

13.21.73

4 years ago

13.21.72

4 years ago

13.21.71

4 years ago

13.21.70

4 years ago

13.21.69

4 years ago

6.6.6

4 years ago

4.0.0

4 years ago

3.0.7

4 years ago

3.0.6

4 years ago

3.0.5

5 years ago

3.0.4

5 years ago

3.0.3

5 years ago

3.0.2

5 years ago

3.0.1

5 years ago

3.0.0

5 years ago

2.2.0

5 years ago

2.1.9

5 years ago

2.1.8

5 years ago

2.1.7

5 years ago

2.1.6

5 years ago

2.1.5

5 years ago

2.1.4

5 years ago

2.1.3

5 years ago

2.1.2

5 years ago

2.1.1

5 years ago

2.1.0

5 years ago

2.0.9

5 years ago

2.0.8

5 years ago

2.0.7

5 years ago

2.0.6

5 years ago

2.0.5

5 years ago

2.0.4

5 years ago

2.0.3

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago