|
| 1 | +// Example of using array of proxies wth tgbot-cpp library. |
| 2 | +// Based on original code (c) Oleg Morozenkov [reo7sp] https://github.com/reo7sp |
| 3 | +// https://github.com/reo7sp/tgbot-cpp/blob/master/samples/echobot-curl-client/src/main.cpp |
| 4 | + |
| 5 | +#include <csignal> |
| 6 | +#include <cstdio> |
| 7 | +#include <cstdlib> |
| 8 | +#include <exception> |
| 9 | +#include <string> |
| 10 | +#include <vector> |
| 11 | + |
| 12 | +#ifndef HAVE_CURL |
| 13 | +#define HAVE_CURL |
| 14 | +#endif |
| 15 | + |
| 16 | +#include <tgbot/net/CurlHttpClient.h> |
| 17 | +#include <tgbot/tgbot.h> |
| 18 | + |
| 19 | +using namespace std; |
| 20 | +using namespace TgBot; |
| 21 | + |
| 22 | +#define CONNECT_TIMEOUT 10L |
| 23 | + |
| 24 | +int main() { |
| 25 | + // Filling array of proxies. |
| 26 | + vector<const char*> proxies; |
| 27 | + // NULL = no proxy, direct connection to API. |
| 28 | + proxies.push_back(NULL); // [0] |
| 29 | + // All proxy-URLs below are fake. Use yuor own. |
| 30 | + proxies.push_back("socks5://user:password@10.20.30.40:1080"); // [1] |
| 31 | + proxies.push_back("http://user:password@192.168.50.70:3128"); // [2] |
| 32 | + proxies.push_back("http://user:password@192.168.80.90:3128"); // [3] |
| 33 | + // Choose "staring" index of proxy. In this example [0] - NULL=no proxy. |
| 34 | + size_t proxy_now = 0; |
| 35 | + |
| 36 | + string token(getenv("TOKEN")); |
| 37 | + printf("Token: %s\n", token.c_str()); |
| 38 | + |
| 39 | + CurlHttpClient curlHttpClient; |
| 40 | + Bot bot(token, curlHttpClient); |
| 41 | + |
| 42 | + bot.getEvents().onCommand("start", [&bot](Message::Ptr message) { |
| 43 | + bot.getApi().sendMessage(message->chat->id, "Hi!"); |
| 44 | + }); |
| 45 | + bot.getEvents().onAnyMessage([&bot](Message::Ptr message) { |
| 46 | + printf("User wrote %s\n", message->text.c_str()); |
| 47 | + if (StringTools::startsWith(message->text, "/start")) return; |
| 48 | + bot.getApi().sendMessage(message->chat->id, |
| 49 | + "Your message is: " + message->text); |
| 50 | + }); |
| 51 | + |
| 52 | + signal(SIGINT, [](int s) { |
| 53 | + printf("SIGINT got: %i\n", s); |
| 54 | + exit(0); |
| 55 | + }); |
| 56 | + |
| 57 | + while (true) { |
| 58 | + try { |
| 59 | + printf("Bot username: %s\n", bot.getApi().getMe()->username.c_str()); |
| 60 | + bot.getApi().deleteWebhook(); |
| 61 | + |
| 62 | + TgLongPoll longPoll(bot); |
| 63 | + while (true) { |
| 64 | + printf("Long poll started\n"); |
| 65 | + longPoll.start(); |
| 66 | + } |
| 67 | + } catch (exception &e) { |
| 68 | + printf("Proxy[%li]: %s error\n", proxy_now, proxies[proxy_now]); |
| 69 | + printf("%s\n", e.what()); |
| 70 | + // Assumption: |
| 71 | + // the reason of exception was - disconnect, |
| 72 | + // connection timeout or other network problem. |
| 73 | + // Trying to switch (cycle) to next proxy in array. |
| 74 | + proxy_now++; |
| 75 | + if (proxy_now >= proxies.size()) proxy_now = 0; |
| 76 | + curlHttpClient.setProxy(proxies[proxy_now], CONNECT_TIMEOUT); |
| 77 | + printf("Switch proxy[%li]: %s\n", proxy_now, proxies[proxy_now]); |
| 78 | + } |
| 79 | + } |
| 80 | + return 0; |
| 81 | +} |
| 82 | + |
| 83 | +// eof |
0 commit comments