Skip to content

Commit c74e510

Browse files
committed
Fix manage_worker
1 parent 5536582 commit c74e510

2 files changed

Lines changed: 35 additions & 34 deletions

File tree

tests/test_manage_worker.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ def test_does_nothing_if_state_not_exists(self):
3939

4040
class TestStart:
4141
def test_start_sends_welcome_message(self):
42-
bot = Mock()
4342
update = Mock()
43+
context = Mock()
4444

45-
start(bot, update)
45+
start(update, context)
4646

4747
update.message.reply_text.assert_called_once()
4848
call_args = update.message.reply_text.call_args[0][0]
@@ -52,24 +52,24 @@ def test_start_sends_welcome_message(self):
5252
class TestNew:
5353
@patch('vk_channelify.manage_worker.metrics')
5454
def test_new_starts_conversation(self, mock_metrics):
55-
bot = Mock()
5655
update = Mock()
56+
context = Mock()
5757

58-
result = new(bot, update)
58+
result = new(update, context)
5959

6060
assert_that(result, equal_to(ASKED_VK_GROUP_LINK_IN_NEW))
6161
update.message.reply_text.assert_called_once()
6262

6363

6464
class TestNewInStateAskedVkGroupLink:
6565
def test_saves_vk_domain_and_asks_for_channel_access(self):
66-
bot = Mock()
6766
update = Mock()
67+
context = Mock()
6868
update.message.text = 'https://vk.ru/mygroup'
6969
update.message.from_user.id = 12345
7070
users_state = {}
7171

72-
result = new_in_state_asked_vk_group_link(bot, update, users_state=users_state)
72+
result = new_in_state_asked_vk_group_link(update, context, users_state=users_state)
7373

7474
assert_that(result, equal_to(ASKED_CHANNEL_ACCESS_IN_NEW))
7575
assert_that(users_state[12345]['vk_domain'], equal_to('mygroup'))
@@ -78,10 +78,10 @@ def test_saves_vk_domain_and_asks_for_channel_access(self):
7878

7979
class TestNewInStateAskedChannelAccess:
8080
def test_asks_for_channel_message(self):
81-
bot = Mock()
8281
update = Mock()
82+
context = Mock()
8383

84-
result = new_in_state_asked_channel_access(bot, update)
84+
result = new_in_state_asked_channel_access(update, context)
8585

8686
assert_that(result, equal_to(ASKED_CHANNEL_MESSAGE_IN_NEW))
8787
update.message.reply_text.assert_called_once()
@@ -92,27 +92,27 @@ class TestNewInStateAskedChannelMessage:
9292
@patch('vk_channelify.manage_worker.Channel')
9393
@patch('vk_channelify.manage_worker.DisabledChannel')
9494
def test_creates_channel_successfully(self, mock_disabled_channel, mock_channel, mock_metrics):
95-
bot = Mock()
9695
update = Mock()
96+
context = Mock()
9797
update.message.from_user.id = 12345
9898
update.message.from_user.username = 'testuser'
9999
update.message.forward_from_chat.id = -100123456
100100
users_state = {12345: {'vk_domain': 'mygroup'}}
101101
db = Mock()
102102
db_session_maker = Mock(return_value=db)
103103

104-
result = new_in_state_asked_channel_message(bot, update, db_session_maker=db_session_maker, users_state=users_state)
104+
result = new_in_state_asked_channel_message(update, context, db_session_maker=db_session_maker, users_state=users_state)
105105

106106
assert_that(result, equal_to(ConversationHandler.END))
107107
db.add.assert_called_once()
108108
db.commit.assert_called_once()
109-
bot.send_message.assert_called_once()
109+
context.bot.send_message.assert_called_once()
110110

111111
@patch('vk_channelify.manage_worker.metrics')
112112
@patch('vk_channelify.manage_worker.Channel')
113113
def test_rolls_back_on_error(self, mock_channel, mock_metrics):
114-
bot = Mock()
115114
update = Mock()
115+
context = Mock()
116116
update.message.from_user.id = 12345
117117
update.message.from_user.username = 'testuser'
118118
update.message.forward_from_chat.id = -100123456
@@ -122,7 +122,7 @@ def test_rolls_back_on_error(self, mock_channel, mock_metrics):
122122
db_session_maker = Mock(return_value=db)
123123

124124
with pytest.raises(Exception):
125-
new_in_state_asked_channel_message(bot, update, db_session_maker=db_session_maker, users_state=users_state)
125+
new_in_state_asked_channel_message(update, context, db_session_maker=db_session_maker, users_state=users_state)
126126

