Telegram bot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1.1 KiB

  1. #include <stdio.h>
  2. #include <tgbot/tgbot.h>
  3. int main() {
  4. TgBot::Bot bot("436931164:AAG3LyZywIzODiMhXtBPzMI4U3BBEv4z4zA");
  5. bot.getEvents().onCommand("start", [&bot](TgBot::Message::Ptr message) {
  6. bot.getApi().sendMessage(message->chat->id, "Hi!");
  7. });
  8. bot.getEvents().onCommand("love", [&bot](TgBot::Message::Ptr message) {
  9. bot.getApi().sendMessage(message->chat->id, "I love you!");
  10. });
  11. bot.getEvents().onAnyMessage([&bot](TgBot::Message::Ptr message) {
  12. printf("User wrote %s\n", message->text.c_str());
  13. if (StringTools::startsWith(message->text, "/start")) {
  14. return;
  15. }
  16. if (StringTools::startsWith(message->text, "/love")) {
  17. return;
  18. }
  19. bot.getApi().sendMessage(message->chat->id, "Your message is: " + message->text);
  20. });
  21. try {
  22. printf("Bot username: %s\n", bot.getApi().getMe()->username.c_str());
  23. TgBot::TgLongPoll longPoll(bot);
  24. while (true) {
  25. printf("Long poll started\n");
  26. longPoll.start();
  27. }
  28. } catch (TgBot::TgException& e) {
  29. printf("error: %s\n", e.what());
  30. }
  31. return 0;
  32. }