Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 84 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,43 +1,86 @@
# Telebot

![Build](https://github.com/smartnode/telebot/workflows/Build/badge.svg)
![GitHub repo size](https://img.shields.io/github/repo-size/smartnode/telebot)
![GitHub issues](https://img.shields.io/github/issues/smartnode/telebot)
![GitHub pull requests](https://img.shields.io/github/issues-pr/smartnode/telebot)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)

# Telebot
C Library for Telegram bot API that uses json-c and libcurl.
Head over to [Telegram Bots](https://core.telegram.org/bots) page to read about how to register your brand new bot with Telegram platform and see it in action using telebot.

## Building

You need to install libraries and build tools such as CMake.
On Debian-based Linux distributions you can do it as follows:

```sh
sudo apt-get install libcurl4-openssl-dev libjson-c-dev cmake binutils make
```

On Mac OSX, first install macports from [MacPorts](https://www.macports.org/install.php) and in Terminal

```sh
sudo port install cmake json-c curl
```

To build the library run following commands:

```sh
cd [your repository]
mkdir -p Build && cd Build
cmake ../
make
```

## Sample
<details>
<summary>Sample</summary>

Following sample creates a simple dummy bot which echoes back the messages sent to it.
The [same example](test/echobot) is built as `echobot` executable under `Build/test` folder.
The [same example](test/echobot.c) is built as `echobot` executable under `Build/test` folder. The
executable expects and reads bot token from `.token` file on the same location.

```c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
#include <telebot.h>

#define SIZE_OF_ARRAY(array) (sizeof(array) / sizeof(array[0]))

int main(int argc, char *argv[])
{
printf("Welcome to Echobot\n");

FILE *fp = fopen(".token", "r");
if (fp == NULL)
{
printf("Failed to open .token file\n");
return -1;
}

char token[1024];
if (fscanf(fp, "%s", token) == 0)
{
printf("Failed to read token\n");
fclose(fp);
return -1;
}
printf("Token: %s\n", token);
fclose(fp);

telebot_handler_t handle;
if (telebot_create(&handle, token) != TELEBOT_ERROR_NONE) {
if (telebot_create(&handle, token) != TELEBOT_ERROR_NONE)
{
printf("Telebot create failed\n");
return -1;
}

telebot_user_t me;
if (telebot_get_me(handle, &me) != TELEBOT_ERROR_NONE) {
if (telebot_get_me(handle, &me) != TELEBOT_ERROR_NONE)
{
printf("Failed to get bot information\n");
telebot_destroy(handle);
return -1;
Expand All @@ -49,35 +92,57 @@ The [same example](test/echobot) is built as `echobot` executable under `Build/t

telebot_put_me(&me);

int index, count, offset = 1;
char str[4096];
int index, count, offset = -1;
telebot_error_e ret;
telebot_message_t message;
telebot_update_type_e update_types[] = {TELEBOT_UPDATE_TYPE_MESSAGE};

while (1) {
while (1)
{
telebot_update_t *updates;
ret = telebot_get_updates(handle, offset, 20, 0, NULL, 0, &updates, &count);
ret = telebot_get_updates(handle, offset, 20, 0, update_types, 0, &updates, &count);
if (ret != TELEBOT_ERROR_NONE)
continue;
printf("Number of updates: %d\n", count);
for (index = 0; index < count; index++) {
if (updates[index].update_type == TELEBOT_UPDATE_TYPE_MESSAGE) {
message = updates[index].message;
for (index = 0; index < count; index++)
{
message = updates[index].message;
if (message.text)
{
printf("%s: %s \n", message.from->first_name, message.text);
if (strstr(message.text, "/start")) {
snprintf(str, SIZE_OF_ARRAY(str), "Hello %s", message.from->first_name);
} else {
snprintf(str, SIZE_OF_ARRAY(str), "RE:%s", message.text);
if (strstr(message.text, "/dice"))
{
telebot_send_dice(handle, message.chat->id, false, 0, "");
}
ret = telebot_send_message(handle, message.chat->id, str, "", false, false, 0, "");
if (ret != TELEBOT_ERROR_NONE) {
else
{
char str[4096];
if (strstr(message.text, "/start"))
{
snprintf(str, SIZE_OF_ARRAY(str), "Hello %s", message.from->first_name);
}
else
{
snprintf(str, SIZE_OF_ARRAY(str), "<i>%s</i>", message.text);
}
ret = telebot_send_message(handle, message.chat->id, str, "HTML", false, false, updates[index].message.message_id, "");
}
if (ret != TELEBOT_ERROR_NONE)
{
printf("Failed to send message: %d \n", ret);
}
}
offset = updates[index].update_id + 1;
}
telebot_put_updates(updates, count);

sleep(1);
}

telebot_destroy(handle);
```

return 0;
}
```

</details>
12 changes: 10 additions & 2 deletions include/telebot-methods.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,16 @@ telebot_error_e telebot_get_proxy(telebot_handler_t handle, char **addr);
* @brief This function is used to get latest updates.
*
* @param[in] handle The telebot handler created with #telebot_create().
* @param[in] allowed_updates Types of updates you want your bot to receive.
* Referes #telebot_update_type_e.
* @param[in] offset Identifier of the first update to be returned. The
* negative offset can be specified to retrieve updates starting from -offset
* update from the end of the updates queue.
* @param[in] limit Number of updates to be retrieved. Values between 1-100
* are accepted. Defaults to 100.
* @param[in] timeout Timeout in seconds for long polling.
* Defaults to 0, i.e. usual short polling. Should be positive,
* short polling should be used for testing purposes only.
* @param[in] allowed_updates An array of types of updates you want your bot to receive.
* Refers to #telebot_update_type_e.
* @param[in] allowed_updates_count Number of update types.
* @param[out] updates An array of update objects, it needs to be released with
* #telebot_put_updates after use.
Expand Down
2 changes: 1 addition & 1 deletion src/telebot-parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ telebot_error_e telebot_parser_get_message(struct json_object *obj, telebot_mess
if (json_object_object_get_ex(obj, "dice", &dice))
{
msg->dice = malloc(sizeof(telebot_dice_t));
if (telebot_parser_get_dice(poll, msg->dice) != TELEBOT_ERROR_NONE)
if (telebot_parser_get_dice(dice, msg->dice) != TELEBOT_ERROR_NONE)
{
ERR("Failed to get <dice> from message object");
TELEBOT_SAFE_FREE(msg->dice);
Expand Down
30 changes: 19 additions & 11 deletions test/echobot.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ int main(int argc, char *argv[])
FILE *fp = fopen(".token", "r");
if (fp == NULL)
{
printf("Failed to open token file\n");
printf("Failed to open .token file\n");
return -1;
}

char token[1024];
if (fscanf(fp, "%s", token) == 0)
{
printf("Reading token failed");
printf("Failed to read token\n");
fclose(fp);
return -1;
}
Expand Down Expand Up @@ -49,33 +49,41 @@ int main(int argc, char *argv[])

telebot_put_me(&me);

int index, count, offset = 1;
char str[4096];
int index, count, offset = -1;
telebot_error_e ret;
telebot_message_t message;
telebot_update_type_e update_types[] = {TELEBOT_UPDATE_TYPE_MESSAGE};

while (1)
{
telebot_update_t *updates;
ret = telebot_get_updates(handle, offset, 20, 0, NULL, 0, &updates, &count);
ret = telebot_get_updates(handle, offset, 20, 0, update_types, 0, &updates, &count);
if (ret != TELEBOT_ERROR_NONE)
continue;
printf("Number of updates: %d\n", count);
for (index = 0; index < count; index++)
{
if (updates[index].update_type == TELEBOT_UPDATE_TYPE_MESSAGE)
message = updates[index].message;
if (message.text)
{
message = updates[index].message;
printf("%s: %s \n", message.from->first_name, message.text);
if (strstr(message.text, "/start"))
if (strstr(message.text, "/dice"))
{
snprintf(str, SIZE_OF_ARRAY(str), "Hello %s", message.from->first_name);
telebot_send_dice(handle, message.chat->id, false, 0, "");
}
else
{
snprintf(str, SIZE_OF_ARRAY(str), "RE:%s", message.text);
char str[4096];
if (strstr(message.text, "/start"))
{
snprintf(str, SIZE_OF_ARRAY(str), "Hello %s", message.from->first_name);
}
else
{
snprintf(str, SIZE_OF_ARRAY(str), "<i>%s</i>", message.text);
}
ret = telebot_send_message(handle, message.chat->id, str, "HTML", false, false, updates[index].message.message_id, "");
}
ret = telebot_send_message(handle, message.chat->id, str, "", false, false, 0, "");
if (ret != TELEBOT_ERROR_NONE)
{
printf("Failed to send message: %d \n", ret);
Expand Down