Browse Source

telegram interface

master
alistair 3 years ago
parent
commit
3f43fb2ea6
  1. 126
      telegram_bot.cpp

126
telegram_bot.cpp

@ -24,14 +24,19 @@ using json = nlohmann::json; @@ -24,14 +24,19 @@ using json = nlohmann::json;
namespace util {
std::string trim_whitespace(std::string s) {
std::string
trim_whitespace(std::string s)
{
int ff = s.find_first_not_of(" \n\t");
int ll = s.find_last_not_of(" \n\t");
return s.substr(ff, ll - ff + 1);
}
std::string read_file(std::string const &fpath) {
std::string
read_file(std::string const &fpath)
{
std::ostringstream sstr;
std::ifstream in (fpath);
sstr << in.rdbuf();
@ -62,7 +67,9 @@ class spotify { @@ -62,7 +67,9 @@ class spotify {
public:
void verify_logged_in() {
void
verify_logged_in()
{
/*
*
* Would be a good idea to just requrst a page to chek. but the /me
@ -85,12 +92,14 @@ class spotify { @@ -85,12 +92,14 @@ class spotify {
*/
}
spotify(std::string access_token) : access_token(access_token) {
spotify(std::string access_token) : access_token(access_token)
{
auth_header = cpr::Header{{"Authorization", "Bearer " + access_token}};
verify_logged_in();
}
spotify(std::string client_id, std::string client_secret) {
spotify (std::string client_id, std::string client_secret)
{
auto ascii_token = client_id + ":" + client_secret;
size_t buf_length = base64::get_encoded_length(ascii_token.length());
@ -118,7 +127,9 @@ class spotify { @@ -118,7 +127,9 @@ class spotify {
verify_logged_in();
}
std::optional<json> get_track(std::string track_id) {
std::optional<json>
get_track(std::string track_id)
{
auto r = cpr::Get(cpr::Url{API_NAME_BASE + "v1/tracks/" + track_id}, auth_header);
if (r.status_code == 200) {
@ -136,6 +147,27 @@ class spotify { @@ -136,6 +147,27 @@ class spotify {
}
/*
* Parse track link from a spotify url like:
*
* https://open.spotify.com/track/4UO1pfxi5fDbxshrwwznJ2?si=BtN9Yn_JQXSHGUa4CEZKvQ&utm_source=copy-link
*
*
*/
std::optional<std::string>
track_id_from_link(std::string link)
{
const std::string start = "spotify.com/track/";
auto f = link.find(start);
if (f == std::string::npos) {
return {};
}
auto end = link.find("?", f);
auto begin = f + start.length();
return link.substr(begin, end - begin);
}
};
class db {
@ -155,7 +187,7 @@ class db { @@ -155,7 +187,7 @@ class db {
conffile >> data;
}
bool add_song(std::string spotify_uri) {
bool add_song(std::string spotify_track_id) {
// request spotify api for info
// yeet into db
@ -200,16 +232,82 @@ int main() { @@ -200,16 +232,82 @@ int main() {
Bot bot(teletoken);
InlineKeyboardMarkup::Ptr keyboard(new InlineKeyboardMarkup);
std::vector<InlineKeyboardButton::Ptr> row0;
InlineKeyboardButton::Ptr button1(new InlineKeyboardButton);
button1->text = "1";
button1->callbackData = "1";
row0.push_back(button1);
InlineKeyboardButton::Ptr button2(new InlineKeyboardButton);
button2->text = "2";
button2->callbackData = "2";
row0.push_back(button2);
InlineKeyboardButton::Ptr button3(new InlineKeyboardButton);
button3->text = "3";
button3->callbackData = "3";
row0.push_back(button3);
InlineKeyboardButton::Ptr button4(new InlineKeyboardButton);
button4->text = "4";
button4->callbackData = "4";
row0.push_back(button4);
InlineKeyboardButton::Ptr button5(new InlineKeyboardButton);
button5->text = "5";
button5->callbackData= "5";
row0.push_back(button5);
keyboard->inlineKeyboard.push_back(row0);
bot.getEvents().onCallbackQuery([&bot, &keyboard](CallbackQuery::Ptr query) {
if (StringTools::startsWith(query->data, "check")) {
std::string response = "ok";
bot.getApi().sendMessage(query->message->chat->id, response, false, 0, keyboard, "Markdown");
}
});
bot.getEvents().onCommand("add", [&bot, &keyboard, s](Message::Ptr message) {
std::string link = util::trim_whitespace(message->text.substr(message->text.find("add")));
auto resp = s->track_id_from_link(link);
if (!resp) {
bot.getApi().sendMessage(message->chat->id, "Sorry, I don't understand that link.");
return;
}
auto spot_resp = s->get_track(*resp);
if (!spot_resp) {
bot.getApi().sendMessage(message->chat->id, "Sorry, I cannot find that track in spotify.");
return;
}
json track_data = *spot_resp;
std::cout << spot_resp->dump(4);
std::string title = track_data["name"];
std::string artist = track_data["artists"][0]["name"];
std::string response = "Added song: " + title + ", by " + artist;
response += "\n\r\n\r";
response += "Everyone, please rate this suggestion /5";
bot.getApi().sendMessage(message->chat->id, response, false, 0, keyboard, "Markdown");
});
bot.getEvents().onCommand("start", [&bot](Message::Ptr message) {
bot.getApi().sendMessage(message->chat->id, "Hi!");
});
bot.getEvents().onAnyMessage([&bot](Message::Ptr message) {
printf("User wrote %s\n", message->text.c_str());
if (StringTools::startsWith(message->text, "/start")) {
return;
}
bot.getApi().sendMessage(message->chat->id, "Your message is: " + message->text);
});
signal(SIGINT, [](int s) {
printf("SIGINT got\n");

Loading…
Cancel
Save