LibTgBotPP
 All Classes Namespaces Files Functions Variables Typedefs Macros
example02.cc
Go to the documentation of this file.
1 
26 #include <Telegram/TelegramBot.h>
27 
28 #include <sstream>
29 #include <string>
30 
31 #include <termios.h>
32 #include <unistd.h>
33 
34 #define WEBHOOK_URL "https://your-domain/cgi-bin/bot.cgi"
35 #define BOT_TOKEN "your secret token"
36 
37 // main function
38 int main(int argc, char** argv) {
39 
40  // Process options
41  std::string user_id = ""; // ID of the receiving user
42  std::string filename = ""; // File name of file to be sent
43 
45 
46 #ifdef __arm__
47  int c;
48 #else
49  char c;
50 #endif
51 
52  while ((c = getopt(argc, argv, "wu:p:")) != -1) {
53  switch(c) {
54  case 'u':
55  user_id = (std::string)optarg;
56  break;
57  case 'w':
59  return 0;
60  break; // Never reached
61  case 'p':
62  filename = (std::string)optarg;
63  break;
64  case ':':
65  std::cerr << optopt << " without argument" << std::endl;
66  break;
67  }
68  }
69 
70  if (user_id == "") {
71  std::cerr << "No user ID given!" << std::endl;
72  return 1;
73  }
74 
75  if (filename != "") {
76  // Currently only photos are supported
77  tg.sendPhoto(filename, user_id);
78  return 0;
79  }
80 
81  // Read from stdin
82  struct termios t;
83  std::string s = "";
84 
85  if (tcgetattr(STDIN_FILENO, &t) < 0) {
86  // If we have a text piped in, read from std::cin
87  while(std::cin) {
88  std::string line;
89  std::getline(std::cin, line);
90  s = s + line + "\n";
91  }
92  s.erase(s.end()-1);
93  }
94 
95  if (s != "") {
96  tg.sendMessage(s, user_id); // Send the message to the user
97  }
98 
99  return 0;
100 }
#define WEBHOOK_URL
Definition: example02.cc:34
void setWebhook(std::string)
Definition: TelegramBot.cc:65
Telegram::Message * sendPhoto(std::string, Json::Int64)
Definition: TelegramBot.cc:149
int main(int argc, char **argv)
Definition: example02.cc:38
Telegram::Message * sendMessage(std::string, Json::Int64)
Definition: TelegramBot.cc:115
#define BOT_TOKEN
Definition: example02.cc:35