For each bot command, private message or group message (but this only works * if the bot is set as group admin), this function is called in a new thread, * with its private state, sqlite database handle and so forth. * * For group messages, this function is ONLY called if one of the patterns * specified as "triggers" in startBot() matched the message. Otherwise we * would spawn threads too o
| 19 | * specified as "triggers" in startBot() matched the message. Otherwise we |
| 20 | * would spawn threads too often :) */ |
| 21 | void handleRequest(sqlite3 *dbhandle, BotRequest *br) { |
| 22 | char buf[256]; |
| 23 | char *where = br->type == TB_TYPE_PRIVATE ? "privately" : "publicly"; |
| 24 | snprintf(buf, sizeof(buf), "I just %s received: %s", where, br->request); |
| 25 | |
| 26 | int64_t sent_chat_id, sent_message_id; |
| 27 | botSendMessageAndGetInfo(br->target,buf,0,&sent_chat_id,&sent_message_id); |
| 28 | printf("Sent message IDs: chat_id:%lld message_id:%lld\n", |
| 29 | (long long) sent_chat_id, (long long) sent_message_id); |
| 30 | |
| 31 | /* Edit message after 1 second. */ |
| 32 | sleep(1); |
| 33 | snprintf(buf, sizeof(buf), "I just %s received: %s :D", where, br->request); |
| 34 | botEditMessageText(sent_chat_id,sent_message_id,buf); |
| 35 | |
| 36 | /* Words received in this request. */ |
| 37 | for (int j = 0; j < br->argc; j++) |
| 38 | printf("%d. %s | ", j, br->argv[j]); |
| 39 | printf("is was mentioned? %d | ", br->bot_mentioned); |
| 40 | if (br->mentions) { |
| 41 | printf("mentions: "); |
| 42 | for (int j = 0; j < br->num_mentions; j++) |
| 43 | printf("%s, ",br->mentions[j]); |
| 44 | } |
| 45 | printf("\n"); |
| 46 | |
| 47 | /* Show if the message has a voice file inside. */ |
| 48 | if (br->file_type == TB_FILE_TYPE_VOICE_OGG) { |
| 49 | printf("Voice file ID: %s\n", br->file_id); |
| 50 | botGetFile(br,"audio.oga"); |
| 51 | } |
| 52 | |
| 53 | /* Let's use our key-value store API on top of Sqlite. If the |
| 54 | * user in a Telegram group tells "foo is bar" we will set the |
| 55 | * foo key to bar. Then if somebody write "foo?" and we have an |
| 56 | * associated key, we reply with what "foo" is. */ |
| 57 | if (br->argc >= 3 && !strcasecmp(br->argv[1],"is")) { |
| 58 | kvSet(dbhandle,br->argv[0],br->request,0); |
| 59 | /* Note that in this case we don't use 0 as "from" field, so |
| 60 | * we are sending a reply to the user, not a general message |
| 61 | * on the channel. */ |
| 62 | botSendMessage(br->target,"Ok, I'll remember.",br->msg_id); |
| 63 | } |
| 64 | |
| 65 | int reqlen = strlen(br->request); |
| 66 | if (br->argc == 1 && reqlen && br->request[reqlen-1] == '?') { |
| 67 | char *copy = strdup(br->request); |
| 68 | copy[reqlen-1] = 0; |
| 69 | printf("Looking for key %s\n", copy); |
| 70 | sds res = kvGet(dbhandle,copy); |
| 71 | if (res) { |
| 72 | botSendMessage(br->target,res,0); |
| 73 | } |
| 74 | sdsfree(res); |
| 75 | free(copy); |
| 76 | } |
| 77 | } |
| 78 |
nothing calls this directly
no test coverage detected