-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
68 lines (56 loc) · 2.18 KB
/
main.py
File metadata and controls
68 lines (56 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from telegram import Update, ReplyKeyboardMarkup
from telegram.ext import ApplicationBuilder, CommandHandler, MessageHandler, ContextTypes, filters
from dotenv import load_dotenv
import os, requests, logging
load_dotenv()
TOKEN=os.getenv("BOT_TOKEN")
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(message)s")
kb=ReplyKeyboardMarkup([
["ℹ️ About Bot"],
["👥 Who is on shift today?"],
["🛠️ Who handles what?"],
["♻️ Reset Bot"],
], resize_keyboard=True)
def log(update: Update):
user=update.effective_user
logging.info(f"user_id={user.id} username={user.username} text={update.message.text}")
async def info(update, context):
log(update)
await update.message.reply_text(
"This bot shows members and small info using api.\n\n"
"Available actions:\n"
"- View today's members\n"
"- View user info\n"
"- Reset bot state\n"
"- /start to reload menu"
)
async def reset(update, context):
log(update)
context.user_data.clear()
await update.message.reply_text("Bot state has been reset.", reply_markup=kb)
async def users(update, context):
log(update)
users=requests.get("https://jsonplaceholder.typicode.com/users").json()
await update.message.reply_text("\n".join(u["username"] for u in users))
async def numbers(update, context):
log(update)
users=requests.get("https://jsonplaceholder.typicode.com/users").json()
out="\n".join(f"{u['name']} -> age {20 + u['id']}" for u in users)
await update.message.reply_text(out)
routes={
"ℹ️ About Bot": info,
"👥 Who is online today?": users,
"🛠️ Who handles what?": numbers,
"♻️ Reset Bot": reset
}
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
log(update)
await update.message.reply_text("Select an option:", reply_markup=kb)
async def handle(update: Update, context: ContextTypes.DEFAULT_TYPE):
log(update)
fn=routes.get(update.message.text)
if fn: await fn(update, context)
app=ApplicationBuilder().token(TOKEN).build()
app.add_handler(CommandHandler("start", start))
app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle))
app.run_polling()