forked from MCMrARM/updateprocessor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelegram_state.cpp
32 lines (26 loc) · 1.15 KB
/
telegram_state.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "telegram_state.h"
#include <fstream>
#include <sstream>
#include <regex>
TelegramState::TelegramState(ApkManager& apkManager) {
std::ifstream ifs("priv/telegram.conf");
config.load(ifs);
api.setToken(config.get("token"));
broadcastTo = config.get_array("broadcast", {});
using namespace std::placeholders;
apkManager.addNewVersionCallback(std::bind(&TelegramState::onNewVersion, this, _1, _2, _3, _4));
}
void TelegramState::onNewVersion(int version, std::string const& versionString, std::string const& changelog,
std::string const& variant) {
static std::regex regexNewlines ("<br>");
std::string processedChangelog = std::regex_replace(changelog, regexNewlines, "\n");
std::stringstream ss;
ss << "*New version available: " << versionString
<< "* (version code: " << version << ", variant: " << variant << ")\n"
<< processedChangelog;
broadcastMessage(ss.str(), "Markdown");
}
void TelegramState::broadcastMessage(std::string const& text, const std::string& parseMode) {
for (std::string const& target : broadcastTo)
api.sendMessage(target, text, parseMode);
}