예제 #1
0
    def request_matches(self, event):
        channel_id = event['channel']['id']
        team_id = event['team']['id']
        user_id = event['user']['id']

        profile = SocialProfile.objects.get(slack_id=user_id)
        topic_channel = TopicChannel.objects.get(channel_id=channel_id, slack_team_id=team_id)

        recent_requests = MatchRequest.objects.filter(
            topic_channel=topic_channel,
            profile=profile,
            date__gte=timezone.now() - timedelta(days=7),
        )

        if len(recent_requests) > 0:
            chat_postEphemeral_with_fallback(
                self.slack_client,
                channel=channel_id,
                user=user_id,
                text='You already requested to be matched in this channel recently.',
            )
        else:
            MatchRequest.objects.create(profile=profile, topic_channel=topic_channel)

            chat_postEphemeral_with_fallback(
                self.slack_client,
                channel=channel_id,
                user=user_id,
                blocks=confirm_match_request(channel_id),
            )
예제 #2
0
def command(request):
    event = request.POST
    logging.info(f'COMMAND> {request.POST}')
    command = event['text'].split(' ', 1)[0]
    if command == 'chat':
        PennyChatBotModule.create_penny_chat(slack_client, event)
    elif command == 'set-topic':
        MatchMakingBotModule.set_topic_channel(slack_client, event)
    else:
        blocks = [
            {
                "type": "section",
                "text": {
                    "type": "mrkdwn",
                    "text": "I can help you make a new Penny Chat! Type `/penny chat` to get started.\n"
                            "_More features coming soon..._"
                }
            }
        ]
        chat_postEphemeral_with_fallback(
            slack_client,
            channel=event['channel_id'],
            user=event['user_id'],
            blocks=blocks,
        )

    return HttpResponse('')
예제 #3
0
    def set_topic_channel(cls, slack, event):
        channel, created = TopicChannel.objects.get_or_create(
            channel_id=event['channel_id'],
            defaults={
                'slack_team_id': event['team_id'],
                'name': event['channel_name']
            })

        if created:
            text = 'This channel is now a Penny Chat topic channel. We will invite users to connect every few weeks.'
        else:
            text = 'This channel is already a topic channel.'

        chat_postEphemeral_with_fallback(
            slack,
            channel=event['channel_id'],
            user=event['user_id'],
            text=text,
        )
예제 #4
0
    def attendance_selection(self, event):
        """
        When a penny chat is shared, the user can click on "will attend" and "won't attend" buttons, and the resulting
        event is handled here.

        There are two side effects of this method:
         1. A Participant entry for the penny_chat and the user will be created or updated with the appropriate role.
         2. The organizer will be notified of "important" changes.
         3. The user will be told that the organizer will be notified.

         The specifics are rather complicated, but you can see how they work in
         bot.tests.processors.test_pennychat.test_PennyChatBotModule_attendance_selection

        """
        participant_role = Participant.ATTENDEE
        if event['actions'][0]['action_id'] == PENNY_CHAT_CAN_NOT_ATTEND:
            participant_role = None

        try:
            user = get_or_create_user_profile_from_slack_id(
                event['user']['id'],
                slack_client=self.slack_client,
            )
            action_value = json.loads(event['actions'][0]['value'])
            penny_chat_id = action_value[PENNY_CHAT_ID]
            penny_chat = PennyChat.objects.get(pk=penny_chat_id)

            organizer = UserProfile.objects.get(
                user_chats__penny_chat=penny_chat,
                user_chats__role=Participant.ORGANIZER,
            )

            if organizer == user:
                # TODO notify user that it's silly to attend or not attend their own event
                return

            # create organizer notification message (even if we choose not to use it below)
            timestamp = int(penny_chat.date.astimezone(utc).timestamp())
            date_text = f'<!date^{timestamp}^{{date_pretty}} at {{time}}|{penny_chat.date}>'
            _not = '' if participant_role == Participant.ATTENDEE else ' _not_'
            notification = f'<@{user.slack_id}> will{_not} attend your Penny Chat "{penny_chat.title}" ({date_text})'
            we_will_notify_organizer = 'Thank you. We will notify the organizer.'

            changed = False
            if participant_role:
                participant, created = Participant.objects.update_or_create(
                    user=user,
                    penny_chat=penny_chat,
                    defaults={'role': participant_role}
                )
                if created:
                    changed = True
            else:
                num_deleted, _ = Participant.objects.filter(user=user, penny_chat=penny_chat).delete()
                if num_deleted > 0:
                    changed = True

            if changed:
                self.slack_client.chat_postMessage(channel=organizer.slack_id, text=notification)
                chat_postEphemeral_with_fallback(
                    self.slack_client,
                    channel=event['channel']['id'],
                    user=user.slack_id,
                    text=we_will_notify_organizer,
                )
        except RuntimeError:
            chat_postEphemeral_with_fallback(
                self.slack_client,
                channel=event['channel']['id'],
                user=event['user']['id'],
                text="An error has occurred. Please try again in a moment.",
            )
            logging.exception('error in penny chat attendance selection')