Files
FoxLang/telegram_bot/bot_api.fox
T

123 lines
3.9 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// bot_api.fox - API для работы с Telegram Bot API
// Глобальные переменные для API
string api_base_url = "https://api.telegram.org/bot";
// HTTP запрос к Telegram API
string telegram_request(string token, string method, string params) {
string url = api_base_url + token + "/" + method;
if (params != "") {
url = url + "?" + params;
}
return http_get(url);
}
// Отправка сообщения
void send_message(string token, string chat_id, string text) {
string params = "chat_id=" + chat_id + "&text=" + url_encode(text);
string response = telegram_request(token, "sendMessage", params);
print("Message sent to " + chat_id);
}
// Получение обновлений
string get_updates(string token, int offset) {
string params = "offset=" + (offset + 1) + "&timeout=10";
return telegram_request(token, "getUpdates", params);
}
// Обработка обновлений
void process_updates(string json_response) {
print("=== PROCESS_UPDATES START ===");
// Извлекаем данные последнего сообщения
string chat_id = extract_chat_id(json_response);
print("Extracted chat_id: " + chat_id);
string text = extract_message_text(json_response);
print("Extracted text: " + text);
string username = extract_username(json_response);
int update_id = extract_update_id(json_response);
print("Chat ID: " + chat_id + ", Text: " + text + ", Update ID: " + update_id);
// Проверяем, что это новое сообщение
if (update_id > last_update_id && chat_id != "" && text != "") {
print("Processing new message...");
// Обрабатываем сообщение
string response = handle_message(text, username);
print("Sending response: " + response);
// Отправляем ответ
send_message(bot_token, chat_id, response);
// Обновляем offset
last_update_id = update_id;
print("Updated last_update_id to: " + last_update_id);
} else {
print("Skipping old message or empty data");
}
print("=== PROCESS_UPDATES END ===");
}
// Вспомогательные функции для парсинга JSON
string extract_chat_id(string json) {
return json_get(json, "chat_id");
}
string extract_message_text(string json) {
return json_get(json, "text");
}
string extract_username(string json) {
return "SkrinVex"; // Пока заглушка
}
int extract_update_id(string json) {
string id_str = json_get(json, "update_id");
if (id_str != "") {
return str_to_int(id_str);
}
return 0;
}
// URL кодирование
string url_encode(string text) {
// Простая замена специальных символов
string result = text;
result = replace_all(result, " ", "%20");
result = replace_all(result, "\n", "%0A");
return result;
}
// Проверка содержания строки - удалена, используем встроенную contains()
// Замена всех вхождений
string replace_all(string text, string from, string to) {
// Заглушка - в реальности нужна реализация
return text;
}
// Проверка начала строки
bool starts_with(string text, string prefix) {
// Заглушка - в реальности нужна реализация
if (text == "/start" || text == "/help" || text == "/example") {
return true;
}
return false;
}
// Чтение токена
string read_token() {
// Читаем токен из файла token.txt
return read_file("telegram_bot/token.txt");
}
// Пауза (заглушка)
void sleep(int seconds) {
// В реальности нужна реализация задержки
print("Sleeping for " + seconds + " seconds...");
}