erainformatica.tbjhelper.helpers.TelegramBotReceiver.java Source code

Java tutorial

Introduction

Here is the source code for erainformatica.tbjhelper.helpers.TelegramBotReceiver.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package erainformatica.tbjhelper.helpers;

import erainformatica.tbjhelper.classes.TelegramRequestResult;
import erainformatica.tbjhelper.classes.Update;
import erainformatica.tbjhelper.protocols.BotUpdatesCallbackProtocol;
import erainformatica.utility.JSONHelper;
import erainformatica.utility.Utility;
import java.util.HashMap;
import org.json.JSONObject;

/**
 *
 * @author robertodedomenico
 */
public abstract class TelegramBotReceiver implements Runnable, BotUpdatesCallbackProtocol {
    private Integer offset;
    private Integer timeout;
    private Integer limit;
    private String api_key;

    public TelegramBotReceiver(String api_key) {
        this.api_key = api_key;
        this.offset = null;
    }

    public TelegramBotReceiver(String api_key, Integer timeout) {
        this.api_key = api_key;
        this.timeout = timeout;
    }

    public Integer getOffset() {
        return offset;
    }

    public void setOffsetInteger(Integer offset) {
        this.offset = offset;
    }

    public Integer getTimeout() {
        return timeout;
    }

    public void setTimeout(Integer timeout) {
        this.timeout = timeout;
    }

    public Integer getLimit() {
        return limit;
    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    public String getApi_key() {
        return api_key;
    }

    public void setApi_key(String api_key) {
        this.api_key = api_key;
    }

    // METHODS

    @Override
    public void run() {
        HashMap parameters = new HashMap();
        while (true) {
            parameters.clear();
            if (offset != null)
                parameters.put("offset", offset.toString());
            if (timeout != null)
                parameters.put("timeout", timeout.toString());
            String url = Utility.buildAPIUrl(this.api_key, TelegramBot.API_OPERATION_GET_UPDATES, parameters);
            TelegramRequestResult<JSONObject> result = JSONHelper.readJsonFromUrl(url);
            if (result.isRequestOk()) {
                Update[] updates;
                JSONObject r = result.get();
                if (r.getBoolean("ok") == true) {
                    try {
                        updates = (Update[]) Utility.castJsonToObject(r.getJSONArray("result").toString(),
                                TelegramBot.TYPE_ARRAY_OF_UPDATES);
                    } catch (Exception e) {
                        updates = null;
                    }
                    if (updates != null && updates.length > 0) {
                        try {
                            if (this.onUpdate(updates)) {
                                offset = updates[updates.length - 1].getUpdate_id() + 1;
                            }
                        } catch (Exception e) {

                        }
                    }
                }
            }
        }
    }

}