LibTgBotPP
 All Classes Namespaces Files Functions Variables Typedefs Macros
example01.cc
Go to the documentation of this file.
1 
22 #include <Telegram/TelegramBot.h>
23 
24 #include <map>
25 #include <sstream>
26 #include <string>
27 #include <vector>
28 
29 #include <termios.h>
30 #include <stdbool.h>
31 #include <unistd.h>
32 
33 #define WEBHOOK_URL "https://<your-web-server>/cgi-bin/<binary name>"
34 #define BOT_TOKEN "<your token>"
35 #define SSTR( x ) static_cast< std::ostringstream & >(( std::ostringstream() << std::dec << x ) ).str()
36 
37 std::string exec(const char* cmd) {
38 
39  FILE* pipe = popen(cmd, "r");
40  if (!pipe)
41  return "ERROR";
42 
43  char buffer[128];
44  std::string result = "";
45  while (!feof(pipe)) {
46  if (fgets(buffer, 128, pipe) != NULL)
47  result += buffer;
48  }
49  pclose(pipe);
50 
51  return result;
52 }
53 
54 std::vector<std::string> explode( const std::string &delimiter, const std::string &str) {
55 
56  std::vector<std::string> arr;
57 
58  int strleng = str.length();
59  int delleng = delimiter.length();
60  if (delleng == 0)
61  return arr; //no change
62 
63  int i = 0;
64  int k = 0;
65  while(i < strleng) {
66  int j = 0;
67  while ((i+j < strleng) && (j < delleng) && (str[i+j] == delimiter[j]))
68  j++;
69  if (j == delleng) {
70  arr.push_back(str.substr(k, i-k));
71  i += delleng;
72  k = i;
73  } else {
74  i++;
75  }
76  }
77  arr.push_back(str.substr(k, i-k));
78  return arr;
79 }
80 
81 // Command methods (callbacks)
83 
84  return("I am a simple Bot, written in C++\nHave fun.");
85 }
86 
88 
89  std::map<std::string, Telegram::TCommand>::iterator it;
90  std::string help_list = "Commands:\n";
91  Telegram::TCommandMap command_map = tg->getCommandMap();
92 
93  for (it = command_map.begin(); it != command_map.end(); it++) {
94  help_list = help_list + it->first + ": " + it->second.help_text + "\n";
95  }
96 
97  return(help_list);
98 }
99 
101 
102  return("Your ID is " + SSTR(tg->getMessage()->getChat()->getId()));
103 }
104 
106 
107  return(exec("cat /proc/loadavg"));
108 }
109 
111 
112  return(exec("pstree"));
113 }
114 
115 // main function
116 int main(int argc, char** argv) {
117  std::vector<std::string> sExploded = explode(".", argv[0]);
118 
119  // @TODO: Check if we are called as .cgi!
120  if ((sExploded.size() > 1) && (sExploded[1] == "cgi")) {
121  // Do bot stuff!
122  std::string line = "";
123  while(std::cin) {
124  std::string s;
125  std::getline(std::cin, s);
126  line = line + s;
127  }
129 
130  // Set the commands
132  sc.command = "/start";
133  sc.callback = &start;
134  sc.help_text = "This is the start command";
135  tg.addCommand(sc);
136 
137  sc.command = "/help";
138  sc.callback = &help;
139  sc.help_text = "Shows you the commands this bot knows";
140  tg.addCommand(sc);
141 
142  sc.command = "/whoami";
143  sc.callback = &whoami;
144  sc.help_text = "Tells you your Telegram ID";
145  tg.addCommand(sc);
146 
147  sc.command = "/loadavg";
148  sc.callback = &loadavg;
149  sc.help_text = "Shows average load of the system this bot is running on";
150  tg.addCommand(sc);
151 
152  sc.command = "/proc";
153  sc.callback = &proc;
154  sc.help_text = "Shows the process list of the system the bot is running on";
155  tg.addCommand(sc);
156 
157  tg.processMessage(line);
158 
159  } else {
160  // Process options
161  std::string user_id = ""; // ID of the receiving user
162 
163 #ifdef __arm__
164  int c;
165 #else
166  char c;
167 #endif
168 
169  while ((c = getopt(argc, argv, "u:")) != -1) {
170  switch(c) {
171  case 'u':
172  user_id = (std::string)optarg;
173  break;
174  case ':':
175  std::cerr << optopt << " without argument" << std::endl;
176  break;
177  }
178  }
179 
180  if (user_id == "") {
181  std::cerr << "No user ID given!" << std::endl;
182  return 1;
183  }
184 
185  // Read from stdin
186  struct termios t;
187  std::string s = "";
188  if (tcgetattr(STDIN_FILENO, &t) < 0) {
189  // If we have a text piped in, read from std::cin
190  while(std::cin) {
191  std::string line;
192  std::getline(std::cin, line);
193  s = s + line + "\n";
194  }
195  s.erase(s.end()-1);
196  }
197 
198  if (s != "") {
200  tg.sendMessage(s, user_id);
201  }
202 
203  }
204 
205  return 0;
206 }
std::string command
The command, e.g. /help.
Definition: TelegramBot.h:27
int main(int argc, char **argv)
Definition: example01.cc:116
std::string help(Telegram::TelegramBot *tg, Telegram::TCommandLine args)
Definition: example01.cc:87
std::string start(Telegram::TelegramBot *tg, Telegram::TCommandLine args)
Definition: example01.cc:82
#define SSTR(x)
Definition: example01.cc:35
std::vector< std::string > explode(const std::string &delimiter, const std::string &str)
Definition: example01.cc:54
std::string help_text
Help text of the command.
Definition: TelegramBot.h:29
void processMessage(std::string)
Definition: TelegramBot.cc:79
std::map< std::string, TCommand > getCommandMap()
Definition: TelegramBot.cc:192
std::string exec(const char *cmd)
Definition: example01.cc:37
std::string proc(Telegram::TelegramBot *tg, Telegram::TCommandLine args)
Definition: example01.cc:110
CommandCallback callback
Pointer to the callback function.
Definition: TelegramBot.h:28
std::string whoami(Telegram::TelegramBot *tg, Telegram::TCommandLine args)
Definition: example01.cc:100
Telegram::Chat * getChat()
Definition: Message.cc:122
std::map< std::string, TCommand > TCommandMap
Map that stores the known commands.
Definition: TelegramBot.h:31
std::vector< std::string > TCommandLine
Arguments for the bot's commands.
Definition: TelegramBot.h:18
void addCommand(TCommand)
Definition: TelegramBot.cc:49
Telegram::Message * getMessage()
Definition: TelegramBot.cc:182
Json::Int64 getId()
Definition: Chat.cc:13
#define BOT_TOKEN
Definition: example01.cc:34
std::string loadavg(Telegram::TelegramBot *tg, Telegram::TCommandLine args)
Definition: example01.cc:105
Telegram::Message * sendMessage(std::string, Json::Int64)
Definition: TelegramBot.cc:115