From 3d834c6d18e9c64a0873ba0f18de1eac6d2db75a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hauke=20Z=C3=BChl?= Date: Wed, 7 Jun 2017 20:36:32 +0200 Subject: [PATCH] Added a simple Telegram client --- examples/example02.cc | 100 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 examples/example02.cc diff --git a/examples/example02.cc b/examples/example02.cc new file mode 100644 index 0000000..0eda28e --- /dev/null +++ b/examples/example02.cc @@ -0,0 +1,100 @@ +/** + * + * This is a simple Telegram client that can echo a message, piped to its + * binary and to set your web hook. + * + * If you want to use this example to send your web hook to Telegram's + * API, change BOT_TOKEN according to your needs and your web server! + * + * Example: + * BOT_TOKEN: Your token given by Telegram's Bot Father! + * + * To compile do: + * g++ -c -o example02.o -I/usr/include/jsoncpp example02.cc + * g++ -o example02 example02.o -ltgbotpp + * + * Run: + * echo "Your message" | ./example02 -u + * + * To set your web hook to Telegram's network: + * ./example02 -w + * + * To send a photo to a user or group: + * ./example02 -p file:///path/to/pic.jpg -u + */ + +#include + +#include +#include + +#include +#include + +#define WEBHOOK_URL "https://your-domain/cgi-bin/bot.cgi" +#define BOT_TOKEN "your secret token" + +// main function +int main(int argc, char** argv) { + + // Process options + std::string user_id = ""; // ID of the receiving user + std::string filename = ""; // File name of file to be sent + + Telegram::TelegramBot tg(BOT_TOKEN); + +#ifdef __arm__ + int c; +#else + char c; +#endif + + while ((c = getopt(argc, argv, "wu:p:")) != -1) { + switch(c) { + case 'u': + user_id = (std::string)optarg; + break; + case 'w': + tg.setWebhook(WEBHOOK_URL); + return 0; + break; // Never reached + case 'p': + filename = (std::string)optarg; + break; + case ':': + std::cerr << optopt << " without argument" << std::endl; + break; + } + } + + if (user_id == "") { + std::cerr << "No user ID given!" << std::endl; + return 1; + } + + if (filename != "") { + // Currently only photos are supported + tg.sendPhoto(filename, user_id); + return 0; + } + + // Read from stdin + struct termios t; + std::string s = ""; + + if (tcgetattr(STDIN_FILENO, &t) < 0) { + // If we have a text piped in, read from std::cin + while(std::cin) { + std::string line; + std::getline(std::cin, line); + s = s + line + "\n"; + } + s.erase(s.end()-1); + } + + if (s != "") { + tg.sendMessage(s, user_id); // Send the message to the user + } + + return 0; +}