-
Notifications
You must be signed in to change notification settings - Fork 21
Getting Started
Telegram API tries to be both easy to use and compatible with the API.
To accomplish compatibility with the API, we let you pass options with the same name as defined by the official Telegram API, and in order to make it easy to use, we provide chainable methods to configure methods.
You have access to raw API methods, which send your request to Telegram API server (they acccept an options object), as well as Message Types which allow you to use the methods in an easier, and maintanable way.
To get updates from the server, we use polling. I have added partial support for webhooks, but I have not tested it yet, any help in this section is appreciated. 👍
Whatever method you use to receive updates, you can listen on RegExp matched messages, commands, as well as raw Update responses.
Okay, let's write a super simple Telegram Bot, this bot will answer our greetings, that's all.
I use ES6 for examples, I love ES6. ❤️
import Bot from 'telegram-api';
import Message from 'telegram-api/types/Message';
let bot = new Bot({
token: 'YOUR_TOKEN'
});
bot.start();
bot.get(/Hi|Hey|Hello|Yo/i, message => {
let answer = new Message().text('Hello, Sir').to(message.chat.id);
bot.send(answer);
})
Let's explain the code, it's pretty simple.
-
First of all, you have to import the message types you want to use, as you see, here I'm importing
Message
, which sends a simple message out. -
Then I create a bot instance, with my token.
-
By calling
bot.start()
the bot starts polling updates from the server. -
Then I simply listen on messages which pass the RegExp test, when I get such message, I create a new
Message
instance called answer, set it's target to the chat the original message came from, and give it a text. -
I give my message to bot and ask him to send it.
That's it, try it yourself, it's cool. 😎