GithubHelp home page GithubHelp logo

fjmontesinos / java-telegram-bot-api Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pengrad/java-telegram-bot-api

0.0 2.0 0.0 225 KB

Simple Java API for Telegram Bots

Home Page: https://core.telegram.org/bots

License: Apache License 2.0

Java 100.00%

java-telegram-bot-api's Introduction

Simple Java API for Telegram Bots

Full support of all Bot API functions

Download

Download the latest version via Gradle:

compile 'com.github.pengrad:java-telegram-bot-api:1.3.2'

or Maven:

<dependency>
  <groupId>com.github.pengrad</groupId>
  <artifactId>java-telegram-bot-api</artifactId>
  <version>1.3.2</version>
</dependency>

Usage

Create bot

TelegramBot bot = TelegramBotAdapter.build("BOT_TOKEN");

All bot methods have the same signature as original ones.
You can pass null as any Optional parameter

Send message

// short syntax
bot.sendMessage(chatId, "short message sending");
bot.sendMessage("@mychannel", "short message sending");

// full
bot.sendMessage(
    chatId,                    // chat_id
    "Hello _italic_ *bold*!",  // text
    ParseMode.Markdown,        // Markdown text or null
    false,                     // disable_web_page_preview
    replyMessageId,            // reply_to_message_id
    new ReplyKeyboardMarkup(new String[]{"ok", "cancel"}).oneTimeKeyboard(true));  // keyboard

Keyboards

Keyboard replyKeyboardMarkup = new ReplyKeyboardMarkup(
                new String[]{"first row button1", "first row button2"},
                new String[]{"second row button1", "second row button2"})
                .oneTimeKeyboard(true)   // optional
                .resizeKeyboard(true)    // optional
                .selective(true);        // optional
                
Keyboard forceReply = new ForceReply(isSelective); // or just new ForceReply();
Keyboard replyKeyboardHide = new ReplyKeyboardHide(); // new ReplyKeyboardHide(isSelective)

Getting response to sending methods

SendResponse sendResponse = bot.sendMessage(chatId, "short message sending");
Message message = sendResponse.message();

Send files

3 options to sending files

// as String, resending existing file
String fileId;

// as File
InputFile.photo(file);
InputFile.audio(file);
InputFile.video(file);
InputFile.voice(file);
new InputFile("text/plain", file); 

// as byte[]
InputFileBytes.photo(bytes);
InputFileBytes.audio(bytes);
InputFileBytes.video(bytes);
InputFileBytes.voice(bytes);
new InputFileBytes("text/plain", bytes, "my-file.txt");

Examples

// Photo
String fileId = // resending fileId
bot.sendPhoto(chatId, fileId, "caption", null, null);
bot.sendPhoto(chatId, InputFile.photo(imageFile), "caption", replyMessageId, new ForceReply());
bot.sendPhoto(chatId, InputFileBytes.photo(bytes), "caption", null, new ReplyKeyboardHide());

// Same options for all types 
// Audio
bot.sendAudio(chatId, InputFile.audio(audioFile), duration, performer, title, null, null);

// Video
bot.sendVideo(chatId, InputFile.video(videoFile), duration, "caption", null, null);

// Document
bot.sendDocument(chatId, new InputFile("text/plain", docFile), null, null);

// Sticker
bot.sendSticker(chatId, stickerId, null, null);

// Voice
bot.sendVoice(chatId, InputFileBytes.voice(bytes), duration, null, null);

Send chat action

bot.sendChatAction(chatId, ChatAction.find_location);
bot.sendChatAction(chatId, ChatAction.typing);
bot.sendChatAction(chatId, ChatAction.record_audio);
bot.sendChatAction(chatId, ChatAction.record_video);
bot.sendChatAction("@channel", ChatAction.upload_audio);
bot.sendChatAction("@channel", ChatAction.upload_document);
bot.sendChatAction("@channel", ChatAction.upload_photo);
bot.sendChatAction("@channel", ChatAction.upload_video);

Get updates

GetUpdatesResponse updatesResponse = bot.getUpdates(offset, limit, timeout);
List<Update> updates = updatesResponse.updates();
...
Message message = update.message()

If using webhook, you can parse request to Message

Update update = BotUtils.parseUpdate(stringRequest); // from String
Update update = BotUtils.parseUpdate(reader); // from java.io.Reader
Message message = update.message();

Get file

GetFileResponse getFileResponse = bot.getFile("fileId");
File file = getFileResponse.file(); // com.pengrad.telegrambot.model.File
file.fileId();
file.filePath();  // relative path
file.fileSize();

To get downloading link as https://api.telegram.org/file/bot<token>/<file_path>

String fullPath = bot.getFullFilePath("fileId");
String fullPath = bot.getFullFilePath(file);  // com.pengrad.telegrambot.model.File

Inline mode

Getting updates

GetUpdatesResponse updatesResponse = bot.getUpdates(offset, limit, timeout);
List<Update> updates = updatesResponse.updates();
...
InlineQuery inlineQuery = update.inlineQuery();

If using webhook, you can parse request to InlineQuery

Update update = BotUtils.parseUpdate(stringRequest); // from String
Update update = BotUtils.parseUpdate(reader); // from java.io.Reader
InlineQuery inlineQuery = update.inlineQuery();

Build InlineQueryResult

InlineQueryResult r1 = new InlineQueryResultPhoto("id", "photoUrl", "thumbUrl");
InlineQueryResult r2 = new InlineQueryResultArticle("id", "title", "message text").thumbUrl("url");
InlineQueryResult r3 = new InlineQueryResultGif("id", "gifUrl", "thumbUrl");
InlineQueryResult r4 = new InlineQueryResultMpeg4Gif("id", "mpeg4Url", "thumbUrl");
InlineQueryResult r5 = new InlineQueryResultVideo("id", "videoUrl", InlineQueryResultVideo.MIME_VIDEO_MP4, "message text", "thumbUrl", "video title");

Send query

bot.answerInlineQuery(inlineQuery.id(), r1, r2, r3, r4, r5);
// or full
bot.answerInlineQuery(inlineQuery.id(), new InlineQueryResult[]{r1, r2, r3, r4, r5}, cacheTime, isPersonal, nextOffset);

java-telegram-bot-api's People

Contributors

pengrad avatar

Watchers

James Cloos avatar Javier Montesinos 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.