Repository navigation
Expand file tree
/
Copy pathweb_app_button.cpp
More file actions
45 lines (41 loc) · 1.73 KB
/
Copy pathweb_app_button.cpp
File metadata and controls
45 lines (41 loc) · 1.73 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
// Sends a message with an inline Web App button through the MTProto client. The button opens the URL as a Mini App.
// Verify the initData the Mini App sends back with cppgram::webapp::verify before you trust it (cppgram/webapp.h).
//
// Environment: TG_API_ID, TG_API_HASH, TG_SESSION
// Usage: web_app_button <username|me> <https URL>
#include <cstdlib>
#include <exception>
#include <iostream>
#include <stdexcept>
#include <string>
#include "cppgram/client.h"
#include "cppgram/types.h"
int main(int argc, char** argv) {
if (argc != 3) {
std::cerr << "usage: web_app_button <username|me> <https URL>\n";
return 2;
}
try {
const char* id = std::getenv("TG_API_ID");
const char* hash = std::getenv("TG_API_HASH");
const char* session = std::getenv("TG_SESSION");
if (id == nullptr || hash == nullptr || session == nullptr)
throw std::runtime_error("set TG_API_ID, TG_API_HASH and TG_SESSION");
cppgram::ClientOptions options;
options.api_id = std::stoi(id);
options.api_hash = hash;
options.session_path = session;
cppgram::Client client(options);
client.connect();
// One row with one button. InlineButton::web_app is the Web App form of an inline keyboard button.
const cppgram::InlineKeyboard keyboard { { cppgram::InlineButton::web_app("Open app", argv[2]) } };
const cppgram::Message sent
= client.send_with_markup(client.resolve(argv[1]), "Tap the button to open the app.", keyboard);
std::cout << "sent message " << sent.id << "\n";
client.disconnect();
} catch (const std::exception& e) {
std::cerr << "error: " << e.what() << "\n";
return 1;
}
return 0;
}