rubenlagus / TelegramBots

Java library to create bots using Telegram Bots API
https://telegram.me/JavaBotsApi
MIT License
4.77k stars 1.22k forks source link

Question on Creating Bot #5

Closed timyip3 closed 8 years ago

timyip3 commented 8 years ago

I would like to ask if it is able to create TelegramBotApi without any parameter(i.e: using no-arg constructor) and using it to register bot??

When I follow the usage, there is HTTPHostConnectException popped up.

Here is my code:

`public static void main(String[] args) {

    TelegramBotsApi telegramBotsApi = new TelegramBotsApi();

    TelegramLongPollingBot telegramLongPollingBot = new TelegramLongPollingBot() {

        @Override
        public void onUpdateReceived(Update update) {
            Message message = update.getMessage();

            if (message != null && message.hasText()) {
                HandlerMessages(message);
            }
        }

        private void HandlerMessages(Message message) {
            SendMessage sendMessage = new SendMessage();
            sendMessage.setChatId(message.getChatId().toString());

            sendMessage.setText("response");

            try {
                sendMessage(sendMessage);
            } catch (TelegramApiException e) {
                e.printStackTrace();
            }
        }

        @Override
        public String getBotUsername() {
            return "testBot";

        }

        @Override
        public String getBotToken() {
            return "{MY_TOKEN}";

        }
    };

    try {
        telegramBotsApi.registerBot(telegramLongPollingBot);
        System.out.println("registered");
    } catch (TelegramApiException e) {
        e.printStackTrace();
    }
}`

Here is the console:

org.telegram.telegrambots.TelegramApiException: Error executing setWebook method at org.telegram.telegrambots.TelegramBotsApi.setWebhook(TelegramBotsApi.java:158) at org.telegram.telegrambots.TelegramBotsApi.setWebhook(TelegramBotsApi.java:111) at org.telegram.telegrambots.TelegramBotsApi.registerBot(TelegramBotsApi.java:88) at main.Main.main(Main.java:65) Caused by: org.apache.http.conn.HttpHostConnectException: Connect to api.telegram.org:443 [api.telegram.org/149.154.167.198, api.telegram.org/149.154.167.199, api.telegram.org/149.154.167.200, api.telegram.org/149.154.167.197] failed: Connection timed out: connect at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:151) at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353) at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380) at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236) at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184) at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88) at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110) at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107) at org.telegram.telegrambots.TelegramBotsApi.setWebhook(TelegramBotsApi.java:147) ... 3 more Caused by: java.net.ConnectException: Connection timed out: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) at java.net.Socket.connect(Socket.java:589) at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:337) at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:134) ... 13 more

Clevero commented 8 years ago

try to follow these :)

rubenlagus commented 8 years ago

@timyip3 Did @Clevero's tutorial fixed it? In any case, it seems like some kind of network connection issue... as long as "{MY_TOKEN}" represent your real bot token, it should work.

timyip3 commented 8 years ago

the tutorial really helps. thanks!

rubenlagus commented 8 years ago

@timyip3 Did you manage it? If so, can you close the issue?

Abdulloh90 commented 7 years ago

Can I work without a proxy in the java

Abdulloh90 commented 7 years ago

can you help