-
Notifications
You must be signed in to change notification settings - Fork 0
/
telegram_bot.py
148 lines (126 loc) · 4.5 KB
/
telegram_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
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
145
146
147
148
from contextlib import suppress
import os
import sqlite3
from functools import partial
from environs import Env
from telegram import KeyboardButton, ReplyKeyboardMarkup, Update
from telegram.ext import (CallbackContext, CallbackQueryHandler,
CommandHandler, Filters, MessageHandler, Updater)
import db_processing as db_processing
import settings
MAIN_KEYBOARD = ReplyKeyboardMarkup(
[
[KeyboardButton(settings.GET_VACANCY_BUTTON)],
[KeyboardButton(settings.COUNT_ACTIVE_VACANCIES)],
[KeyboardButton(settings.COVERING_LETTER_BUTTON)]
],
resize_keyboard=True
)
def start(update: Update, context: CallbackContext) -> None:
context.bot.send_message(
update.effective_chat.id,
'Привет',
reply_markup=MAIN_KEYBOARD
)
def send_vacancy(db: sqlite3.Connection,
cursor: sqlite3.Cursor,
update: Update,
context: CallbackContext) -> None:
vacancy = db_processing.get_active_vacancy(db, cursor)
context.bot.send_message(
chat_id=update.effective_chat.id,
text=vacancy.message(),
reply_markup=vacancy.make_tg_inline_keyboard()
)
def count_active_vacancy(db: sqlite3.Connection,
cursor: sqlite3.Cursor,
update: Update,
context: CallbackContext) -> None:
active_vacancies_num = db_processing.count_active_vacancies(db, cursor)
context.bot.send_message(
update.effective_chat.id,
f'У вас {active_vacancies_num} необработанных вакансий.'
)
def response_vacancy(db: sqlite3.Connection,
cursor: sqlite3.Cursor,
update: Update,
context: CallbackContext) -> None:
context.bot.edit_message_text(
f'{update.callback_query.message.text}\n\nUPD:\nБыл отправлен отклик.\nВакансия в архиве',
chat_id=update.callback_query.message.chat_id,
message_id=update.callback_query.message.message_id,
)
db_processing.update_response_and_process_statuses(
db,
cursor,
update.callback_query.data.split(':')[1]
)
def archive_vacancy(db: sqlite3.Connection,
cursor: sqlite3.Cursor,
update: Update,
context: CallbackContext) -> None:
context.bot.edit_message_text(
f'{update.callback_query.message.text}\n\nUPD:\nВакансия в архиве',
chat_id=update.callback_query.message.chat_id,
message_id=update.callback_query.message.message_id,
)
db_processing.update_process_status(
db,
cursor,
update.callback_query.data.split(':')[1]
)
def send_covering_letter(update: Update, context: CallbackContext):
with open(env.str('COVERING_LETTER_PATH'), 'r') as file:
context.bot.send_message(
update.effective_chat.id,
file.read()
)
context.bot.send_document(
chat_id=update.effective_chat.id,
document=open(env.str('COVERING_LETTER_PATH'), 'rb')
)
def run_bot(tg_bot_token: str):
update = Updater(token=tg_bot_token, use_context=True)
update.dispatcher.add_handler(
CommandHandler('start', start)
)
update.dispatcher.add_handler(
MessageHandler(
Filters.text([settings.GET_VACANCY_BUTTON]),
partial(send_vacancy, db, cursor)
)
)
update.dispatcher.add_handler(
MessageHandler(
Filters.text([settings.COVERING_LETTER_BUTTON]),
send_covering_letter
)
)
update.dispatcher.add_handler(
MessageHandler(
Filters.text([settings.COUNT_ACTIVE_VACANCIES]),
partial(count_active_vacancy, db, cursor)
)
)
update.dispatcher.add_handler(
CallbackQueryHandler(
partial(archive_vacancy, db, cursor),
pattern='archive_vacancy'
)
)
update.dispatcher.add_handler(
CallbackQueryHandler(
partial(response_vacancy, db, cursor),
pattern='response_vacancy'
)
)
update.start_polling()
update.idle()
if __name__ == '__main__':
env = Env()
env.read_env()
with suppress(Exception):
db = sqlite3.connect(os.path.join(settings.DB_ROOT_FOLDER, env.str('DATABASE', 'vacancies.sqlite3')), check_same_thread=False)
cursor = db.cursor()
run_bot(env.str('TELEGRAM_BOT_TOKEN'))
db.close()