MCPcopy Index your code
hub / github.com/antirez/botlib / botProcessUpdates

Function botProcessUpdates

botlib.c:655–832  ·  view source on GitHub ↗

Get the updates from the Telegram API, process them, and return the * ID of the highest processed update. * * The offset is the last ID already processed, the timeout is the number * of seconds to wait in long polling in case no request is immediately * available. */

Source from the content-addressed store, hash-verified

653 * of seconds to wait in long polling in case no request is immediately
654 * available. */
655int64_t botProcessUpdates(int64_t offset, int timeout) {
656 char *options[6];
657 int res;
658
659 options[0] = "offset";
660 options[1] = sdsfromlonglong(offset+1);
661 options[2] = "timeout";
662 options[3] = sdsfromlonglong(timeout);
663 options[4] = "allowed_updates";
664 options[5] = "message";
665 sds body = makeGETBotRequest("getUpdates",&res,options,3);
666 sdsfree(options[1]);
667 sdsfree(options[3]);
668
669 /* If two --debug options are provided, log the whole Telegram
670 * reply here. */
671 if (Bot.debug >= 2)
672 printf("RECEIVED FROM TELEGRAM API:\n%s\n",body);
673
674 /* Parse the JSON in order to extract the message info. */
675 cJSON *json = cJSON_Parse(body);
676 cJSON *result = cJSON_Select(json,".result:a");
677 if (result == NULL) goto fmterr;
678 /* Process the array of updates. */
679 cJSON *update;
680 cJSON_ArrayForEach(update,result) {
681 cJSON *update_id = cJSON_Select(update,".update_id:n");
682 if (update_id == NULL) continue;
683 int64_t thisoff = (int64_t) update_id->valuedouble;
684 if (thisoff > offset) offset = thisoff;
685
686 /* The actual message may be stored in .message or .channel_post
687 * depending on the fact this is a private or group message,
688 * or, instead, a channel post. */
689 cJSON *msg = cJSON_Select(update,".message");
690 if (!msg) msg = cJSON_Select(update,".channel_post");
691 if (!msg) continue;
692
693 cJSON *chatid = cJSON_Select(msg,".chat.id:n");
694 if (chatid == NULL) continue;
695 int64_t target = (int64_t) chatid->valuedouble;
696
697 cJSON *fromid = cJSON_Select(msg,".from.id:n");
698 int64_t from = fromid ? (int64_t) fromid->valuedouble : 0;
699
700 cJSON *fromuser = cJSON_Select(msg,".from.username:s");
701 char *from_username = fromuser ? fromuser->valuestring : "unknown";
702
703 cJSON *msgid = cJSON_Select(msg,".message_id:n");
704 int64_t message_id = msgid ? (int64_t) msgid->valuedouble : 0;
705
706 cJSON *chattype = cJSON_Select(msg,".chat.type:s");
707 char *ct = chattype->valuestring;
708 int type = TB_TYPE_UNKNOWN;
709 if (ct != NULL) {
710 if (!strcmp(ct,"private")) type = TB_TYPE_PRIVATE;
711 else if (!strcmp(ct,"group")) type = TB_TYPE_GROUP;
712 else if (!strcmp(ct,"supergroup")) type = TB_TYPE_SUPERGROUP;

Callers 1

botMainFunction · 0.85

Calls 13

sdsfromlonglongFunction · 0.85
makeGETBotRequestFunction · 0.85
sdsfreeFunction · 0.85
cJSON_ParseFunction · 0.85
cJSON_SelectFunction · 0.85
strmatchFunction · 0.85
sdsnewFunction · 0.85
createBotRequestFunction · 0.85
sdslenFunction · 0.85
sdsnewlenFunction · 0.85
xreallocFunction · 0.85
freeBotRequestFunction · 0.85

Tested by

no test coverage detected