-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathagent-beast-coro.cpp
70 lines (54 loc) · 2.56 KB
/
agent-beast-coro.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
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
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "examples_util.hpp"
#include <banana/api.hpp>
#include <banana/agent/beast.hpp>
#include <boost/asio/co_spawn.hpp>
#ifndef BOOST_ASIO_HAS_CO_AWAIT
#error Coroutine support disabled
#endif
#include <iostream>
static_assert(std::is_same_v<banana::api_result<banana::api::message_t, banana::agent::beast_coro_monadic>, boost::asio::awaitable<banana::expected<banana::api::message_t>>>);
static_assert(std::is_same_v<banana::api_result<banana::api::message_t, banana::agent::beast_coro>, boost::asio::awaitable<banana::api::message_t>>);
int main(int argc, const char** argv) {
const auto [token, target, name, message_text] = parse_input_or_fail(argc, argv);
boost::asio::io_context io_context;
boost::asio::ssl::context ctx(boost::asio::ssl::context::tlsv12_client);
int result = 0;
boost::asio::co_spawn(io_context, [&]() -> boost::asio::awaitable<void> {
banana::agent::beast_coro agent{ token, io_context, ctx };
std::cout << "bot name: " << (co_await banana::api::get_me(agent)).username.value() << "\n";
// TODO: workaround for double free bug in gcc-10.2
banana::api::send_message_args_t args{ .chat_id = target, .text = message_text + " (throwing)" };
std::cout << "message sent: " << (co_await banana::api::send_message(agent, std::move(args))).message_id << "\n";
co_return;
}, [&](std::exception_ptr e) {
try {
if (e) {
std::rethrow_exception(e);
}
}
catch (std::exception& e) {
std::cout << "exception while running throwing " << name << ": " << e.what() << "\n";
result = 1;
}
});
boost::asio::co_spawn(io_context, [&]() -> boost::asio::awaitable<void> {
banana::agent::beast_coro_monadic agent{ token, io_context, ctx };
std::cout << "bot name: " << (co_await banana::api::get_me(agent)).value().username.value() << "\n";
// TODO: workaround for double free bug in gcc-10.2
banana::api::send_message_args_t args{ .chat_id = target, .text = message_text + " (monadic)" };
std::cout << "message sent: " << (co_await banana::api::send_message(agent, std::move(args))).value().message_id << "\n";
co_return;
}, [&](std::exception_ptr e) {
try {
if (e) {
std::rethrow_exception(e);
}
}
catch (std::exception& e) {
std::cout << "exception while running monadic " << name << ": " << e.what() << "\n";
result = 1;
}
});
io_context.run();
return result;
}