from datetime import date from telebot.types import CallbackQuery, Message from bot import Bot from community import Community def views(bot: Bot): @bot.handle_commands(['/start']) def handle_start(msg: Message, user, args): if msg.chat.type in 'supergroup': bot.reply_with_template(msg, 'welcome', community=Community.by_id(msg.chat.id, bot)) return if args.strip().startswith('comm'): community_id = int('-'.join(args.strip().split('-')[1:])) community = Community.by_id(community_id, bot) if user.user_id in community.members: bot.reply_with_template(msg, 'community_added', community=community, already_member=True) elif community.add_member(user.user_id, bot): bot.reply_with_template(msg, 'community_added', community=community, already_member=False) bot.poll_user_for_community(user, community) else: bot.reply_with_template(msg, 'err_not_a_member', community=community) bot.save_community(community) bot.reply_with_template(msg, 'info', start=True) @bot.handle_commands(['/help', 'ℹ️ About', 'ℹ️ О боте']) def handle_help(msg, _user, _args): bot.reply_with_template(msg, 'info', start=False) @bot.handle_commands(['/request', '📝 Request a meeting', '📝 Запросить встречу']) def request_meeting(msg: Message, user, _args): if msg.chat.type != 'private': bot.reply_with_template(msg, 'err_not_private') return comm_ids_and_names = [(community.chat_id, community.name) for community in user.get_communities(bot)] bot.reply_with_template(msg, 'request_meeting', comm_list=comm_ids_and_names) @bot.handle_commands(['/join']) def join_community(msg: Message, user, args): # check if it really is a community if msg.chat.type == 'private': bot.reply_with_template(msg, 'err_not_community') return # send community link community = Community.by_id(msg.chat.id, bot) bot.reply_with_template(msg, 'welcome', community=community, join=True) @bot.handle_callback('request_meeting') def request_meeting_callback(query: CallbackQuery, user, args): community = Community.by_id(int(args), bot) if community.can_user_request_a_meeting(user.user_id): community.polled[user.user_id] = date.today() community.pool.append(user.user_id) bot.reply_with_template(query, 'request_meeting_success', community=community) else: bot.reply_with_template(query, 'request_meeting_failure', community=community) bot.answer_callback_query(query.id) @bot.handle_callback('dismiss') def dismiss_callback(query: CallbackQuery, user, _args): bot.answer_callback_query(query.id, {'en': 'Ok, fine', 'ru': 'ладно'}.get(user.locale, 'Ok')) bot.delete_message(query.message.chat.id, query.message.message_id) @bot.handle_callback('community_add') def community_add_callback(query: CallbackQuery, user, args): community = Community.by_id(int(args), bot) if user.user_id in community.members: bot.reply_with_template(query, 'community_added', community=community, already_member=True) elif community.add_member(user.user_id, bot): bot.reply_with_template(query, 'community_added', community=community, already_member=False) bot.poll_user_for_community(user, community) bot.answer_callback_query(query.id) bot.save_community(community) @bot.handle_commands(['/send_all']) def send_spam(msg, user, _args): if not user.is_admin(): return def handler(msg_1): bot.send_all_copy(msg_1) bot.send_message(msg.chat.id, 'Send the message') bot.register_next_step_handler(msg, handler) @bot.handle_commands(['/all_stats']) def all_stats(msg, user, _args): if not user.is_admin(): return stats = bot.stats() bot.reply_with_template(msg, 'admin_stats', stats=stats) @bot.handle_commands(['🛑 Cancel', '🛑 Отменить']) def cancel_creation(msg, _user, _args): bot.clear_step_handler_by_chat_id(msg.chat.id) bot.reply_with_template(msg, 'canceled') @bot.my_chat_member_handler() def handle_my_chat_member(upd): chat_id: int = upd.chat.id # get the community and add it if it doesn't exist community: Community = Community.by_id(chat_id, bot) # send welcome message to the chat (if we can) - and if we should bot.reply_with_template(upd, 'welcome', community=community) @bot.handle_callback('meetings') def handle_meetings_answer(query, user, args): community_id, answer = args.split('_') answer = int(answer) community = Community.by_id(int(community_id), bot) community.add_answer(user.user_id, int(answer), bot) # edit the keyboard bot.edit_message_with_template(query, 'poll_user', community=community, answer=answer) @bot.handle_commands(['/send_polls']) def send_polls(msg, _user, _args): community: Community = Community.by_id(msg.chat.id, bot) bot.poll_users_in_community(community) @bot.handle_commands(['/send_meetings']) def send_meetings(msg, _user, _args): community: Community = Community.by_id(msg.chat.id, bot) bot.send_meeting_info_in_community(community) @bot.handle_commands(['/schedule_meetings']) def schedule_meetings(msg, _user, _args): community: Community = Community.by_id(msg.chat.id, bot) community.schedule_meetings() bot.save_community(community)