2.0.5 • Published 4 years ago

telegram-free v2.0.5

Weekly downloads
1
License
ISC
Repository
-
Last release
4 years ago

Telegram - Free Mobile

This library has been developed for specific needs. It combines communication with Telegram and the free mobile API.

Configuration

  • Install the library
npm install --save telegram-free
  • Define the configuration variables
  1. Using a json file (example in config.json)
let communicationApi = new TelegramFreeMobile("/home/user/config.json");
  1. Using an object
let config = {
    free_mobile: [
        {
            user: 123,
            password: "xxxxx"
        }
    ],
    telegram: {
        group_id: 123456,
        bot_token: "xxxxxxxxxx"
    }
};

let communicationApi = new TelegramFreeMobile(config);

Usage

Free Mobile

Send a message

let free = communicationApi.freeMobile;
free.sendMessage("Hello World!")
    .then(()=> {
        console.log("Sent to all specified users!");
    })
    .catch((err: Error | null) => {
        console.log("Error...", err);
    });

Telegram

Send a message

let telegram = communicationApi.telegram;
telegram.sendMessage("Hello World!")
.then((message) => {
    console.log("Message sent!");
});

Send a message with a picture

let telegram = communicationApi.telegram;
telegram.sendMessageWithPicture("Hello World!", "/home/user/image.png")
.then((message) => {
    console.log("Photo sent!");
});

Launch Telegram Bot and set OnMessageListener

let telegram = communicationApi.telegram;
telegram.launchTelegramServer()
    .setOnMessageListener((message) => {
        console.log("Message received:", message);
    });

API

Class TelegramFreeMobile

TelegramFreeMobile(config)

  • config - (ConfigurationInterface or string) The configuration variables. If config is a string, then it should be the absolute path of the json configuration file (example in config.json). If config is an object, it should implements ConfigurationInterface. Default: config.json of this current directory.

telegram(): Telegram

Returns an instance of Telegram.

freeMobile(): FreeMobile

Returns an instance of FreeMobile.

configuration(): Configuration

Returns an instance of Configuration.

Class Telegram

sendMessage(content): Promise<TelegramBot.Message>

  • content - (string) Message to be sent.

sendMessageWithPicture(content, picture): Promise<TelegramBot.Message>

  • content - (string) Message to be sent.
  • picture - (string) Absolute path or public URL of the picture to be sent.

sendMessageWithVideo(content, video): Promise<TelegramBotDefinition.Message>

  • content - (string) Message to be sent.
  • video - (string) Absolute path or public URL of the video to be sent.

launchTelegramServer(): Telegram

Launches the telegram bot server. Required to use listeners.

setOnMessageListener(onMessageReceived): void

  • onMessageReceived - ((message: TelegramBot.Message) => void) Callback called each time a message is received.

bot: TelegramBot

Returns an instance of TelegramBot from node-telegram-bot-api library.

Class FreeMobile

sendMessage(content): Promise<void[]>

  • content - (string) Message to be sent.

Interface ConfigurationInterface

  • telegram - (TelegramConfigurationInterface) Telegram group configuration. The bot will listen for messages into the group specified.
  • freemobile - (_Array\) Free mobile configuration. The FreeMobile class will send messages to all specified users.

Interface TelegramConfigurationInterface

  • bottoken - (_string) Token of the telegram bot.
  • groupid - (_number) Group ID. The bot will listen for messages into the group specified.

Interface FreeMobileConfigurationInterface

  • user - (number) Account number of the free mobile user.
  • password - (string) Token of the free mobile user.