1.6.0 • Published 3 years ago

z-telbot v1.6.0

Weekly downloads
10
License
ISC
Repository
github
Last release
3 years ago

z-telbot

Client for telegram bots

Installation

npm i z-telbot

Usage

Javascript

const { ZTelBot } = require('z-telbot');

Typescript

import { ZTelBot } from 'z-telbot';

Example

const telBot = new ZTelBot({ token: getApiToken() });

telBot.addTextMessageListener(evt => {
  console.log(`[${evt.chat.name} (ID: ${evt.chat.id})] ${evt.from?.name}(ID: ${evt.from?.id}): ${evt.message?.text}`);
});

telBot.addCommandListener('hi', evt => {
  evt.reply().text({ text: `Hi: ${evt.command.argsText}` });
});

telBot.addDefaultCommandListener(evt => {
  if (!evt.commandFound) {
    evt.reply().text({ text: `Comando inválido: ${evt.command.commandName}` });
  }
});

telBot.listenUpdates().then((botInfo) => {
  console.log(`Bot iniciado com sucesso: ${botInfo.username}`);
});

Summary

constructor getMe getUpdates sendMessage listenUpdates addUpdateListener addMessageListener addSentMessageListener addTextMessageListener addDefaultCommandListener addCommandListener removeUpdateListener removeSentMessageListener removeMessageListener removeTextMessageListener removeDefaultCommandListener removeCommandListener sendCommands setMyCommands getMyCommands

constructor

const telBot = new ZTelBot({token: '...'});

Parameters:

{
  token!: string;
}

getMe

const botInfo = await telBot.getMe();
console.log(botInfo);

getUpdates

const updates = await telBot.getUpdates();
console.log(updates);

Parameters:

{
  offset?: number;
  limit?: number;
  timeout?: number;
  allowedUpdates?: string[];
}

sendMessage

const messageInfo = telBot.sendMessage({text: 'Oi', chatId: 1234, parseMode: 'MarkdownV2'});
console.log(messageInfo);

Parameters:

{
  chatId!: number | string;
  text!: string;
  parseMode?: ParseMode;
  entities?: MessageEntity[];
  disableWebPagePreview?: boolean;
  disableNotification?: boolean;
  replyToMessageId?: number;
  allowSendingWithoutReply?: boolean;
  replyMarkup?: InlineKeyboardMarkup | ReplyKeyboardMarkup | ReplyKeyboardRemove | ForceReply;
}

listenUpdates

telBot.listenUpdates().then((botInfo) => {
  console.log(`Bot iniciado com sucesso: ${botInfo.username}`);
});

Parameters:

{
  once?: boolean;
  offset?: number;
  limit?: number;
  timeout?: number;
  allowedUpdates?: string[];
  skipFirstBatch?: boolean;
  delay?: number;
}

addUpdateListener

telBot.addUpdateListener((evt)=>{
  console.log(evt);
});

addMessageListener

telBot.addMessageListener((evt)=>{
  console.log(evt);
});

addSentMessageListener

telBot.addSentMessageListener((evt)=>{
  console.log(evt);
});

addTextMessageListener

telBot.addSentMessageListener((evt)=>{
  console.log(evt.text);
});

addDefaultCommandListener

telBot.addCommandListener('hi', (evt)=>{
  evt.reply().text({text: 'Hi'});
});

telBot.addDefaultCommandListener((evt)=>{
  if (!evt.commandFound){
    evt.reply().text({text: `Invalid command: ${evt.command.commandName}`});
  }
});

addCommandListener

telBot.addCommandListener('hi', (evt)=>{
  evt.reply().text({text: 'Hi'});
});
const cmd = {command: 'hi', description: 'Send hi'};
telBot.addCommandListener(cmd, (evt)=>{
  evt.reply().text({text: 'Hi'});
});

Parameters:

{
  command!: string;
  hidden?: boolean;
  description?: string;
}

sendCommands

telBot.addCommandListener('test', (evt)=>{
  evt.reply().text({text: 'Test'});
}

const cmd = {command: 'hi', description: 'Send hi'};
telBot.addCommandListener(cmd, (evt)=>{
  evt.reply().text({text: 'Hi'});
});

telBot.sendCommands();
telBot.addCommandListener('hi', (evt)=>{
  evt.reply().text({text: 'Hi'});
});

telBot.addDefaultCommandListener((evt)=>{
  if (evt.command.commandName==='test'){
    evt.reply().text({text: 'Test'});
  }
});

telBot.sendCommands([{command: 'test', description: 'Test'}]);

removeListener

const listenerId = telBot.addCommandListener('test', (evt)=>{
  evt.reply().text({text: 'Test'});
});
telBot.removeCommandListener(listenerId);

getMyCommands

const commands: BotCommand[] = await telBot.getMyCommands();
console.log(commands);

setMyCommands

telBot.setMyCommands([{
  command: 'hi'
  description: 'Send hi'
}]);
1.5.5

3 years ago

1.5.4

3 years ago

1.5.3

3 years ago

1.6.0

3 years ago

1.5.10

3 years ago

1.5.11

3 years ago

1.5.9

3 years ago

1.5.8

3 years ago

1.5.7

3 years ago

1.5.2

3 years ago

1.5.1

4 years ago

1.5.0

4 years ago

1.3.0

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago