All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.litongjava.tio.utils.telegram.TelegramBot Maven / Gradle / Ivy

There is a newer version: 3.7.3.v202400213-RELEASE
Show newest version
package com.litongjava.tio.utils.telegram;

import java.util.HashMap;
import java.util.Map;

import com.litongjava.tio.utils.http.Http;
import com.litongjava.tio.utils.http.ResponseVo;
import com.litongjava.tio.utils.json.JsonUtils;

public class TelegramBot {

  private String botToken;
  private String name;

  public TelegramBot(String botToken) {
    this.name = "main";
    this.botToken = botToken;
  }

  public TelegramBot(String name, String botToken) {
    this.name = name;
    this.botToken = botToken;
  }

  public String getName() {
    return name;
  }

  public ResponseVo sendMessage(String chatId, String message) {
    String urlString = "https://api.telegram.org/bot" + botToken + "/sendMessage";
    Map map = new HashMap<>();
    map.put("chat_id", chatId);
    map.put("text", message);
    String payload = JsonUtils.toJson(map);

    return Http.postJson(urlString, payload);
  }

  public TelegramBot withToken(String botToken) {
    this.botToken = botToken;
    return this;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy