AI prompts
base on C Telegram bot framework # BOTLIB - Telegram C bot framework
Botlib is a C framework to write Telegram bots. It is mainly the sum of two things:
1. An implementation of a subset of the Telegram bot API, wrapped in an event loop that waits for events from the Telegram API and calls our callback in the context of a new thread. The callback that implements the bot has access to various APIs to perform actions in Telegram.
2. A set of higher level wrappers for Sqlite3, JSON, and dynamic strings (SDS library).
## Why this library is written in C?
* First of all, this framework makes writing bots in C a lot more higher level than you could expect. Sqlite3 is exported as a high level API and also exported as a key-value store. Callbacks are called with a structure that already has all the informations about the incoming message, and so forth.
* In the high level languages landscape I had a few bad experiences with libraries changing APIs continuously. A bot is something you write and put online for years: I don't want to babysit code that already works. Bots written with this library will run everywhere as long as you can compile them with `make`. The only dependencies are `libcurl` and `libsqlite3`, which are basically everywhere.
* In the process of writing a few Telegram bots, I found that many requests are quite long living. Think at a bot that transcribes the audio into text, or that uses another external API to fetch information. So multiplexing is not the way to go most of the times: this is why this library uses a thread for each request. And if you use threads like that, you want each thread to be as bare metal as possible, sharing most of the state with the main thread. C is good at that, and the library is implemented so that all the threading issues are transparent for the bot writer.
* Certain bots are quite CPU intensive to run. For instance I wrote a bot that performs analysis on the financial market, and C was a good fit to do Montecaro simulations and things like that.
To give you some feeling about how bots are developed with this framework, see this trivial example, implementing a toy bot:
```c
/* ... standard includes here ... */
#include "botlib.h"
/* 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 often :) */
void handleRequest(sqlite3 *dbhandle, BotRequest *br) {
char buf[256];
char *where = br->type == TB_TYPE_PRIVATE ? "privately" : "publicly";
snprintf(buf, sizeof(buf), "I just %s received: %s", where, br->request);
int64_t sent_chat_id, sent_message_id;
botSendMessageAndGetInfo(br->target,buf,0,&sent_chat_id,&sent_message_id);
printf("Sent message IDs: chat_id:%lld message_id:%lld\n",
(long long) sent_chat_id, (long long) sent_message_id);
/* Edit message after 1 second. */
sleep(1);
snprintf(buf, sizeof(buf), "I just %s received: %s :D", where, br->request);
botEditMessageText(sent_chat_id,sent_message_id,buf);
/* Words received in this request. */
for (int j = 0; j < br->argc; j++)
printf("%d. %s | ", j, br->argv[j]);
printf("is was mentioned? %d | ", br->bot_mentioned);
if (br->mentions) {
printf("mentions: ");
for (int j = 0; j < br->num_mentions; j++)
printf("%s, ",br->mentions[j]);
}
printf("\n");
/* Show if the message has a voice file inside. */
if (br->file_type == TB_FILE_TYPE_VOICE_OGG) {
printf("Voice file ID: %s\n", br->file_id);
botGetFile(br,"audio.oga");
}
/* Let's use our key-value store API on top of Sqlite. If the
* user in a Telegram group tells "foo is bar" we will set the
* foo key to bar. Then if somebody write "foo?" and we have an
* associated key, we reply with what "foo" is. */
if (br->argc >= 3 && !strcasecmp(br->argv[1],"is")) {
kvSet(dbhandle,br->argv[0],br->request,0);
/* Note that in this case we don't use 0 as "from" field, so
* we are sending a reply to the user, not a general message
* on the channel. */
botSendMessage(br->target,"Ok, I'll remember.",br->msg_id);
}
int reqlen = strlen(br->request);
if (br->argc == 1 && reqlen && br->request[reqlen-1] == '?') {
char *copy = strdup(br->request);
copy[reqlen-1] = 0;
printf("Looking for key %s\n", copy);
sds res = kvGet(dbhandle,copy);
if (res) {
botSendMessage(br->target,res,0);
}
sdsfree(res);
free(copy);
}
}
// This is just called every 1 or 2 seconds. */
void cron(sqlite3 *dbhandle) {
UNUSED(dbhandle);
printf("."); fflush(stdout);
}
int main(int argc, char **argv) {
/* Only group messages matching this list of glob patterns
* are passed to the callback. */
static char *triggers[] = {
"Echo *",
"Hi!",
"* is *",
"*\?",
"!ls",
NULL,
};
startBot(TB_CREATE_KV_STORE, argc, argv, TB_FLAGS_NONE, handleRequest, cron, triggers);
return 0; /* Never reached. */
}
```
See further in this README file for the full API specification.
## Installation
1. Create your bot using the Telegram [@BotFather](https://t.me/botfather).
2. After obtaining your bot API key, store it into a file called `apikey.txt` inside the bot working directory. Alternatively you can use the `--apikey` command line argument to provide your Telegram API key.
3. Optionally edit `mybot.c` to personalized the bot.
3. Build the bot: you need libcurl and libsqlite installed. Just type `make`.
4. Run with `./mybot`. There is also a debug mode if you run it using the `--debug` option (add --debug multiple times for even more verbose messages). For a more moderate output use `--verbose`. Try `mybot --help` for the full list of command line options.
5. Add the bot to your Telegram channel.
6. **IMPORTANT:** The bot *must* be an administrator of the channel in order to read all the messages that are sent in such channel. Private messages will work regardless.
By default the bot will create an SQLite database in the working directory.
If you want to specify another path for your SQLite db, use the `--dbfile`
command line option.
## APIs
... Work in progress ...
For the bot API check `mybot.c` example itself. The basic usage is pretty simple. However for all the other stuff, like the Sqlite3 abstractions, they are taken from [Stonky](https://github.com/antirez/stonky), so the code there (which is very accessible) will provide some help. I hope to document this project better. For now my main goal was to stop duplicating Stonly to create new bots with all the common code inside.
", Assign "at most 3 tags" to the expected json: {"id":"5266","tags":[]} "only from the tags list I provide: [{"id":77,"name":"3d"},{"id":89,"name":"agent"},{"id":17,"name":"ai"},{"id":54,"name":"algorithm"},{"id":24,"name":"api"},{"id":44,"name":"authentication"},{"id":3,"name":"aws"},{"id":27,"name":"backend"},{"id":60,"name":"benchmark"},{"id":72,"name":"best-practices"},{"id":39,"name":"bitcoin"},{"id":37,"name":"blockchain"},{"id":1,"name":"blog"},{"id":45,"name":"bundler"},{"id":58,"name":"cache"},{"id":21,"name":"chat"},{"id":49,"name":"cicd"},{"id":4,"name":"cli"},{"id":64,"name":"cloud-native"},{"id":48,"name":"cms"},{"id":61,"name":"compiler"},{"id":68,"name":"containerization"},{"id":92,"name":"crm"},{"id":34,"name":"data"},{"id":47,"name":"database"},{"id":8,"name":"declarative-gui "},{"id":9,"name":"deploy-tool"},{"id":53,"name":"desktop-app"},{"id":6,"name":"dev-exp-lib"},{"id":59,"name":"dev-tool"},{"id":13,"name":"ecommerce"},{"id":26,"name":"editor"},{"id":66,"name":"emulator"},{"id":62,"name":"filesystem"},{"id":80,"name":"finance"},{"id":15,"name":"firmware"},{"id":73,"name":"for-fun"},{"id":2,"name":"framework"},{"id":11,"name":"frontend"},{"id":22,"name":"game"},{"id":81,"name":"game-engine "},{"id":23,"name":"graphql"},{"id":84,"name":"gui"},{"id":91,"name":"http"},{"id":5,"name":"http-client"},{"id":51,"name":"iac"},{"id":30,"name":"ide"},{"id":78,"name":"iot"},{"id":40,"name":"json"},{"id":83,"name":"julian"},{"id":38,"name":"k8s"},{"id":31,"name":"language"},{"id":10,"name":"learning-resource"},{"id":33,"name":"lib"},{"id":41,"name":"linter"},{"id":28,"name":"lms"},{"id":16,"name":"logging"},{"id":76,"name":"low-code"},{"id":90,"name":"message-queue"},{"id":42,"name":"mobile-app"},{"id":18,"name":"monitoring"},{"id":36,"name":"networking"},{"id":7,"name":"node-version"},{"id":55,"name":"nosql"},{"id":57,"name":"observability"},{"id":46,"name":"orm"},{"id":52,"name":"os"},{"id":14,"name":"parser"},{"id":74,"name":"react"},{"id":82,"name":"real-time"},{"id":56,"name":"robot"},{"id":65,"name":"runtime"},{"id":32,"name":"sdk"},{"id":71,"name":"search"},{"id":63,"name":"secrets"},{"id":25,"name":"security"},{"id":85,"name":"server"},{"id":86,"name":"serverless"},{"id":70,"name":"storage"},{"id":75,"name":"system-design"},{"id":79,"name":"terminal"},{"id":29,"name":"testing"},{"id":12,"name":"ui"},{"id":50,"name":"ux"},{"id":88,"name":"video"},{"id":20,"name":"web-app"},{"id":35,"name":"web-server"},{"id":43,"name":"webassembly"},{"id":69,"name":"workflow"},{"id":87,"name":"yaml"}]" returns me the "expected json"