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
| import requests
import sqlite3
from queue import Queue
from telegram import ReplyKeyboardMarkup, KeyboardButton, InlineKeyboardMarkup, InlineKeyboardButton
from telegram.ext import Updater, CommandHandler, MessageHandler, Dispatcher
API_TOKEN = '????????'
conn = sqlite3.connect('user_data.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS user_data
(user_id integer, message text, timestamp timestamp)''')
conn.commit()
def main():
updater = Updater(token=API_TOKEN, use_context=True)
dispatcher: Dispatcher = updater.dispatcher
def get_random_joke():
return "Вот тебе анекдот: колобок повесился"
def get_weather(city):
return "Погода в городе {Харьков}: Холодно аж зубы цокотят".format(city)
def create_menu(buttons):
reply_keyboard = [[KeyboardButton(button)] for button in buttons]
markup = ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True)
return markup
def send_message(chat_id, text, reply_markup=None, parse_mode=None):
url = f"https://api.telegram.org/bot{API_TOKEN}/sendMessage"
params = {'chat_id': chat_id, 'text': text}
if reply_markup:
params['reply_markup'] = reply_markup
if parse_mode:
params['parse_mode'] = parse_mode
response = requests.post(url, params=params)
return response.json()
def handle_update(update, context):
chat_id = update.effective_chat.id
text = update.message.text.lower()
store_user_data(chat_id, text)
if text.startswith('/'):
if text == '/start':
send_message(chat_id, 'Привет! Я твой бот.')
elif text == '/menu':
reply_markup = create_menu(['Анекдот', 'Погода', 'Настройки'])
send_message(chat_id, 'Выберите команду:', reply_markup=reply_markup)
elif text == '/whisper':
send_message(chat_id, 'Секрет: ', parse_mode='MarkdownV2')
elif text == '/scream':
send_message(chat_id, '*КРИК!!!*', parse_mode='MarkdownV2')
elif text == '/settings':
pass
else:
if text == 'анекдот':
joke = get_random_joke()
send_message(chat_id, joke)
elif text == 'погода':
city = 'Мухосранск'
weather = get_weather(city)
send_message(chat_id, weather)
else:
send_message(chat_id, 'Я не понимаю эту команду.')
def store_user_data(user_id, message):
with conn:
c.execute("INSERT INTO user_data VALUES (?, ?, datetime('now'))", (user_id, message))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main() |