slack_messenger.h
Go to the documentation of this file.
1 /*
2  -------------------------------------------------------------------
3 
4  Copyright (C) 2020, Andrew W. Steiner
5 
6  This file is part of O2scl.
7 
8  O2scl is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version.
12 
13  O2scl is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with O2scl. If not, see <http://www.gnu.org/licenses/>.
20 
21  -------------------------------------------------------------------
22 */
23 /** \file slack_messenger.h
24  \brief File defining \ref o2scl::slack_messenger
25 */
26 #ifndef O2SCL_SLACK_MESSENGER_H
27 #define O2SCL_SLACK_MESSENGER_H
28 
29 #ifndef DOXYGEN_NO_O2NS
30 namespace o2scl {
31 #endif
32 
33  /** \brief Object to send messages to Slack using curl
34 
35  \note Experimental
36  */
38 
39  protected:
40 
41  /** \brief Time (in seconds) the last message was sent
42  */
44 
45  /** \brief If true, use MPI to determine time
46  */
47  bool mpi_time;
48 
49  public:
50 
51  /** \brief The URL for the Slack webhook
52  */
53  std::string url;
54 
55  /** \brief The destination channel
56  */
57  std::string channel;
58 
59  /** \brief Minimum time between messages in seconds (default 300)
60  */
62 
63  /** \brief Icon to use (without colons; default "computer")
64  */
65  std::string icon;
66 
67  /** \brief Slack username
68  */
69  std::string username;
70 
71  /** \brief Verbosity parameter (default 1)
72  */
73  int verbose;
74 
75  /** \brief Create a messenger object with specified channel,
76  username, URL and time method
77 
78  If the URL is not specified, this constructor tries to determine
79  it from the environment variable \c O2SCL_SLACK_URL .
80  */
81  slack_messenger(std::string p_channel="", std::string p_username="",
82  std::string p_url="", bool p_mpi_time=false) {
83 
84  if (p_url.length()==0) {
85  set_url_from_env("O2SCL_SLACK_URL");
86  } else {
87  url=p_url;
88  }
89  channel=p_channel;
90  username=p_username;
91  min_time_between=300.0;
92  icon="computer";
93  verbose=1;
94  mpi_time=p_mpi_time;
95  if (mpi_time) {
96 #ifdef O2SCL_MPI
97  time_last_message=MPI_Wtime()-min_time_between-1.0;
98 #else
99  O2SCL_ERR2("Value mpi_time is true but O2SCL_MPI not defined ",
100  "in slack_messenger::slack_messenger().",
102 #endif
103  } else {
105  }
106  }
107 
108  /** \brief Set the time mode (normal or MPI)
109  */
110  void set_time_mode(bool loc_mpi_time) {
111  if (mpi_time) {
112 #ifdef O2SCL_MPI
113  time_last_message=MPI_Wtime();
114 #else
115  O2SCL_ERR2("Value mpi_time is true but O2SCL_MPI not defined ",
116  "in slack_message::slack_message().",
118 #endif
119  } else {
120  time_last_message=time(0);
121  }
122  mpi_time=loc_mpi_time;
123  return;
124  }
125 
126  /** \brief Set the Slack webhook URL from an environment variable
127  */
128  bool set_url_from_env(std::string env_var) {
129  char *cstring=getenv(env_var.c_str());
130  if (cstring) {
131  url=cstring;
132  return true;
133  }
134  return false;
135  }
136 
137  bool set_channel_from_env(std::string env_var) {
138  char *cstring=getenv(env_var.c_str());
139  if (cstring) {
140  channel=cstring;
141  return true;
142  }
143  return false;
144  }
145 
146  /** \brief Desc
147  */
148  bool set_username_from_env(std::string env_var) {
149  char *cstring=getenv(env_var.c_str());
150  if (cstring) {
151  username=cstring;
152  return true;
153  }
154  return false;
155  }
156 
157  /** \brief Send a message
158  */
159  int send(std::string message, bool err_on_fail=true) {
160 
161  int iret=0;
162 
163  if (url.length()>0) {
164 
165  if (channel.length()==0) {
166  O2SCL_ERR2("No slack channel specified in ",
167  "slack_messenger::send().",o2scl::exc_einval);
168  }
169  if (username.length()==0) {
170  O2SCL_ERR2("No slack username specified in ",
171  "slack_messenger::send().",o2scl::exc_einval);
172  }
173 
174  double time_now=time_last_message;
175  if (mpi_time) {
176 #ifdef O2SCL_MPI
177  time_now=MPI_Wtime();
178 #else
179  O2SCL_ERR2("Value mpi_time is true but O2SCL_MPI not defined ",
180  "in slack_message::slack_message().",
182 #endif
183  } else {
184  time_now=time(0);
185  }
186 
187  if (time_now-time_last_message>min_time_between) {
188 
189  std::string scr;
190  if (icon.length()>0) {
191  scr=((std::string)"curl -X POST --data-urlencode ")+
192  "\"payload={\\\"channel\\\": \\\""+channel+"\\\", "+
193  "\\\"username\\\": \\\""+username+"\\\", "+
194  "\\\"text\\\": \\\""+message+"\\\", "+
195  "\\\"icon_emoji\\\": \\\":"+icon+":\\\"}\" "+url;
196  } else {
197  scr=((std::string)"curl -X POST --data-urlencode ")+
198  "\"payload={\\\"channel\\\": \\\""+channel+"\\\", "+
199  "\\\"username\\\": \\\""+username+"\\\", "+
200  "\\\"text\\\": \\\""+message+"\\\"}\" "+url;
201  }
202 
203  if (verbose>1) {
204  std::cout << "Executing: " << scr << std::endl;
205  }
206 
207  iret=system(scr.c_str());
208 
209  if (iret!=0 && err_on_fail) {
210  O2SCL_ERR2("System command failed in ",
211  "slack_messenger::send().",o2scl::exc_efailed);
212  }
213 
214  time_last_message=time_now;
215 
216  }
217 
218  } else {
219  O2SCL_ERR2("No slack URL specified in ",
220  "slack_messenger::send().",o2scl::exc_einval);
221  }
222 
223  return iret;
224  }
225 
226  };
227 
228 #ifndef DOXYGEN_NO_O2NS
229 }
230 #endif
231 
232 #endif
233 
o2scl::slack_messenger::username
std::string username
Slack username.
Definition: slack_messenger.h:69
o2scl::slack_messenger::set_url_from_env
bool set_url_from_env(std::string env_var)
Set the Slack webhook URL from an environment variable.
Definition: slack_messenger.h:128
o2scl::exc_efailed
@ exc_efailed
generic failure
Definition: err_hnd.h:61
O2SCL_ERR2
#define O2SCL_ERR2(d, d2, n)
Set an error, two-string version.
Definition: err_hnd.h:281
o2scl::slack_messenger
Object to send messages to Slack using curl.
Definition: slack_messenger.h:37
o2scl
The main O<span style='position: relative; top: 0.3em; font-size: 0.8em'>2</span>scl O$_2$scl names...
Definition: anneal.h:42
o2scl::slack_messenger::mpi_time
bool mpi_time
If true, use MPI to determine time.
Definition: slack_messenger.h:47
o2scl::slack_messenger::icon
std::string icon
Icon to use (without colons; default "computer")
Definition: slack_messenger.h:65
o2scl::slack_messenger::set_time_mode
void set_time_mode(bool loc_mpi_time)
Set the time mode (normal or MPI)
Definition: slack_messenger.h:110
o2scl::slack_messenger::time_last_message
double time_last_message
Time (in seconds) the last message was sent.
Definition: slack_messenger.h:43
o2scl::slack_messenger::min_time_between
double min_time_between
Minimum time between messages in seconds (default 300)
Definition: slack_messenger.h:61
o2scl::slack_messenger::slack_messenger
slack_messenger(std::string p_channel="", std::string p_username="", std::string p_url="", bool p_mpi_time=false)
Create a messenger object with specified channel, username, URL and time method.
Definition: slack_messenger.h:81
o2scl::exc_einval
@ exc_einval
invalid argument supplied by user
Definition: err_hnd.h:59
o2scl::slack_messenger::url
std::string url
The URL for the Slack webhook.
Definition: slack_messenger.h:53
o2scl::slack_messenger::verbose
int verbose
Verbosity parameter (default 1)
Definition: slack_messenger.h:73
o2scl::slack_messenger::channel
std::string channel
The destination channel.
Definition: slack_messenger.h:57
o2scl::slack_messenger::send
int send(std::string message, bool err_on_fail=true)
Send a message.
Definition: slack_messenger.h:159
o2scl::slack_messenger::set_username_from_env
bool set_username_from_env(std::string env_var)
Desc.
Definition: slack_messenger.h:148

Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).