GithubHelp home page GithubHelp logo

ahtoh874 / teleapi Goto Github PK

View Code? Open in Web Editor NEW

This project forked from telekits/teleapi

0.0 1.0 0.0 95 KB

๐Ÿš€ The useful library to simplify your work with Telegram Bot API

Home Page: https://www.npmjs.com/package/teleapi

License: MIT License

JavaScript 100.00%

teleapi's Introduction

Header image

telekit โ€ƒโ€ขโ€ƒ teleapi

Build Status npm npm Airbnb code style license

Now support the Telegram Bot API 3.5 with the new albums feature.

What is it?

This is useful library to simplify your work with the Telegram Bot API

Before you start, please, read an introduction for developers.
Just a moment, I'll read it.

Install

npm

$ npm install teleapi --save

yarn

$ yarn add teleapi

How it use?

Elementary! Look at the next example that show you how to send request to getMe method:

/** First be we've required the teleapi */
const teleapi = require('teleapi');

/**
 * Next we need a get an instance of the teleapi.
 * Also, we should set the bot token.
 */
const api = teleapi('telegram_bot_token');

/**
 * And now we can send requests to the Telegram Bot API;
 * This method returns info about the bot to us.
 *
 * (`getMe` method is available in the Telegram Bot API).
 */
api.getMe().then((response) => {
    /** Bot ID */
    console.log('id:', response.id);
    /** and username of the our bot */
    console.log('username:', response.username);

    /**
     * And also first name and last name and lang code.
     * See about it below.
     */
}).catch((error) => {
    /** Something is wrong! */
    console.log(error);
});

If you want to see available methods and types:
Please, have a look at here first.

API

teleapi(token, [api]);

  • token:String - Token of the Bot that you can get from the BotFather
  • api:Object - (optional) An Object with Custom API(see api.json)
  • Returns: api - An instance of the teleapi.

Creates an new instance of the teleapi with your token of the bot.

teleapi.version:String

A contain of the current API version.

teleapi.methods:Array

A contain an Array of String with all available methods.

api.getFile(id);

  • id:String - File ID
  • Returns: Stream - A stream with file data

Get file from the Telegram.

api.method(name, [params]);

  • name:String - Name of the method that available in the Telegram Bot API
  • params:Object - (optional) An Object with body params for the request
  • Returns: Promise with response in then

This method send request to the Telegram Bot API;
It's private method but you can use it.

api.<method>(params);

  • params:Object - An Object with body params for the request
  • Returns: Promise with response in then

The <method> is one of the available methods from Telegram Bot API.

See all available methods here.

Examples

Send text message to chat

const teleapi = require('teleapi');

const api = teleapi('telegram_bot_token');

/** Send request with chat and text message */
api.sendMessage({
    chat_id: 0000,
    text: 'Hello!', 
});

Save file from the Telegram

const teleapi = require('teleapi');

const api = teleapi('telegram_bot_token');

/** Send request with file id */
api.getFile('file_id').pipe(fs.createWriteStream('image.png'));

Send sticker(file_id)

const teleapi = require('teleapi');

const api = teleapi('telegram_bot_token');

/** Send request where `sticker` is a file id */
api.sendSticker({
   chat_id: 0000,
   sticker: 'file_id', 
});

Send document(stream.Readable)

const teleapi = require('teleapi');
const fs = require('fs');

const api = teleapi('telegram_bot_token');

/** Send request where `document` is stream.Readable */
api.sendDocument({
   chat_id: 0000,
   document: fs.readFile('book_from_tpb.pdf'), 
});

Send photo(URL)

const teleapi = require('teleapi');

const api = teleapi('telegram_bot_token');

/** Send request where `photo` is an URL */
api.sendPhoto({
   chat_id: 0000,
   photo: 'https://upload.wikimedia.org/wikipedia/commons/d/d9/Test.png', 
});

Send voice(Buffer)

const teleapi = require('teleapi');

const api = teleapi('telegram_bot_token');

/** Send request where `voice` is a Buffer */
api.sendVoice({
   chat_id: 0000,
   voice: new Buffer([1, 2, 3]), 
});

Send document(strict)

const teleapi = require('teleapi');
const fs = require('fs');

const api = teleapi('telegram_bot_token');

/** Send request where 'document' is an Object with strict data */
api.sendDocument({
    chat_id: 000000,
    document: {
        filename: 'photo.png',
        value: fs.readFile('family_photo.dat'),
        mime: 'image/png',
    },
});

A more is coming soon.

LICENSE

MIT ยฉ Denis Maslennikov

teleapi's People

Contributors

nof1000 avatar pinkinice avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.