-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
144 lines (121 loc) · 4.91 KB
/
Copy pathmain.py
File metadata and controls
144 lines (121 loc) · 4.91 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import requests
import time
import random
import datetime
import pytz
import re
TOKEN = "" # Bot Token
API_URL = f"https://bot-api.zapps.me/bot{TOKEN}"
OWM_API_KEY = "a0a2306c46722b48a22d5fe7324c4765" # OpenWeatherMap API Key
# ==== API CORE ====
def send_message(chat_id, text):
url = f"{API_URL}/sendMessage"
payload = {"chat_id": chat_id, "text": text}
res = requests.post(url, json=payload)
print("Send response:", res.json())
def get_updates():
url = f"{API_URL}/getUpdates"
res = requests.get(url)
return res.json()
# ==== COMMAND HANDLER ====
def handle_greeting(chat_id, user_name="bạn"):
GREETINGS = [
"𝙩ố𝙩 𝙡à𝙣𝙝 🥳", "𝙫𝙪𝙞 𝙫ẻ 😄", "𝙝ạ𝙣𝙝 𝙥𝙝ú𝙘 ❤", "𝙮ê𝙪 đờ𝙞 😘",
"𝙢𝙖𝙮 𝙢ắ𝙣 🍀", "𝙛𝙪𝙡𝙡 𝙣ă𝙣𝙜 𝙡ượ𝙣𝙜 ⚡", "𝙩𝙪𝙮ệ𝙩 𝙫ờ𝙞 😁",
"𝙩ỉ𝙣𝙝 𝙩á𝙤 🤓", "đầ𝙮 𝙨ứ𝙘 𝙨ố𝙣𝙜 😽", "𝙣𝙝𝙞ệ𝙩 𝙝𝙪𝙮ế𝙩 🔥"
]
tz = pytz.timezone('Asia/Ho_Chi_Minh')
current_time = datetime.datetime.now(tz).strftime('%H%M')
hours = int(current_time)
if 1 <= hours <= 400:
session = "𝙨á𝙣𝙜 𝙩𝙞𝙣𝙝 𝙢ơ"
elif 401 <= hours <= 700:
session = "𝙨á𝙣𝙜 𝙨ớ𝙢"
elif 701 <= hours <= 1000:
session = "𝙨á𝙣𝙜"
elif 1001 <= hours <= 1200:
session = "𝙩𝙧ư𝙖"
elif 1201 <= hours <= 1700:
session = "𝙘𝙝𝙞ề𝙪"
elif 1701 <= hours <= 1800:
session = "𝙘𝙝𝙞ề𝙪 𝙩à"
elif 1801 <= hours <= 2100:
session = "𝙩ố𝙞"
elif 2101 <= hours <= 2400:
session = "𝙩ố𝙞 𝙢𝙪ộ𝙣"
else:
session = "𝐥ỗ𝐢"
greeting_text = random.choice(GREETINGS)
response = f"𝘾𝙝à𝙤 {user_name}, 𝘾𝙝ú𝙘 𝙗ạ𝙣 𝙢ộ𝙩 𝙗𝙪ổ𝙞 {session} {greeting_text}"
send_message(chat_id, response)
def handle_weather_command(chat_id, text):
# Tìm city từ câu nhập
city = None
match = re.search(r"(?:thời tiết|thoi tiet)(?: ở| tai)?\s*(.+)", text, re.IGNORECASE)
if match:
city = match.group(1).strip()
if not city or "hôm nay" in text:
city = "Buon Ma Thuot"
url = "https://api.openweathermap.org/data/2.5/weather"
params = {
"q": city,
"appid": OWM_API_KEY,
"units": "metric",
"lang": "vi"
}
try:
r = requests.get(url, params=params).json()
if r.get("cod") != 200:
raise ValueError(r.get("message", "Unknown error"))
desc = r["weather"][0]["description"].capitalize()
temp = r["main"]["temp"]
feels = r["main"]["feels_like"]
humidity = r["main"]["humidity"]
wind = r["wind"]["speed"]
msg = (
f"🌤 Thời tiết ở *{city.title()}*:\n"
f"🌡 Nhiệt độ: {temp}°C (cảm giác: {feels}°C)\n"
f"☁️ Trạng thái: {desc}\n"
f"💧 Độ ẩm: {humidity}%\n"
f"🌬 Gió: {wind} m/s\n"
)
except Exception as e:
msg = f"Không lấy được thông tin thời tiết: {e}"
send_message(chat_id, msg)
def handle_menu(chat_id):
menu_text = (
"✨ 𝗠𝗘𝗡𝗨 𝗕𝗢𝗧 ✨\n\n"
"👋 `chào` → bot chúc buổi {sáng / trưa / chiều / tối}\n"
"🌤 `thời tiết [tên thành phố]`\n"
" Ví dụ: `thời tiết Hà Nội`\n"
"📍 `thời tiết hôm nay` → mặc định Buôn Ma Thuột\n"
"📑 `menu` → hiện lại menu\n\n"
"⚡ Hãy thử nhắn tin ngay nào!"
)
send_message(chat_id, menu_text)
# ==== BOT LOOP ====
def main():
print("🤖 Bot Menu đang chạy...")
last_message_id = None
while True:
updates = get_updates()
if updates.get("ok"):
msg = updates["result"]["message"]
msg_id = msg["message_id"]
chat_id = msg["chat"]["id"]
text = msg.get("text", "").lower()
user_name = msg.get("from", {}).get("display_name", "bạn")
if msg_id != last_message_id:
last_message_id = msg_id
print(f"📩 {user_name}: {text}")
if text == "menu":
handle_menu(chat_id)
elif text in ["hi", "hello", "chào", "xin chào", "hí", "helo"]:
handle_greeting(chat_id, user_name)
elif "thời tiết" in text or "thoi tiet" in text:
handle_weather_command(chat_id, text)
else:
send_message(chat_id, f"Bạn vừa gửi: {text}")
time.sleep(2)
if __name__ == "__main__":
main()