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

java.net.SocketException: Socket is not connected: #288

Closed nickolson77 closed 6 years ago

nickolson77 commented 7 years ago

Hi, I'm using below code and got error. Maybe problem in proxy settings?

public class SimpleBot extends TelegramLongPollingBot {

    public static void main(String[] args) {
        ApiContextInitializer.init();
        TelegramBotsApi telegramBotsApi = new TelegramBotsApi();

        try {                    
                    telegramBotsApi.registerBot(new SimpleBot());
        } catch (TelegramApiException e) {
            e.printStackTrace();
        }
    }
Retrying request to {s}->https://api.telegram.org:443
org.telegram.telegrambots.exceptions.TelegramApiRequestException: Error executing setWebook method
    at org.telegram.telegrambots.bots.TelegramLongPollingBot.clearWebhook(TelegramLongPollingBot.java:56)
    at org.telegram.telegrambots.TelegramBotsApi.registerBot(TelegramBotsApi.java:120)
    at menu.SimpleBot.main(SimpleBot.java:18)
Caused by: java.net.SocketException: Socket is not connected: 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:339)
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:359)
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:381)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:237)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)
    at org.telegram.telegrambots.bots.TelegramLongPollingBot.clearWebhook(TelegramLongPollingBot.java:44)
rubenlagus commented 7 years ago

Maybe you can try to configure a RequestConfig with the settings of your proxy and provide it in the BotOptions? https://github.com/rubenlagus/TelegramBots/blob/master/telegrambots/src/main/java/org/telegram/telegrambots/bots/DefaultBotOptions.java#L69

nickolson77 commented 7 years ago

Thanks for reply, it works! Could you review my code. Is this "template" good for starting implementing telegram bot? (plan to increase features) Method "sendMessage(sendMessage)" is deprecated. (@Deprecated public final Message sendMessage(SendMessage p1) { }) Are there alternatives ?

public class SimpleBot extends TelegramLongPollingBot {

    private SimpleBot(DefaultBotOptions options) {
        super(options);
    }
    public static void main(String[] args) {
        HttpHost proxy = new HttpHost("10.10.10.2", 3008);
        RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
        DefaultBotOptions options = new DefaultBotOptions();
        options.setRequestConfig(config);

            ApiContextInitializer.init();
            TelegramBotsApi telegramBotsApi = new TelegramBotsApi();
            try {                    
                telegramBotsApi.registerBot(new SimpleBot(options));
            } catch (TelegramApiException e) {
                    e.printStackTrace();
            }
    }

    @Override
    public String getBotUsername() {
        return "NAME_bot";
    }

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

    @Override
    public void onUpdateReceived(Update update) {
        Message message = update.getMessage();
        if (message != null && message.hasText()) {
                    if (message.getText().equals("/help")){
                sendMsg(message, "Hi, I'm bot");
                    }
        }
    }

    private void sendMsg(Message message, String text) {
        SendMessage sendMessage = new SendMessage();
        sendMessage.enableMarkdown(true);
        sendMessage.setChatId(message.getChatId().toString());
        sendMessage.setReplyToMessageId(message.getMessageId());
        sendMessage.setText(text);
        try {
            sendMessage(sendMessage);
        } catch (TelegramApiException e) {
            e.printStackTrace();
        }
    }

}
stefan-kolb commented 7 years ago

Method "sendMessage(sendMessage)" is deprecated. (@deprecated public final Message sendMessage(SendMessage p1) { }) Are there alternatives ?

Alternatives are #execute

nickolson77 commented 7 years ago

Alternatives are #execute

Sorry, not clear for me =) What do you mean saying #execute ?

stefan-kolb commented 7 years ago

@nickolson77 The new method is called execute instead of sendMessage.

nickolson77 commented 7 years ago

Ok, thanks =) It works!

ps Now I'm trying to implement following for my bot and can't find any examples: 1) User subscribe to my bot 2) Bot send message to all subscribed users

Could you help me?

stefan-kolb commented 7 years ago

See https://github.com/rubenlagus/TelegramBots/wiki/Getting-Started. Check for /start message. For your second question just send a message to all your users via chatIDs (throttle sending to ~30msg/second!).