def send_telegram_message(user_id: int, message: str, parse_mode: str = None) -> int: user = User.objects.get(pk=user_id) message = bot.send_message(user.telegram_account.uid, message, parse_mode=parse_mode) return message.message_id
def check_employee_in_telegram(employee_id): employee = Employee.objects.select_related('user').get(pk=employee_id) if employee.user.telegram_account is None: employee.state = Employee.State.offline employee.save() return try: bot.send_message( employee.user.telegram_account.uid, __("We have started to look for a perfect lunch partner for you! " "Soon you will get link to your partner's account.\n\n" "_You received this message because we make sure all participants still available._" ), parse_mode='Markdown', ) except ApiException as e: if e.result.status_code == 403 and b'bot was blocked by the user' in e.result.content: employee.state = Employee.State.offline employee.save() logging.info( f'User `{employee.user}` as employee of company `{employee.company} was switched offline`' ) else: logging.error(f'{e}')
def test(user, message): bot.send_message(message.chat.id, str(user))
def send_welcome(user: User, message: Message): bot.send_message( message.chat.id, "Hi! I'm random lunch bot.")
def set_offline(user: Optional[User], message): if user: bot.send_message(message.chat.id, "Choose groups in which you want to go offline:", reply_markup=get_offline_keyboard_markup(user))
def send_message(self, text): bot.send_message(self.telegram_chat_id, text)
def process_join_answer(message): invite_token = message.text try: company = Company.objects.get(invite_token=invite_token) except Company.DoesNotExist: bot.reply_to( message, _("Sorry, couldn't find lunch group with the given token. :(")) state_registry.del_state(message.from_user.id) return # Authenticate user, create if need to from_user = message.from_user try: social_auth = UserSocialAuth.objects.get(provider='telegram', uid=from_user.id) except UserSocialAuth.DoesNotExist: storage = DjangoStorage() strategy = DjangoStrategy(storage) backend = TelegramAuth(strategy) username = get_username(strategy, from_user.__dict__, backend)['username'] with transaction.atomic(): user = create_user(strategy, from_user.__dict__, backend, username=username)['user'] user.first_name = from_user.first_name or '' user.last_name = from_user.last_name or '' user.has_telegram = True user.telegram_chat_id = message.chat.id user.save() UserSocialAuth.objects.get_or_create(provider='telegram', uid=from_user.id, defaults={ 'extra_data': from_user.__dict__, 'user': user, }) else: user = social_auth.user if not user.is_active: bot.send_message(message.chat.id, _('Sorry, your account has been deactivated.')) return # Create employee and add him to the group employee, created = Employee.objects.get_or_create(company=company, user=user) if created: msg = _( "You've successfully joined lunch group «{}». " "You may manage your participation with /offline and /online commands." ).format(company.name) else: msg = _( "You've joined lunch group «{}» already. " "You may manage your participation with /offline and /online commands." ).format(company.name) bot.send_message(message.chat.id, msg)
def join(message): bot.send_message(message.chat.id, _('Please enter invite token provided by your coworker:')) state_registry.set_state(message.from_user.id, 'join_answer')