Telegram bot
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 

36 行
1.1 KiB

#include <stdio.h>
#include <tgbot/tgbot.h>
int main() {
TgBot::Bot bot("436931164:AAG3LyZywIzODiMhXtBPzMI4U3BBEv4z4zA");
bot.getEvents().onCommand("start", [&bot](TgBot::Message::Ptr message) {
bot.getApi().sendMessage(message->chat->id, "Hi!");
});
bot.getEvents().onCommand("love", [&bot](TgBot::Message::Ptr message) {
bot.getApi().sendMessage(message->chat->id, "I love you!");
});
bot.getEvents().onAnyMessage([&bot](TgBot::Message::Ptr message) {
printf("User wrote %s\n", message->text.c_str());
if (StringTools::startsWith(message->text, "/start")) {
return;
}
if (StringTools::startsWith(message->text, "/love")) {
return;
}
bot.getApi().sendMessage(message->chat->id, "Your message is: " + message->text);
});
try {
printf("Bot username: %s\n", bot.getApi().getMe()->username.c_str());
TgBot::TgLongPoll longPoll(bot);
while (true) {
printf("Long poll started\n");
longPoll.start();
}
} catch (TgBot::TgException& e) {
printf("error: %s\n", e.what());
}
return 0;
}