-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
93 lines (78 loc) · 2.82 KB
/
bot.py
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
# -*- coding: utf-8 -*-
import telebot
import sqlite3
import time
import datetime
from utils import uptime
from conf import TOKEN, DB_NAME, ADMIN_IDS
import logging
bot_start_time = datetime.datetime.utcnow()
bot = telebot.TeleBot(TOKEN)
def execute_sql(query):
db_connection = sqlite3.connect(DB_NAME)
cursor = db_connection.cursor()
cursor.execute(query)
rows = cursor.fetchall()
db_connection.commit()
db_connection.close()
return rows
def broadcast(messages):
for message in messages:
if (message.chat.id not in ADMIN_IDS
or message.content_type != 'text'
or message.text.startswith('/')):
continue
query = 'SELECT id FROM chat ORDER BY created_at ASC LIMIT 10'
chat_ids = execute_sql(query)
for chat_id in chat_ids:
try:
bot.send_message(chat_id[0], message.text)
except telebot.apihelper.ApiException as e:
logging.error(e)
@bot.message_handler(commands=['subscribe'])
def command_subscribe(message):
chat_id = message.chat.id
query = '''INSERT OR IGNORE INTO chat (id, username, first_name, last_name, type)
VALUES ({id}, '{username}', '{first_name}', '{last_name}', '{type}')
'''.format(id=chat_id, username=message.chat.username, first_name=message.chat.first_name,
last_name=message.chat.last_name, type=message.chat.type)
execute_sql(query)
bot.send_message(chat_id, 'You have been subscribed.')
@bot.message_handler(commands=['unsubscribe'])
def command_unsubscribe(message):
chat_id = message.chat.id
query = 'DELETE from chat where id={}'.format(chat_id)
execute_sql(query)
bot.send_message(chat_id, 'You have been unsubscribed.')
@bot.message_handler(commands=['help'])
def command_help(message):
chat_id = message.chat.id
bot.send_message(
chat_id,
'\n'.join([
'I can help you keep up to date with news from Russian online travel market.',
'Add me to contacts and wait for messages...'])
)
@bot.message_handler(commands=['status'])
def command_status(message):
chat_id = message.chat.id
if chat_id in ADMIN_IDS:
chats_number = execute_sql('SELECT count(1) from chat')[0][0]
bot.send_message(
chat_id,
'\n'.join([
uptime(bot_start_time, datetime.datetime.utcnow()),
'Number of subscriptions: {}'.format(chats_number)
])
)
else:
bot.send_message(chat_id, 'Everything is ok. Stay in touch.')
if __name__ == '__main__':
bot.set_update_listener(broadcast)
while True:
try:
bot.polling()
except Exception as e:
bot.stop_polling()
logging.critical(e, exc_info=True)
time.sleep(10)