splinter: typing that actually runs, and a testable API base
Sending 'typing' every four seconds while a turn runs was in the code and never on the screen: the API base became overridable and the URL format lost the token on the way, so the gateway could not poll at all. Fixed, and proven against a fake Telegram API with a slow provider behind it: get /bot.../getUpdates typing 00s, 04s, 08s, 12s (one per four seconds, whole turn) message <answer> (typing stops with the turn) SPLINTER_TELEGRAM_API now overrides the API base, because a gateway you can only test in production is a gateway you test in production.
This commit is contained in:
1 parent
a720b67cb0
commit
8baa24f37a
2 files changed
+17
-5
No files matched your search
Binary file not shown.
+17
-5
@@ -37,7 +37,19 @@
|
||||
#include "cmdscript.h"
|
||||
#include "splinter_plugin.h"
|
||||
|
||||
#define TG_API "https://api.telegram.org/bot"
|
||||
/*
|
||||
* Where the API lives. Overridable, because a gateway you cannot test without
|
||||
* Telegram is a gateway you only test in production.
|
||||
*/
|
||||
static const char *
|
||||
api_base(void)
|
||||
{
|
||||
const char *env = getenv("SPLINTER_TELEGRAM_API");
|
||||
|
||||
return env != NULL && *env != '\0' ? env : "https://api.telegram.org";
|
||||
}
|
||||
|
||||
|
||||
#define TG_MAX_CHUNK 3500
|
||||
#define TG_POLL_S 25
|
||||
|
||||
@@ -63,7 +75,7 @@ typing_thread(void *arg)
|
||||
if (sp_buf_init(&url) != 0 || sp_buf_init(&reply) != 0 ||
|
||||
sp_buf_init(&err) != 0)
|
||||
return NULL;
|
||||
sp_buf_printf(&url, "%s%s/sendChatAction", TG_API, t->token);
|
||||
sp_buf_printf(&url, "%s/bot%s/sendChatAction", api_base(), t->token);
|
||||
{
|
||||
struct sp_buf body;
|
||||
|
||||
@@ -187,7 +199,7 @@ send_message(const char *token, long chat_id, const char *text,
|
||||
if (sp_buf_init(&url) != 0 || sp_buf_init(&body) != 0 ||
|
||||
sp_buf_init(&reply) != 0)
|
||||
return -1;
|
||||
sp_buf_printf(&url, "%s%s/sendMessage", TG_API, token);
|
||||
sp_buf_printf(&url, "%s/bot%s/sendMessage", api_base(), token);
|
||||
do {
|
||||
size_t take = len - off;
|
||||
|
||||
@@ -265,8 +277,8 @@ sp_telegram_run(struct sp_agent *ag, struct sp_host *host, const char *token,
|
||||
int ok;
|
||||
|
||||
sp_buf_clear(&url);
|
||||
sp_buf_printf(&url, "%s%s/getUpdates?timeout=%d&offset=%ld",
|
||||
TG_API, token, TG_POLL_S, offset);
|
||||
sp_buf_printf(&url, "%s/bot%s/getUpdates?timeout=%d&offset=%ld",
|
||||
api_base(), token, TG_POLL_S, offset);
|
||||
sp_buf_clear(&reply);
|
||||
if (tg_curl(url.p, NULL, &reply, err) != 0) {
|
||||
fprintf(stderr, "splinter: %s\n",
|
||||
|
||||
Reference in new issue
Block a user