Following sample creates a simple dummy bot which echoes back the messages sent to it. A more comprehensive test bot is built as testbot executable under Build/test folder. It supports various commands to test APIs, polls, custom keyboards, and multimedia sending. The executable expects and reads bot token from .token file on the same location.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <unistd.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);
{
printf("Telebot create failed\n");
return -1;
}
{
printf("Failed to get bot information\n");
return -1;
}
printf(
"ID: %d\n", me.
id);
int index, count, offset = -1;
while (1)
{
continue;
printf("Number of updates: %d\n", count);
for (index = 0; index < count; index++)
{
{
if (strstr(message.
text,
"/dice"))
{
}
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);
}
}
{
printf("Failed to send message: %d \n", ret);
}
}
}
sleep(1);
}
return 0;
}
telebot_error_e telebot_send_dice(telebot_handler_t handle, long long int chat_id, bool disable_notification, int reply_to_message_id, const char *reply_markup)
Send a dice, which will have a random value from 1 to 6.
telebot_error_e telebot_destroy(telebot_handler_t handle)
Final function to use telebo APIs.
enum telebot_update_type telebot_update_type_e
Enumerations of telegram update types.
telebot_error_e telebot_get_updates(telebot_handler_t handle, int offset, int limit, int timeout, telebot_update_type_e allowed_updates[], int allowed_updates_count, telebot_update_t **updates, int *count)
This function is used to get latest updates.
telebot_error_e telebot_get_me(telebot_handler_t handle, telebot_user_t *me)
This function is used to get information about telegram bot itself.
telebot_error_e telebot_put_me(telebot_user_t *me)
This function is used to release memory used for obtained information about telegram bot itself.
telebot_error_e telebot_put_updates(telebot_update_t *updates, int count)
This function is used to release memory used for obtained updates.
struct telebot_handler * telebot_handler_t
This is opaque object to represent a telebot handler.
Definition telebot-types.h:3489
telebot_error_e telebot_send_message(telebot_handler_t handle, long long int chat_id, const char *text, const char *parse_mode, bool disable_web_page_preview, bool disable_notification, int reply_to_message_id, const char *reply_markup)
Send text messages.
telebot_error_e
Enumerations of error code for telebot programming interface.
Definition telebot-common.h:45
telebot_error_e telebot_create(telebot_handler_t *handle, char *token)
Initial function to use telebot APIs.
@ TELEBOT_UPDATE_TYPE_MESSAGE
Definition telebot-types.h:45
@ TELEBOT_ERROR_NONE
Definition telebot-common.h:46
long long int id
Definition telebot-types.h:231
This object represents a message.
Definition telebot-types.h:1777
struct telebot_user * from
Definition telebot-types.h:1788
struct telebot_chat * chat
Definition telebot-types.h:1813
char * text
Definition telebot-types.h:1898
This object represents an incoming update.
Definition telebot-types.h:3328
int update_id
Definition telebot-types.h:3333
telebot_message_t message
Definition telebot-types.h:3342
This object represents a Telegram user or bot.
Definition telebot-types.h:178
long long int id
Definition telebot-types.h:180
char * username
Definition telebot-types.h:192
char * first_name
Definition telebot-types.h:186
This file includes all the header files of the telegram bot library.