Пример на Python
Пример бота на Python
Простой эхо-бот для Kodari Messenger на long polling.
import time
import requests
TOKEN = "bot_ваш_токен"
API = "https://api.kodari.ru/msg/bot.php"
HEADERS = {"Authorization": f"Bearer {TOKEN}"}
offset = 0
while True:
r = requests.get(API, params={
"method": "getUpdates",
"offset": offset,
"timeout": 20
}, headers=HEADERS)
updates = r.json().get("result", [])
for upd in updates:
offset = upd["update_id"] + 1
msg = upd.get("message")
if msg and msg.get("text", "").startswith("/start"):
requests.post(API + "?method=sendMessage", json={
"chat_id": msg["chat"]["id"],
"text": "Привет! Я бот Kodari. Напиши что-нибудь.",
"reply_markup": {
"inline_keyboard": [[
{"text": "Сайт Kodari", "url": "https://kodari.ru"}
]]
}
}, headers=HEADERS)
cb = upd.get("callback_query")
if cb:
requests.post(API + "?method=answerCallbackQuery", json={
"callback_query_id": cb["id"]
}, headers=HEADERS)
time.sleep(0 if updates else 1)
20 просмотров