127127
db.rollback.assert_called_once()
128128
mock_metrics.telegram_conversations_total.labels.assert_called_with(type='new', status='failed')
@@ -131,12 +131,12 @@ def test_rolls_back_on_error(self, mock_channel, mock_metrics):
131131
class TestCancelNew:
132132
@patch('vk_channelify.manage_worker.metrics')
133133
def test_cancel_ends_conversation(self, mock_metrics):
134-
bot = Mock()
135134
update = Mock()
135+
context = Mock()
136136
update.message.from_user.id = 12345
137137
users_state = {12345: {'vk_domain': 'mygroup'}}
138138

139-
result = cancel_new(bot, update, users_state=users_state)
139+
result = cancel_new(update, context, users_state=users_state)
140140

141141
assert_that(result, equal_to(ConversationHandler.END))
142142
assert_that(12345 not in users_state, is_(True))

vk_channelify/manage_worker.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ def on_error(update, context):
102102

103103
def catch_exceptions(func):
104104
@wraps(func)
105-
def wrapper(bot, update, *args, **kwargs):
105+
def wrapper(update, context, *args, **kwargs):
106106
try:
107-
return func(bot, update, *args, **kwargs)
107+
return func(update, context, *args, **kwargs)
108108
except Exception as e:
109109
logger.error('Exception in {}: {}'.format(func.__name__, e))
110110
traceback.print_exc()
@@ -116,11 +116,11 @@ def wrapper(bot, update, *args, **kwargs):
116116
def observe_metrics(command_name):
117117
def decorator(func):
118118
@wraps(func)
119-
def wrapper(bot, update, *args, **kwargs):
119+
def wrapper(update, context, *args, **kwargs):
120120
metrics.telegram_commands_total.labels(command=command_name).inc()
121121
start_time = time.time()
122122
try:
123-
result = func(bot, update, *args, **kwargs)
123+
result = func(update, context, *args, **kwargs)
124124
duration = time.time() - start_time
125125
metrics.telegram_command_duration_seconds.labels(command=command_name).observe(duration)
126126
return result
@@ -147,13 +147,13 @@ def wrapper(*args, db_session_maker, **kwargs):
147147

148148
@catch_exceptions
149149
@observe_metrics('start')
150-
def start(bot, update):
150+
def start(update, context):
151151
update.message.reply_text('Команда /new настроит новый канал. В канал будут пересылаться посты из группы ВК')
152152

153153

154154
@catch_exceptions
155155
@observe_metrics('new')
156-
def new(bot, update):
156+
def new(update, context):
157157
metrics.telegram_conversations_total.labels(type='new', status='started').inc()
158158

159159
update.message.reply_text('Отправьте ссылку на группу ВК')
@@ -162,7 +162,7 @@ def new(bot, update):
162162

163163

164164
@catch_exceptions
165-
def new_in_state_asked_vk_group_link(bot, update, users_state):
165+
def new_in_state_asked_vk_group_link(update, context, users_state):
166166
vk_url = update.message.text
167167
vk_domain = vk_url.split('/')[-1]
168168
users_state[update.message.from_user.id] = dict()
@@ -178,15 +178,15 @@ def new_in_state_asked_vk_group_link(bot, update, users_state):
178178

179179

180180
@catch_exceptions
181-
def new_in_state_asked_channel_access(bot, update):
181+
def new_in_state_asked_channel_access(update, context):
182182
update.message.reply_text('Хорошо. Перешлите любое сообщение из канала', reply_markup=ReplyKeyboardRemove())
183183

184184
return ASKED_CHANNEL_MESSAGE_IN_NEW
185185

186186

187187
@catch_exceptions
188188
@make_db_session
189-
def new_in_state_asked_channel_message(bot, update, db, users_state):
189+
def new_in_state_asked_channel_message(update, context, db, users_state):
190190
user_id = update.message.from_user.id
191191
username = update.message.from_user.username
192192
channel_id = str(update.message.forward_from_chat.id)
@@ -208,7 +208,7 @@ def new_in_state_asked_channel_message(bot, update, db, users_state):
208208
logger.warning('Cannot delete disabled channel of {}'.format(channel_id))
209209
traceback.print_exc()
210210

211-
bot.send_message(channel_id, 'Канал работает с помощью @vk_channelify_bot')
211+
context.bot.send_message(channel_id, 'Канал работает с помощью @vk_channelify_bot')
212212

213213
update.message.reply_text('Готово!')
214214
update.message.reply_text('Бот будет проверять группу каждые 15 минут')
@@ -221,7 +221,7 @@ def new_in_state_asked_channel_message(bot, update, db, users_state):
221221

