-
Notifications
You must be signed in to change notification settings - Fork 17
/
02-basic.cpp
53 lines (44 loc) · 1.56 KB
/
02-basic.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
#include "slacking.hpp"
#include <fstream>
#include <functional>
// A class which handles a slack instance by reference
class Foo {
std::reference_wrapper<slack::Slacking> slack_;
// Or
// Slacking& slack_;
public:
Foo(slack::Slacking& slack) : slack_{slack} {
slack_.get().chat.postMessage("Foo class ctor"); // parameters channel, username, iconemoji reused from bar()
}
};
// Pass by ref in parameter function
void bar(slack::Slacking& slack) {
// You can try catch API method if an error occur
try {
slack.chat.channel_username_iconemoji("#mychannel", "botname", ":bird:"); // for lazy people
slack.chat.postMessage("bar() function is called");
}
catch(std::exception const& e) {
std::cerr << e.what() << '\n';
}
}
int main() {
// Load the token from a file. You can also specify directly the token in your code as a string.
std::string mytoken;
std::ifstream infile("token.txt");
std::getline(infile, mytoken);
// Create slack instance
slack::Slacking slack_instance{mytoken};
bar(slack_instance);
Foo foo{slack_instance};
// You can create other slack instances with different tokens and parameters
{
slack::Slacking another_slack_instance{"xxxx-xxxxxxxxx-xxxx"};
try {
another_slack_instance.chat.postMessage("this should throw since token is invalid", "#channelisnecessaryhere");
}
catch(std::exception const& e) {
std::cerr << "02-basic failed purposely because of " << e.what() << '\n';
}
}
}