Telegram Bot for Android

Hi, Friends. In this post we will see how to integrate Telegram Bot in Android. It is a light weight library used to manage your Telegram Bot in your Android application. We can send Messages from our own Android Application to Telegram Users.

Project Setup

Open app level build.gradle file and add the following dependency.
compile 'com.ajts.library.telegrambot:telegrambotlibrary:1.0.0'

Coding Part

Open your Activity file (e.g.,MainActivity.java) and initialize telegram bot library with your bot token.
Telegram telegram = new Telegram("<bot-token>");

I have used the services offered by this library.

Authorizing Telegram Bot

It is used to Authorize or Get the details of your bot

telegram.getMe(new TelegramCallback<GetMe>() {
    @Override
    public void onResponse(Call call, final GetMe getMe) {
        Log.v("response.body()", getMe.isOk() + "");
    }

    @Override
    public void onFailure(Call call, Exception e) {

    }
});

We can send Text Messages to telegram as well as with Markdown or HTML

Send Text Message

telegram.sendMessage("<Channel-Name or Chat-ID>", "TelegramBotLibrary", new TelegramCallback<Message>() {
    @Override
    public void onResponse(Call call, Message response) {
        Log.v("response.body()", response.isOk() + "");
    }

    @Override
    public void onFailure(Call call, Exception e) {

    }
});

Send Text Message with Html and Markdown

Html

telegram.sendMessage("<Channel-Name or Chat-ID>",
    "<i>TelegramBotLibrary</i>",
    Telegram.ParseMode.HTML,
    new TelegramCallback<Message>() {
        @Override
        public void onResponse(Call call, Message response) {
            Toast.makeText(MainActivity.this, response.isOk() + "", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFailure(Call call, Exception e) {

        }
    });

Markdown

telegram.sendMessage("<Channel-Name or Chat-ID>",
    "*TelegramBotLibrary*",
    Telegram.ParseMode.Markdown,
    new TelegramCallback<Message>() {
        @Override
        public void onResponse(Call call, Message response) {
            Toast.makeText(MainActivity.this, response.isOk() + "", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFailure(Call call, Exception e) {

        }
    });
We can send Media files to telegram as well.

Send Photo

telegram.sendPhoto("<Channel-Name or Chat-ID>",
    TelegramMediaType.Image.png,
    new File(imagePickedPath),
    "telegram photo",
    new TelegramCallback<Message>() {
        @Override
        public void onResponse(Call call, Message response) {
            Toast.makeText(MainActivity.this, response.isOk() + "", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFailure(Call call, Exception e) {

        }
    });

Send Video

telegram.sendVideo("<Channel-Name or Chat-ID>",
    TelegramMediaType.Video.mp4,
    new File(videoPickedPath),
    "telegram video",
    new TelegramCallback<Message>() {
        @Override
        public void onResponse(Call call, Message response) {
            Toast.makeText(MainActivity.this, response.isOk() + "", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFailure(Call call, Exception e) {

        }
    });

Send Audio

telegram.sendAudio("<Channel-Name or Chat-ID>",
    TelegramMediaType.Audio.mp3,
    new File(audioPickedPath),
    "telegram audio",
    new TelegramCallback<Message>() {
        @Override
        public void onResponse(Call call, Message response) {
            Toast.makeText(MainActivity.this, response.isOk() + "", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFailure(Call call, Exception e) {

        }
    });

Send Document

telegram.sendDocument("<Channel-Name or Chat-ID>",
    TelegramMediaType.Document.file,
    new File(documentPickedPath),
    "telegram document",
    new TelegramCallback<Message>() {
        @Override
        public void onResponse(Call call, Message response) {
            Toast.makeText(MainActivity.this, response.isOk() + "", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFailure(Call call, Exception e) {

        }
    });

Send Location

telegram.sendLocation("<Channel-Name or Chat-ID>",
    "<Latitude>",
    "<Longitude>",
    new TelegramCallback<Message>() {
        @Override
        public void onResponse(Call call, Message response) {
            Toast.makeText(MainActivity.this, response.isOk() + "", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFailure(Call call, Exception e) {

        }
    });

Send Venue

telegram.sendVenue("<Channel-Name or Chat-ID>",
    "<Latitude>",
    "<Longitude>",
    "<Address>",
    "<Country>",
    new TelegramCallback<Message>() {
        @Override
        public void onResponse(Call call, Message response) {
            Toast.makeText(MainActivity.this, response.isOk() + "", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFailure(Call call, Exception e) {

        }
    });

Download Code

You can the download code for this post from Github. If you like this, tutorial star it on Github.

Share this

Related Posts

Previous
Next Post »