From 2930f5091be89452a686b3001f9a405680a79faf Mon Sep 17 00:00:00 2001 From: "Vyacheslav N. Boyko" Date: Thu, 19 Apr 2018 15:13:40 +0300 Subject: [PATCH] updating wiki with page containing instance of http proxy using --- TelegramBots.wiki/Using-Http-Proxy.md | 122 ++++++++++++++++++++++++++ TelegramBots.wiki/_Sidebar.md | 1 + 2 files changed, 123 insertions(+) create mode 100644 TelegramBots.wiki/Using-Http-Proxy.md diff --git a/TelegramBots.wiki/Using-Http-Proxy.md b/TelegramBots.wiki/Using-Http-Proxy.md new file mode 100644 index 00000000..502f145e --- /dev/null +++ b/TelegramBots.wiki/Using-Http-Proxy.md @@ -0,0 +1,122 @@ +### Using HTTP proxy + +HTTP proxy support implemented since version 3.6.1 + +First of all you need to override constructor with `DefaultBotOptions` argument while inheriting from `AbilityBot` or `TelegramLongPollingBot` + +```java +public class MyBot extends AbilityBot { + + protected MyBot(String botToken, String botUsername, DefaultBotOptions botOptions) { + super(botToken, botUsername, botOptions); + } + + public int creatorId() { + return 0; + } + + public Ability pingPong() { + return Ability + .builder() + .name("ping") + .info("ping pong") + .locality(ALL) + .privacy(PUBLIC) + .action(ctx -> silent.send("pong", ctx.chatId())) + .build(); + } + +} +``` + +Now you are able to set up your proxy + +#### without authentication + +```java +public class Main { + + private static String BOT_NAME = "My test bot"; + private static String BOT_TOKEN = "..." /* your bot's token here */; + + private static String PROXY_HOST = "..." /* proxy host */; + private static Integer PROXY_PORT = 3128 /* proxy port */; + + public static void main(String[] args) { + try { + + ApiContextInitializer.init(); + + // Create the TelegramBotsApi object to register your bots + TelegramBotsApi botsApi = new TelegramBotsApi(); + + // Set up Http proxy + DefaultBotOptions botOptions = ApiContext.getInstance(DefaultBotOptions.class); + + HttpHost httpHost = new HttpHost(PROXY_HOST, PROXY_PORT); + + RequestConfig requestConfig = RequestConfig.custom().setProxy(httpHost).setAuthenticationEnabled(false).build(); + botOptions.setRequestConfig(requestConfig); + botOptions.setHttpProxy(httpHost); + + // Register your newly created AbilityBot + MyBot bot = new MyBot(BOT_TOKEN, BOT_NAME, botOptions); + + botsApi.registerBot(bot); + + } catch (TelegramApiException e) { + e.printStackTrace(); + } + } +} + +``` + + +#### With authentication + +```java +public class Main { + + private static String BOT_NAME = "My test bot"; + private static String BOT_TOKEN = "..." /* your bot's token here */; + + private static String PROXY_HOST = "..." /* proxy host */; + private static Integer PROXY_PORT = 3128 /* proxy port */; + private static String PROXY_USER = "..." /* proxy user */; + private static String PROXY_PASSWORD = "..." /* proxy password */; + + public static void main(String[] args) { + try { + + ApiContextInitializer.init(); + + // Create the TelegramBotsApi object to register your bots + TelegramBotsApi botsApi = new TelegramBotsApi(); + + // Set up Http proxy + DefaultBotOptions botOptions = ApiContext.getInstance(DefaultBotOptions.class); + + CredentialsProvider credsProvider = new BasicCredentialsProvider(); + credsProvider.setCredentials( + new AuthScope(PROXY_HOST, PROXY_PORT), + new UsernamePasswordCredentials(PROXY_USER, PROXY_PASSWORD)); + + HttpHost httpHost = new HttpHost(PROXY_HOST, PROXY_PORT); + + RequestConfig requestConfig = RequestConfig.custom().setProxy(httpHost).setAuthenticationEnabled(true).build(); + botOptions.setRequestConfig(requestConfig); + botOptions.setCredentialsProvider(credsProvider); + botOptions.setHttpProxy(httpHost); + + // Register your newly created AbilityBot + MyBot bot = new MyBot(BOT_TOKEN, BOT_NAME, botOptions); + + botsApi.registerBot(bot); + + } catch (TelegramApiException e) { + e.printStackTrace(); + } + } +} +``` \ No newline at end of file diff --git a/TelegramBots.wiki/_Sidebar.md b/TelegramBots.wiki/_Sidebar.md index fc7b47b2..0bc64b51 100644 --- a/TelegramBots.wiki/_Sidebar.md +++ b/TelegramBots.wiki/_Sidebar.md @@ -1,6 +1,7 @@ * Users guide * [[Getting Started]] * [[Errors Handling]] + * [[Using HTTP Proxy]] * [[FAQ]] * AbilityBot * [[Simple Example]]