222222

223223
@catch_exceptions
224-
def cancel_new(bot, update, users_state):
224+
def cancel_new(update, context, users_state):
225225
metrics.telegram_conversations_total.labels(type='new', status='cancelled').inc()
226226

227227
update.message.reply_text('Ладно', reply_markup=ReplyKeyboardRemove())
@@ -235,7 +235,7 @@ def cancel_new(bot, update, users_state):
235235
@catch_exceptions
236236
@make_db_session
237237
@observe_metrics('filter_by_hashtag')
238-
def filter_by_hashtag(bot, update, db, users_state):
238+
def filter_by_hashtag(update, context, db, users_state):
239239
user_id = update.message.from_user.id
240240

241241
metrics.telegram_conversations_total.labels(type='filter_by_hashtag', status='started').inc()
@@ -246,7 +246,7 @@ def filter_by_hashtag(bot, update, db, users_state):
246246
keyboard_row = []
247247
for channel in db.query(Channel).filter(Channel.owner_id == str(user_id)).order_by(Channel.created_at.desc()):
248248
try:
249-
channel_chat = bot.get_chat(chat_id=channel.channel_id)
249+
channel_chat = context.bot.get_chat(chat_id=channel.channel_id)
250250
users_state[user_id]['channels'][channel_chat.title] = channel.channel_id
251251
keyboard_row.append(channel_chat.title)
252252
if len(keyboard_row) == 2:
@@ -265,7 +265,7 @@ def filter_by_hashtag(bot, update, db, users_state):
265265

266266
@catch_exceptions
267267
@make_db_session
268-
def filter_by_hashtag_in_state_asked_channel_id(bot, update, db, users_state):
268+
def filter_by_hashtag_in_state_asked_channel_id(update, context, db, users_state):
269269
user_id = update.message.from_user.id
270270
channel_title = update.message.text
271271
channel_id = str(users_state[user_id]['channels'][channel_title])
@@ -282,7 +282,7 @@ def filter_by_hashtag_in_state_asked_channel_id(bot, update, db, users_state):
282282

283283
@catch_exceptions
284284
@make_db_session
285-
def filter_by_hashtag_in_state_asked_hashtags(bot, update, db, users_state):
285+
def filter_by_hashtag_in_state_asked_hashtags(update, context, db, users_state):
286286
user_id = update.message.from_user.id
287287
channel = users_state[user_id]['channel']
288288

@@ -303,8 +303,9 @@ def filter_by_hashtag_in_state_asked_hashtags(bot, update, db, users_state):
303303

304304

305305
@catch_exceptions
306-
def cancel_filter_by_hashtag(bot, update, users_state):
306+
def cancel_filter_by_hashtag(update, context, users_state):
307307
metrics.telegram_conversations_total.labels(type='filter_by_hashtag', status='cancelled').inc()
308+
308309
update.message.reply_text('Ладно', reply_markup=ReplyKeyboardRemove())
309310
update.message.reply_text('Настроить фильтр по хештегам можно командой /filter_by_hashtag')
310311
update.message.reply_text('Команда /new настроит новый канал')
@@ -317,7 +318,7 @@ def cancel_filter_by_hashtag(bot, update, users_state):
317318
@catch_exceptions
318319
@make_db_session
319320
@observe_metrics('recover')
320-
def recover(bot, update, db, users_state):
321+
def recover(update, context, db, users_state):
321322
user_id = update.message.from_user.id
322323

323324
metrics.telegram_conversations_total.labels(type='recover', status='started').inc()
@@ -349,7 +350,7 @@ def recover(bot, update, db, users_state):
349350

350351
@catch_exceptions
351352
@make_db_session
352-
def recover_in_state_asked_channel_id(bot, update, db, users_state):
353+
def recover_in_state_asked_channel_id(update, context, db, users_state):
353354
user_id = update.message.from_user.id
354355
channel_title = update.message.text
355356
channel_id = str(users_state[user_id]['channels'][channel_title])
@@ -381,7 +382,7 @@ def recover_in_state_asked_channel_id(bot, update, db, users_state):
381382

382383

383384
@catch_exceptions
384-
def cancel_recover(bot, update, users_state):
385+
def cancel_recover(update, context, users_state):
385386
metrics.telegram_conversations_total.labels(type='recover', status='cancelled').inc()
386387

387388
update.message.reply_text('Ладно', reply_markup=ReplyKeyboardRemove())

0 commit comments

Comments
 (0)