Python Starter Bots & Examples¶
Tested Python code examples for creating interactive menu bots, inline callback handlers, and high-performance asynchronous bot workers.
1. Interactive Inline Keyboard Bot (Synchronous)¶
This bot sends an inline keyboard menu and handles button click callbacks:
import requests
import time
TOKEN = "YOUR_BOT_TOKEN_HERE"
API = f"https://botapi.codemeet.chat/bot{TOKEN}"
def send_message(chat_id, text, reply_markup=None):
payload = {"chat_id": chat_id, "text": text}
if reply_markup:
payload["reply_markup"] = reply_markup
return requests.post(f"{API}/sendMessage", json=payload, timeout=10).json()
def answer_callback(query_id, text, show_alert=False):
return requests.post(f"{API}/answerCallbackQuery", json={
"callback_query_id": query_id,
"text": text,
"show_alert": show_alert
}, timeout=10).json()
def main():
print("Python Bot is running...")
offset = None
while True:
try:
res = requests.get(f"{API}/getUpdates", params={"timeout": 25, "offset": offset}, timeout=30).json()
if not res.get("ok"):
time.sleep(1)
continue
for update in res.get("result", []):
offset = update["update_id"] + 1
# Handle incoming messages
if "message" in update:
msg = update["message"]
chat_id = msg["chat"]["id"]
text = msg.get("text", "")
if text == "/start":
keyboard = {
"inline_keyboard": [
[
{"text": "Live Stats", "callback_data": "menu_stats"},
{"text": "Settings", "callback_data": "menu_settings"}
],
[
{"text": "CodeMeet Website", "url": "https://codemeet.chat"}
]
]
}
send_message(chat_id, "Welcome! Choose an option below:", reply_markup=keyboard)
# Handle button clicks
elif "callback_query" in update:
cq = update["callback_query"]
query_id = cq["id"]
data = cq.get("data", "")
chat_id = cq["message"]["chat"]["id"]
if data == "menu_stats":
answer_callback(query_id, "Fetching system statistics...", show_alert=False)
send_message(chat_id, "System Status: All cluster nodes operational.")
elif data == "menu_settings":
answer_callback(query_id, "Settings panel triggered!", show_alert=True)
except Exception as e:
print("Error:", e)
time.sleep(2)
if __name__ == "__main__":
main()
2. Asynchronous Bot with httpx and asyncio¶
Non-blocking architecture using modern Python async capabilities:
import asyncio
import httpx
TOKEN = "YOUR_BOT_TOKEN_HERE"
API = f"https://botapi.codemeet.chat/bot{TOKEN}"
async def main():
async with httpx.AsyncClient(timeout=httpx.Timeout(30.0)) as client:
print("Async Bot running...")
offset = None
while True:
try:
params = {"timeout": 20}
if offset:
params["offset"] = offset
resp = await client.get(f"{API}/getUpdates", params=params)
data = resp.json()
if not data.get("ok"):
await asyncio.sleep(1)
continue
for update in data.get("result", []):
offset = update["update_id"] + 1
if "message" in update:
msg = update["message"]
chat_id = msg["chat"]["id"]
text = msg.get("text", "")
await client.post(f"{API}/sendMessage", json={
"chat_id": chat_id,
"text": f"Async Echo: {text}"
})
except Exception as e:
print("Error:", e)
await asyncio.sleep(2)
if __name__ == "__main__":
asyncio.run(main())