-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
65 lines (49 loc) · 1.82 KB
/
example.py
File metadata and controls
65 lines (49 loc) · 1.82 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
from telegram import Bot
import asyncio
import logging
import json
users = {}
async def sender():
while True:
for user_id in users:
await bot.send_message(user_id, "Broadcasted message")
await asyncio.sleep(10)
async def listener():
async def start_command(chat_id, message):
users[chat_id] = {
"first_name": message["from"].get("first_name", "Unknown"),
"username": message["from"].get("username", "Unknown"),
"language_code": message["from"].get("language_code", "Unknown"),
}
response_text = f"Welcome, {users[chat_id]['first_name']}! You have been added to the users list."
await bot.send_message(chat_id, response_text)
async def info_command(chat_id, _):
response_text = f"Users: {json.dumps(users, indent=2, ensure_ascii=False)}"
await bot.send_message(chat_id, response_text)
async def default_command(chat_id, message):
text = message.get("text", "")
response_text = f"You said: {text}"
await bot.send_message(chat_id, response_text)
commands = {
"/start": start_command,
"/info": info_command,
}
async def dispatch_message(update):
message = update.get("message")
if message:
chat_id = message["chat"]["id"]
text = message.get("text", "<no text provided>")
logging.info(f"New message from {chat_id}: {text}")
command_handler = commands.get(text, default_command)
await command_handler(chat_id, message)
await bot.listen(dispatch_message)
async def main():
await asyncio.gather(
sender(),
listener(),
)
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
API_TOKEN = "your token"
bot = Bot(API_TOKEN)
asyncio.run(main())