Coder Social home page Coder Social logo

telegrambotapi's Introduction

TelegramBotAPI

Version 1.0

Docs

ALL methods on the official Telegram Bot API page

How to get updates from bot

There are 2 methods

  • Webhooks (Setup a callback server. Telegram will send request to our server)

  • Long polling (We will send periodic request to Telegram server)

  • For now only Long Polling can be used.

  • Complete the toString() method with json

Long Polling

bot.setUpdateListener(new UpdateEvent() {
  @Override
  public void onUpdate(Update update) {
    int hour = LocalDateTime.now().getHour();
    int minute = LocalDateTime.now().getMinute();
    int second = LocalDateTime.now().getSecond();

    String time = "[" + hour + ":" + minute + ":" + second + "]";

    System.out.println(time);
    System.out.println(update.toString());
  }
}, 1000, 100, 0, null);

Return a JSONObject

Send methods

ALL methods on the official Telegram Bot API page

TelegramBot bot = new TelegramBot("xxxxxxxxx:yzyzyzyzyzyzyzyzyz-yzyzyzyzyz");

First we need to create an instance of the bot using the unique token gived by Bot Father


Then we need to create the request with one of the available methods

SendMessage request = new SendMessage(chat_id, URLEncoder.encode("TEST message UTF-8", "UTF-8"));

So we must send it to the Telegram servers

Sync method:

bot.execute(request);

Async method:

bot.async_execute(request, new ExecuteEvent() {
    @Override
    public void onExecute(JSONObject result) {
        // NEXT
    }
});

The return of method execute is a JSONArray so we can read it

System.out.println( result.toString(4) );

telegrambotapi's People

Contributors

kratess 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.