1.0.1 • Published 2 years ago

@puregram/utils v1.0.1

Weekly downloads
1
License
MIT
Repository
github
Last release
2 years ago

@puregram/utils

Package, containing some useful utilities for puregram package

Introduction

This package exists only for one main reason: basically not everyone wants to have such utils as getCasinoValues and <insert one more here> when installing puregram, so these functions were moved to separated package

Example

const { Telegram } = require('puregram');
const { getCasinoValues } = require('@puregram/utils');

const telegram = new Telegram({
  token: process.env.TOKEN
});

telegram.updates.on('message', (context) => {
  if (context.hasDice && context.dice.emoji === '🎰') {
    console.log(getCasinoValues(context.dice.value)); // e.g. ['seven', 'bar', 'grapes']
  }
});

telegram.updates.startPolling();

Installation

$ yarn add @puregram/utils
$ npm i -S @puregram/utils

List of utilities

WebApp

generateSecretKey(token: string)

Returns: Promise<WebAppGenerateSecretKeyResult>

Returns secret key generated by the token

const { key } = await WebApp.generateSecretKey(BOT_TOKEN)

parseInitData(initData: string)

Returns: string

Returns parsed init data

const data = WebApp.parseInitData(initData)

generateInitDataHash(initData: string, key: Buffer)

Returns: Promise<WebAppGenerateInitDataHashResult>

Returns init data hash generated by the key

const { hash } = await WebApp.generateInitDataHash(initData, secretKey)

validate(params: WebAppValidateInitDataParams)

Returns: Promise<WebAppValidateResult>

Returns whether params.initData is valid or not

const { valid } = await WebApp.validate({ initData, key: secretKey })

getCasinoValues(value: number | string)

Returns: SlotMachineValue

Returns an array of CasinoValue detected by value in the dice

if (context.hasDice && context.dice.emoji === '🎰') {
  return context.send(`You've got ${getCasinoValues(context.dice.value).join(', ')}!`)
}