def get_service_bot_events(sender: UserProfile, service_bot_tuples: List[Tuple[(int, int)]], mentioned_user_ids: Set[int], active_user_ids: Set[int], recipient_type: int) -> Dict[(str, List[Dict[(str, Any)]])]:
    event_dict: Dict[(str, List[Dict[(str, Any)]])] = defaultdict(list)
    if sender.is_bot:
        return event_dict

    def maybe_add_event(user_profile_id: int, bot_type: int) -> None:
        if (bot_type == UserProfile.OUTGOING_WEBHOOK_BOT):
            queue_name = 'outgoing_webhooks'
        elif (bot_type == UserProfile.EMBEDDED_BOT):
            queue_name = 'embedded_bots'
        else:
            logging.error('Unexpected bot_type for Service bot id=%s: %s', user_profile_id, bot_type)
            return
        is_stream = (recipient_type == Recipient.STREAM)
        if ((user_profile_id not in mentioned_user_ids) and (user_profile_id not in active_user_ids)):
            return
        if (is_stream and (user_profile_id in mentioned_user_ids)):
            trigger = 'mention'
        elif ((not is_stream) and (user_profile_id in active_user_ids)):
            trigger = 'private_message'
        else:
            return
        event_dict[queue_name].append({'trigger': trigger, 'user_profile_id': user_profile_id})
    for (user_profile_id, bot_type) in service_bot_tuples:
        maybe_add_event(user_profile_id=user_profile_id, bot_type=bot_type)
    return event_dict
def get_service_bot_events(sender: UserProfile, service_bot_tuples: List[Tuple[(int, int)]], mentioned_user_ids: Set[int], active_user_ids: Set[int], recipient_type: int) -> Dict[(str, List[Dict[(str, Any)]])]:
    event_dict: Dict[(str, List[Dict[(str, Any)]])] = defaultdict(list)
    if sender.is_bot:
        return event_dict

    def maybe_add_event(user_profile_id: int, bot_type: int) -> None:
        if (bot_type == UserProfile.OUTGOING_WEBHOOK_BOT):
            queue_name = 'outgoing_webhooks'
        elif (bot_type == UserProfile.EMBEDDED_BOT):
            queue_name = 'embedded_bots'
        else:
            logging.error('Unexpected bot_type for Service bot id=%s: %s', user_profile_id, bot_type)
            return
        is_stream = (recipient_type == Recipient.STREAM)
        if ((user_profile_id not in mentioned_user_ids) and (user_profile_id not in active_user_ids)):
            return
        if (is_stream and (user_profile_id in mentioned_user_ids)):
            trigger = 'mention'
        elif ((not is_stream) and (user_profile_id in active_user_ids)):
            trigger = 'private_message'
        else:
            return
        event_dict[queue_name].append({'trigger': trigger, 'user_profile_id': user_profile_id})
    for (user_profile_id, bot_type) in service_bot_tuples:
        maybe_add_event(user_profile_id=user_profile_id, bot_type=bot_type)
    return event_dict