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

Send message behind a proxy #401

Closed pswrdf closed 6 years ago

pswrdf commented 6 years ago

TelegramLongPollingBot

TelegramApiException: Unable to execute sendmessage method
Caused by: org.apache.http.conn.HttpHostConnectException: Connect to api.telegram.org:443 failed: (connect failed)

I'm behind a proxy and set that way:

RequestConfig requestconfig = new RequestConfig.Builder().setProxy(new HttpHost(phost, pport)).build();
bot.getOptions().setRequestConfig(requestconfig);
new TelegramBotsApi().registerBot(bot);

sending message usual way:

            SendMessage sendMessage = new SendMessage();
            sendMessage.setChatId(chatId).setText(msg);
            execute(sendMessage);

Without a proxy this bot worked nicely

kagaricyan commented 6 years ago

but without proxy i can't receive message from bot;the same time i set proxy and have received message but cannot execute the method execute(send message). can you help me?

valinz commented 6 years ago

My telegram bot works well behind a proxy. I set my proxy config by constructor. Hopefully it can work on your site.

DefaultBotOptions botOptions = ApiContext.getInstance(DefaultBotOptions.class);
if (proxyEnabled) {
     HttpHost httpHost = new HttpHost("proxy_host", proxy_port);
     RequestConfig requestConfig = RequestConfig.custom().setProxy(httpHost).build();
     botOptions.setRequestConfig(requestConfig);
}
MyBot myBot = new MyBot(botOptions);
new TelegramBotsApi().registerBot(myBot)
rubenlagus commented 6 years ago

@pswrdf Does this solution work for you?

bvn13 commented 6 years ago

@valinz how to set credentials for proxy authentication?

bvn13 commented 6 years ago

@rubenlagus I have implemented http proxy authorization (tested on squid3). Please check my pull request https://github.com/rubenlagus/TelegramBots/pull/435 (resent into dev branch)

valinz commented 6 years ago

@bvn13 I don't enable authentication in my proxy server.

bvn13 commented 6 years ago

@valinz I reworked the code.

bvn13 commented 6 years ago

@pswrdf Please check new functionality https://github.com/rubenlagus/TelegramBots/wiki/Using-Http-Proxy

@rubenlagus Consider to close this issue please.

KostadinAlmishev commented 6 years ago

I do everything from: Using-Http-Proxy but i recieved this error:

/usr/lib/jvm/java-9-oracle/bin/java -javaagent:/opt/idea-IU-181.4445.78/lib/idea_rt.jar=40371:/opt/idea-IU-181.4445.78/bin -Dfile.encoding=UTF-8 -classpath /home/kostadin/IdeaProjects/telegram-bot/out/production/classes:/home/kostadin/.gradle/caches/modules-2/files-2.1/org.telegram/telegrambots-abilities/3.6/64e6856231f4888382973bdafec09826df98e52/telegrambots-abilities-3.6.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/org.telegram/telegrambots/3.6.1/8f9826516d5e461e5dc0bed7a8595e99be96630e/telegrambots-3.6.1.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/org.telegram/telegrambots-meta/3.6.1/6cc4250f1182335c81a48a146371b7abecc81517/telegrambots-meta-3.6.1.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/com.fasterxml.jackson.jaxrs/jackson-jaxrs-json-provider/2.8.7/3d6ac461d0b478c85ca380468f675c7217295591/jackson-jaxrs-json-provider-2.8.7.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/com.fasterxml.jackson.jaxrs/jackson-jaxrs-base/2.8.7/814af809cd9fca86e9d89c17080bb0446b06569d/jackson-jaxrs-base-2.8.7.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/com.fasterxml.jackson.module/jackson-module-jaxb-annotations/2.8.7/4d7e1de1694d9f95eada828dabf43121b3afc435/jackson-module-jaxb-annotations-2.8.7.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/com.fasterxml.jackson.core/jackson-databind/2.8.7/6c3257ef458ac58a8da69a6dca3d2a15286d88c8/jackson-databind-2.8.7.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/com.fasterxml.jackson.core/jackson-annotations/2.8.0/45b426f7796b741035581a176744d91090e2e6fb/jackson-annotations-2.8.0.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/org.glassfish.jersey.media/jersey-media-json-jackson/2.25.1/19d1e4276eb7b6386640c344d9e5c01eba7eae5d/jersey-media-json-jackson-2.25.1.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/org.glassfish.jersey.containers/jersey-container-grizzly2-http/2.25.1/6cfd264755979bafbc65dba660bbc898b25998d0/jersey-container-grizzly2-http-2.25.1.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/org.glassfish.jersey.core/jersey-server/2.25.1/276e2ee0fd1cdabf99357fce560c5baab675b1a2/jersey-server-2.25.1.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/org.json/json/20160810/aca5eb39e2a12fddd6c472b240afe9ebea3a6733/json-20160810.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/org.apache.httpcomponents/httpmime/4.5.3/889fd6d061bb63b99dd5c6aba35a555ae863de52/httpmime-4.5.3.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/org.apache.httpcomponents/httpclient/4.5.3/d1577ae15f01ef5438c5afc62162457c00a34713/httpclient-4.5.3.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/commons-io/commons-io/2.5/2852e6e05fbb95076fc091f6d1780f1f8fe35e0f/commons-io-2.5.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/org.apache.commons/commons-lang3/3.5/6c6c702c89bfff3cd9e80b04d668c5e190d588c6/commons-lang3-3.5.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/org.mapdb/mapdb/3.0.4/36d8fefc69caec455e5154c99e367b93ccf6970c/mapdb-3.0.4.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/com.google.inject/guice/4.1.0/eeb69005da379a10071aa4948c48d89250febb07/guice-4.1.0.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/com.google.guava/guava/19.0/6ce200f6b23222af3d8abb6b6459e6c44f4bb0e9/guava-19.0.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib/1.0.7/b1724f4912eab4cc011b6750e2b4ed7dca96e66/kotlin-stdlib-1.0.7.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/org.eclipse.collections/eclipse-collections-forkjoin/7.1.1/892b970cb03461f8c10daba778edbef3f0a26846/eclipse-collections-forkjoin-7.1.1.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/org.eclipse.collections/eclipse-collections/7.1.1/83eca9f03f1f2d055df453d72036c5ce2e82896b/eclipse-collections-7.1.1.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/org.eclipse.collections/eclipse-collections-api/7.1.1/806c2b3bef5364d1a1668f25ce70100fa2c6b919/eclipse-collections-api-7.1.1.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/net.jpountz.lz4/lz4/1.3.0/c708bb2590c0652a642236ef45d9f99ff842a2ce/lz4-1.3.0.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/org.mapdb/elsa/3.0.0-M5/d7c1920d084e741e04844f512af24012e745e809/elsa-3.0.0-M5.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-runtime/1.0.7/abb45107a84b3c173a8681b8f51af89e73216524/kotlin-runtime-1.0.7.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/net.jcip/jcip-annotations/1.0/afba4942caaeaf46aab0b976afd57cc7c181467e/jcip-annotations-1.0.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/com.fasterxml.jackson.core/jackson-core/2.8.7/8b46f39c78476fb848c81a49fa807a9e9506dddd/jackson-core-2.8.7.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/org.glassfish.jersey.core/jersey-client/2.25.1/4d563b1f93352ee9fad597e9e1daf2c6159993c6/jersey-client-2.25.1.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/org.glassfish.jersey.media/jersey-media-jaxb/2.25.1/d7da0beeed5614a3bfd882662faec602699e24b/jersey-media-jaxb-2.25.1.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/org.glassfish.jersey.core/jersey-common/2.25.1/2438ce68d4907046095ab54aa83a6092951b4bbb/jersey-common-2.25.1.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/org.glassfish.jersey.ext/jersey-entity-filtering/2.25.1/4a5805060f796ec2c9bb1ba0ce91c1db6d889524/jersey-entity-filtering-2.25.1.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/org.glassfish.hk2/hk2-locator/2.5.0-b32/195474f8ad0a8d130e9ea949a771bcf1215fc33b/hk2-locator-2.5.0-b32.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/org.glassfish.hk2.external/javax.inject/2.5.0-b32/b2fa50c8186a38728c35fe6a9da57ce4cc806923/javax.inject-2.5.0-b32.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/org.glassfish.grizzly/grizzly-http-server/2.3.28/13bc9a63dae3a0a623b52fe71753d5413d134540/grizzly-http-server-2.3.28.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/javax.ws.rs/javax.ws.rs-api/2.0.1/104e9c2b5583cfcfeac0402316221648d6d8ea6b/javax.ws.rs-api-2.0.1.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/javax.annotation/javax.annotation-api/1.2/479c1e06db31c432330183f5cae684163f186146/javax.annotation-api-1.2.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/org.glassfish.hk2/hk2-api/2.5.0-b32/6a576c9653832ce610b80a2f389374ef19d96171/hk2-api-2.5.0-b32.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/javax.validation/validation-api/1.1.0.Final/8613ae82954779d518631e05daa73a6a954817d5/validation-api-1.1.0.Final.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/org.apache.httpcomponents/httpcore/4.4.6/e3fd8ced1f52c7574af952e2e6da0df8df08eb82/httpcore-4.4.6.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/commons-logging/commons-logging/1.2/4bfc12adfe4842bf07b657f0369c4cb522955686/commons-logging-1.2.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/commons-codec/commons-codec/1.9/9ce04e34240f674bc72680f8b843b1457383161a/commons-codec-1.9.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/org.glassfish.hk2/hk2-utils/2.5.0-b32/5108a926988c4ceda7f1e681dddfe3101454a002/hk2-utils-2.5.0-b32.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/javax.inject/javax.inject/1/6975da39a7040257bd51d21a231b76c915872d38/javax.inject-1.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/aopalliance/aopalliance/1.0/235ba8b489512805ac13a8f9ea77a1ca5ebe3e8/aopalliance-1.0.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/org.glassfish.jersey.bundles.repackaged/jersey-guava/2.25.1/a2bb4f8208e134cf2cf71dfb8824e42942f7bd06/jersey-guava-2.25.1.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/org.glassfish.hk2/osgi-resource-locator/1.0.1/4ed2b2d4738aed5786cfa64cba5a332779c4c708/osgi-resource-locator-1.0.1.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/org.glassfish.grizzly/grizzly-http/2.3.28/bb34b4e7fbb66b53ac6d428dcc99f5925c9ff7bd/grizzly-http-2.3.28.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/org.glassfish.hk2.external/aopalliance-repackaged/2.5.0-b32/6af37c3f8ec6f9e9653ec837eb508da28ce443cd/aopalliance-repackaged-2.5.0-b32.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/org.javassist/javassist/3.20.0-GA/a9cbcdfb7e9f86fbc74d3afae65f2248bfbf82a0/javassist-3.20.0-GA.jar:/home/kostadin/.gradle/caches/modules-2/files-2.1/org.glassfish.grizzly/grizzly-framework/2.3.28/23a90f6316b3776699b173ccf9394c69d15b7e9c/grizzly-framework-2.3.28.jar Main
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.google.inject.internal.cglib.core.$ReflectUtils$1 (file:/home/kostadin/.gradle/caches/modules-2/files-2.1/com.google.inject/guice/4.1.0/eeb69005da379a10071aa4948c48d89250febb07/guice-4.1.0.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
WARNING: Please consider reporting this to the maintainers of com.google.inject.internal.cglib.core.$ReflectUtils$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Apr 29, 2018 8:22:03 PM org.telegram.telegrambots.logging.BotLogger info
INFO: Ability - No post action was detected for method with name [hello]
Apr 29, 2018 8:22:03 PM org.telegram.telegrambots.logging.BotLogger info
INFO: Ability - No post action was detected for method with name [hi]
Apr 29, 2018 8:22:03 PM org.telegram.telegrambots.logging.BotLogger info
INFO: Ability - No post action was detected for method with name [commands]
Apr 29, 2018 8:22:03 PM org.telegram.telegrambots.logging.BotLogger info
INFO: Ability - No post action was detected for method with name [backup]
Apr 29, 2018 8:22:03 PM org.telegram.telegrambots.logging.BotLogger info
INFO: Ability - No post action was detected for method with name [recover]
org.telegram.telegrambots.exceptions.TelegramApiRequestException: Error removing old webhook
    at org.telegram.telegrambots.bots.TelegramLongPollingBot.clearWebhook(TelegramLongPollingBot.java:32)
    at org.telegram.telegrambots.TelegramBotsApi.registerBot(TelegramBotsApi.java:120)
    at Main.main(Main.java:46)
Caused by: org.telegram.telegrambots.exceptions.TelegramApiRequestException: Error deleting webhook: [401] Unauthorized
    at org.telegram.telegrambots.api.methods.updates.DeleteWebhook.deserializeResponse(DeleteWebhook.java:40)
    at org.telegram.telegrambots.api.methods.updates.DeleteWebhook.deserializeResponse(DeleteWebhook.java:19)
    at org.telegram.telegrambots.bots.DefaultAbsSender.sendApiMethod(DefaultAbsSender.java:689)
    at org.telegram.telegrambots.bots.AbsSender.execute(AbsSender.java:53)
    at org.telegram.telegrambots.bots.TelegramLongPollingBot.clearWebhook(TelegramLongPollingBot.java:27)
    ... 2 more
KostadinAlmishev commented 6 years ago

I fixed it.

liyaxc commented 6 years ago

@KostadinAlmishev I have the same exception, can't solve it for a week... could you please explain how did you fix it? Can you please share that proxy that works?

liyaxc commented 6 years ago

I can't find a http proxy which will work. Does anyone have one?

bvn13 commented 6 years ago

@liyaxc I used my own private proxy. Could not share it, sorry. But you could install and use your own one.