Repository navigation
Expand file tree
/
Copy pathrich_message.cpp
More file actions
57 lines (52 loc) · 2.01 KB
/
Copy pathrich_message.cpp
File metadata and controls
57 lines (52 loc) · 2.01 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
46
47
48
49
50
51
52
53
54
55
56
57
// Builds formatted text with the RichMessageBuilder and sends it. Offsets are computed for you, in UTF-16 units.
//
// Environment: TG_API_ID, TG_API_HASH, TG_SESSION
// Usage: rich_message <username|me>
#include <cstdlib>
#include <exception>
#include <iostream>
#include <stdexcept>
#include "cppgram/client.h"
#include "cppgram/rich_message.h"
int main(int argc, char** argv) {
if (argc != 2) {
std::cerr << "usage: rich_message <username|me>\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();
const cppgram::FormattedText text
= cppgram::RichMessageBuilder()
.bold("Hello ")
.italic("CppGram")
.text("\n")
.styled("nested", cppgram::RichMessageBuilder::kBold | cppgram::RichMessageBuilder::kUnderline)
.text(" ")
.link("the docs", "https://example.com/docs")
.text("\n")
.code("client.connect();")
.text("\n")
.blockquote("Quoted line")
.build();
const cppgram::Message sent = client.send_formatted(client.resolve(argv[1]), text);
std::cout << "sent message " << sent.id << "\n";
client.disconnect();
} catch (const cppgram::RichMessageError& e) {
std::cerr << "rejected before sending: " << e.what() << "\n";
return 1;
} catch (const std::exception& e) {
std::cerr << "error: " << e.what() << "\n";
return 1;
}
return 0;
}