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
|
#ifndef __SLACKALERTER_HPP__
# define __SLACKALERTER_HPP__
# include <string>
# include <stdarg.h>
# include "ialerter.hpp"
# include "values.hpp"
class SlackAlerter : public IAlerter
{
/* Variables */
private:
slack::Slacking &_handle;
std::string _channel, _emoji, _username;
/* Methods */
public:
SlackAlerter() = delete;
//-----------------------------------------------------
SlackAlerter(const std::string &token, const std::string &channel = "", const std::string &username = "", const std::string &emoji = "") : _handle(slack::create(token)), _channel(channel), _emoji(emoji), _username(username)
//-----------------------------------------------------
{
}
//-----------------------------------------------------
~SlackAlerter()
//-----------------------------------------------------
{
}
//-----------------------------------------------------
void sendMessage(const std::string &format, ...) const
//-----------------------------------------------------
{
va_list args;
char message[MAX_BUFFER_LENGTH];
va_start(args, format);
vsnprintf(message, MAX_BUFFER_LENGTH - 1, format.c_str(), args);
try {
this->_handle.post("chat.postMessage",
{
{ "channel", this->_channel },
{ "username", this->_username },
{ "icon_emoji", this->_emoji },
{ "text", message }
});
} catch (const std::runtime_error &err) {
va_end(args);
throw;
}
va_end(args);
}
};
#endif // !__SLACKALERTER_HPP__ |
Partager