commit 2db57c9

default  ·  2023-02-07 06:37:23 +0000 UTC
parent f6d5135
Added internals for Telegram notifications.
3 files changed,  +75, -29
M data.c
M snac.h
+58, -29
  1@@ -771,13 +771,8 @@ xs_dict *msg_note(snac *snac, xs_str *content, xs_val *rcpts, xs_str *in_reply_t
  2 void notify(snac *snac, char *type, char *utype, char *actor, char *msg)
  3 /* notifies the user of relevant events */
  4 {
  5-    char *email  = xs_dict_get(snac->config, "email");
  6     char *object = NULL;
  7 
  8-    /* no email address? done */
  9-    if (xs_is_null(email) || *email == '\0')
 10-        return;
 11-
 12     if (strcmp(type, "Create") == 0) {
 13         /* only notify of notes specifically for us */
 14         xs *rcpts = recipient_list(snac, msg, 0);
 15@@ -804,21 +799,8 @@ void notify(snac *snac, char *type, char *utype, char *actor, char *msg)
 16         }
 17     }
 18 
 19-    snac_debug(snac, 1, xs_fmt("notify(%s, %s, %s)", type, utype, actor));
 20-
 21-    /* prepare message */
 22-
 23-    xs *subject = xs_fmt("snac notify for @%s@%s",
 24-                    xs_dict_get(snac->config, "uid"), xs_dict_get(srv_config, "host"));
 25-    xs *from    = xs_fmt("snac-daemon <snac-daemon@%s>", xs_dict_get(srv_config, "host"));
 26-    xs *header  = xs_fmt(
 27-                    "From: %s\n"
 28-                    "To: %s\n"
 29-                    "Subject: %s\n"
 30-                    "\n",
 31-                    from, email, subject);
 32-
 33-    xs *body = xs_str_new(header);
 34+    /* prepare message body */
 35+    xs *body = xs_str_new(NULL);
 36 
 37     if (strcmp(utype, "(null)") != 0) {
 38         xs *s1 = xs_fmt("Type  : %s + %s\n", type, utype);
 39@@ -839,7 +821,35 @@ void notify(snac *snac, char *type, char *utype, char *actor, char *msg)
 40         body = xs_str_cat(body, s1);
 41     }
 42 
 43-    enqueue_email(body, 0);
 44+    /* email */
 45+
 46+    char *email = xs_dict_get(snac->config, "email");
 47+
 48+    if (!xs_is_null(email) && *email != '\0') {
 49+        snac_debug(snac, 1, xs_fmt("email notify %s %s %s", type, utype, actor));
 50+
 51+        xs *subject = xs_fmt("snac notify for @%s@%s",
 52+                    xs_dict_get(snac->config, "uid"), xs_dict_get(srv_config, "host"));
 53+        xs *from    = xs_fmt("snac-daemon <snac-daemon@%s>", xs_dict_get(srv_config, "host"));
 54+        xs *header  = xs_fmt(
 55+                    "From: %s\n"
 56+                    "To: %s\n"
 57+                    "Subject: %s\n"
 58+                    "\n",
 59+                    from, email, subject);
 60+
 61+        xs *email_body = xs_fmt("%s%s", header, body);
 62+
 63+        enqueue_email(email_body, 0);
 64+    }
 65+
 66+    /* telegram */
 67+
 68+    char *bot     = xs_dict_get(snac->config, "telegram_bot");
 69+    char *chat_id = xs_dict_get(snac->config, "telegram_chat_id");
 70+
 71+    if (!xs_is_null(bot) && !xs_is_null(chat_id) && *bot && *chat_id)
 72+        enqueue_telegram(body, bot, chat_id);
 73 }
 74 
 75 
 76@@ -1178,17 +1188,40 @@ void process_queue_item(xs_dict *q_item)
 77             retries++;
 78 
 79             if (retries > queue_retry_max)
 80-                srv_log(xs_fmt("process_queue email giving up (errno: %d)", errno));
 81+                srv_log(xs_fmt("email giving up (errno: %d)", errno));
 82             else {
 83                 /* requeue */
 84                 srv_log(xs_fmt(
 85-                    "process_queue email requeue #%d (errno: %d)", retries, errno));
 86+                    "email requeue #%d (errno: %d)", retries, errno));
 87 
 88                 enqueue_email(msg, retries);
 89             }
 90         }
 91     }
 92     else
 93+    if (strcmp(type, "telegram") == 0) {
 94+        /* send this via telegram */
 95+        char *bot   = xs_dict_get(q_item, "bot");
 96+        char *msg   = xs_dict_get(q_item, "message");
 97+        xs *chat_id = xs_dup(xs_dict_get(q_item, "chat_id"));
 98+        int status  = 0;
 99+
100+        /* chat_id must start with a - */
101+        if (!xs_startswith(chat_id, "-"))
102+            chat_id = xs_str_wrap_i("-", chat_id, NULL);
103+
104+        xs *url  = xs_fmt("https:/" "/api.telegram.org/bot%s/sendMessage", bot);
105+        xs *body = xs_fmt("{\"chat_id\":%s,\"text\":\"%s\"}", chat_id, msg);
106+
107+        xs *headers = xs_dict_new();
108+        headers = xs_dict_append(headers, "content-type", "application/json");
109+
110+        xs *rsp  = xs_http_request("POST", url, headers,
111+                                   body, strlen(body), &status, NULL, NULL, 0);
112+
113+        srv_debug(0, xs_fmt("telegram post %d", status));
114+    }
115+    else
116     if (strcmp(type, "purge") == 0) {
117         srv_log(xs_dup("purge start"));
118 
119@@ -1210,12 +1243,8 @@ void process_queue(void)
120     while (xs_list_iter(&p, &fn)) {
121         xs *q_item = dequeue(fn);
122 
123-        if (q_item == NULL) {
124-            srv_log(xs_fmt("process_queue q_item error"));
125-            continue;
126-        }
127-
128-        job_post(q_item);
129+        if (q_item != NULL)
130+            job_post(q_item);
131     }
132 }
133 
M data.c
+16, -0
 1@@ -1420,6 +1420,22 @@ void enqueue_email(xs_str *msg, int retries)
 2 }
 3 
 4 
 5+void enqueue_telegram(const xs_str *msg, const char *bot, const char *chat_id)
 6+/* enqueues a message to be sent via Telegram */
 7+{
 8+    xs *qmsg   = _new_qmsg("telegram", msg, 0);
 9+    char *ntid = xs_dict_get(qmsg, "ntid");
10+    xs *fn     = xs_fmt("%s/queue/%s.json", srv_basedir, ntid);
11+
12+    qmsg = xs_dict_append(qmsg, "bot",      bot);
13+    qmsg = xs_dict_append(qmsg, "chat_id",  chat_id);
14+
15+    qmsg = _enqueue_put(fn, qmsg);
16+
17+    srv_debug(1, xs_fmt("enqueue_email %s %s", bot, chat_id));
18+}
19+
20+
21 void enqueue_message(snac *snac, xs_dict *msg)
22 /* enqueues an output message */
23 {
M snac.h
+1, -0
1@@ -128,6 +128,7 @@ void enqueue_input(snac *snac, xs_dict *msg, xs_dict *req, int retries);
2 void enqueue_output(snac *snac, xs_dict *msg, xs_str *inbox, int retries);
3 void enqueue_output_by_actor(snac *snac, xs_dict *msg, xs_str *actor, int retries);
4 void enqueue_email(xs_str *msg, int retries);
5+void enqueue_telegram(const xs_str *msg, const char *bot, const char *chat_id);
6 void enqueue_message(snac *snac, char *msg);
7 
8 xs_list *user_queue(snac *snac);