def get_media_obj(enroll_complete, file_type, file_id, caption): if enroll_complete: if file_type == 'photo': obj = InputMediaPhoto(file_id, caption=caption) else: obj = InputMediaDocument(file_id, caption=caption) else: obj = InputMediaDocument(kitty, caption=caption) return obj
async def echo(message: types.Message): with open('data.json', 'rb') as f: print(f) await bot.send_document(message.chat.id, f) t0 = time.time() json2 = await bot.forward_message(message.chat.id, message.chat.id, message.message_id + 1) data = await bot.get_file(json2.document.file_id) data2 = bot.get_file_url(data.file_path) page_source = urlopen(data2).read() print(page_source) d = json.loads(page_source) with open('data3.json', 'w') as json_file: json.dump(d, json_file) with open('data3.json', 'rb') as f: await bot.edit_message_media(InputMediaDocument(f), message.chat.id, message.message_id + 1) t1 = time.time() print(t1 - t0)
async def save_data(data): t = time.time() try: await bot.edit_message_media( InputMediaDocument(io.StringIO(json.dumps(data))), admin_id, 4) except Exception as ex: print(ex) print('timesave') print(time.time() - t)
async def show_price(callback: CallbackQuery): id = callback.from_user.id await bot.send_message( id, ''' 📜Подробные цены и условия ты можешь узнать, просмотрев файлы: ''') docs = [ InputMediaDocument( "BQACAgIAAxkDAAOpYJV9sQAB_WyhUnB4OaJyz9iYcwWOAALEDQAChTCoSANj-8Xr19WmHwQ" ), InputMediaDocument( "BQACAgIAAxkDAAOqYJV9tav31IcazY5PneMZgaOFv5YAAsUNAAKFMKhIHwP7qz-cVdkfBA" ), InputMediaDocument( "BQACAgIAAxkDAAOrYJV9t83XufW1vXSi8aVILD3XeuAAAsYNAAKFMKhI8C72eBwH0LQfBA" ), ] await bot.send_media_group(id, docs)
async def save_data(): to = time.time() sql = "SELECT * FROM users " cursor.execute(sql) data = cursor.fetchall() # or use fetchone() try: # Переводим словарь в строку str_data = json.dumps(data) # Обновляем наш файл с данными await bot.edit_message_media(InputMediaDocument(io.StringIO(str_data)), admin_id, config_id) except Exception as ex: print(ex) print('Время сохранения бекапа:=' + str(time.time() - to))
async def send_enrollment_message(message: types.Message, user: User, enrollment: Enrollment, edit=False) -> types.Message: m_b = enrollment_str(user.uid, user.username, user.full_name, enrollment.complete, enrollment.edit_datetime) if edit: try: await message.edit_media(get_media_obj(enrollment.complete, enrollment.file_type, enrollment.file_id, m_b), reply_markup=keyboard_scroll) except MessageNotModified: logging.info(f'Message {message.message_id} is not modified') except BadRequest: # can't send media await message.edit_media(InputMediaDocument(kitty, caption=m_b), reply_markup=keyboard_scroll) finally: result = message else: if enrollment.complete: try: if enrollment.file_type == 'photo': result = await bot.send_photo(message.chat.id, photo=enrollment.file_id, caption=m_b, reply_markup=keyboard_scroll) else: result = await bot.send_document( message.chat.id, document=enrollment.file_id, caption=m_b, reply_markup=keyboard_scroll) except BadRequest: # can't send media result = await bot.send_document(message.chat.id, document=kitty, caption=m_b, reply_markup=keyboard_scroll) else: result = await bot.send_document(message.chat.id, document=kitty, caption=m_b, reply_markup=keyboard_scroll) return result
async def main_logic(message: types.Message): t0 = time.time() if message.text == 'clean': with open('data2.json', 'rb') as f: await bot.edit_message_media(InputMediaDocument(f), admin_id, 4) # with open('data.json', 'rb') as f: # print(f) # await bot.send_document(message.chat.id, f) print(message.message_id) data = await get_data() # print(data) print(len(data['users'])) metka2 = False for user in data['users']: if user['chatid'] == message.chat.id: time_user = [] try: for stroka in message.text.split(':'): time_user.append(int(re.search(r'\d+', stroka).group())) except Exception as ex: # print(ex) pass print(time_user) if len(time_user) == 1: if time_user[0] > 0 and time_user[0] < 120: new_time = datetime.datetime.now() + datetime.timedelta( minutes=time_user[0]) metka_time = False for time_us in user['calltime']: if (new_time.time().hour + 3) == time_us[0] and new_time.time( ).minute == time_us[1]: await bot.send_message( message.chat.id, 'На данное время у Вас уже записан звонок') metka_time = True break if metka_time == False: user['calltime'].append( [new_time.time().hour + 3, new_time.time().minute]) await save_data(data) await bot.send_message( message.chat.id, 'Вы добавили звонок на {}:{}'.format( str(new_time.time().hour + 3), str(new_time.time().minute))) else: await bot.send_message(message.chat.id, 'Укажите время от 1 до 120 минут') if len(time_user) == 2: if time_user[0] >= 0 and time_user[0] <= 24 and time_user[ 1] >= 0 and time_user[1] <= 60: metka_time = False for time_us in user['calltime']: if time_user[0] == time_us[0] and time_user[ 1] == time_us[1]: await bot.send_message( message.chat.id, 'На данное время уже запланирован звонок') metka_time = True break if metka_time == False: user['calltime'].append([time_user[0], time_user[1]]) await save_data(data) await bot.send_message( message.chat.id, 'Вы добавили звонок на {}:{}'.format( str(time_user[0]), str(time_user[1]))) else: await bot.send_message( message.chat.id, 'Вы указали неправильное время. Попробуйте занова.') if message.text == B_CLEAN: user['calltime'].clear() await save_data(data) await bot.send_message(message.chat.id, 'Все звонки удачно сброшены.') if message.text == B_CALL_NOW: client = Client(account_sid, auth_token) client.calls.create(url='https://ex.ru', to=user['phones'], from_='+12027967603') if message.text == B_LIST_CALLS: if len(user['calltime']) == 0: await bot.send_message( message.chat.id, 'У Вас нет запланированных звонков') else: str_call_list = 'Запланированные звонки:\n' for time_call in user['calltime']: str_call_list += str(time_call[0]) + ':' + str( time_call[1]) + '\n' await bot.send_message(message.chat.id, str_call_list) if message.text == B_INFO: await bot.send_message(message.chat.id, 'Инфо тут') metka2 = True break if metka2 == False: button = KeyboardButton(B_REGISTRER) button.request_contact = True kb = ReplyKeyboardMarkup(resize_keyboard=True).add(button) await bot.send_message( message.chat.id, 'Вы не зарегистрированы, нажмите на кнопку "Регистрация"', reply_markup=kb) if message.text == 'add': for number in range(10000): data['users'].append({ 'chatid': number, 'phones': 8917, 'state': 0, 'calltime': [] }) with open('data3.json', 'w') as json_file: json.dump(data, json_file) with open('data3.json', 'rb') as f: await bot.edit_message_media(InputMediaDocument(f), admin_id, 4) t1 = time.time() print('end: ' + str(t1 - t0))