async def submit_answers(collection: AsyncIOMotorCollection, q_id: str, answers: List[str]) -> None: user = get_current_user_id() assert user is not None, 'Must be called from any Slack context' questionnaire = await get_questionnaire_by_id(collection, q_id) assert questionnaire is not None, "Couldn't find questionnaire by _id" assert user in _get_not_answered_recipients(questionnaire) after = await update_questionnaire_answers(collection, q_id, user, answers) await _send_message( f'Your feedback on *"{after["title"]}"* has been submitted!', channel_id=user, ) blocks = build_submitted_feedback_message(user, after['questions'], answers) await _send_message(blocks[0]['text']['text'], blocks, after['user_id'], after['ts']) if not _get_not_answered_recipients(after): await _send_message('All recipients have sent their answers! :tada:', channel_id=after['user_id'], thread_ts=after['ts'], reply_broadcast=True)
async def _send_message_to_user(text: str, blocks: Optional[List[Dict]] = None) -> None: await async_slack_request(method='chat_postEphemeral', payload={ 'text': text, 'blocks': blocks, 'user': get_current_user_id(), 'channel': get_current_channel_id() })
async def send_ephemeral(text: str) -> None: user = get_current_user_id() log.info(f'Sending ephemeral to user {user}: {text}') await async_slack_request(method='chat_postEphemeral', payload={ 'text': text, 'user': user, 'channel': get_current_channel_id() })
async def create_questionnaire(collection: AsyncIOMotorCollection, title: str, recipients: List[str], questions: List[str]) -> None: user = get_current_user_id() assert user is not None, 'Must be called from any Slack context' q_id = await save_questionnaire(collection, user, title, recipients, questions) ts = await _send_control_message(title, recipients, questions, q_id) await update_questionnaire_ts(collection, q_id, ts) await notify_recipients(collection, q_id)
async def _send_message( text: str, blocks: Optional[List[Dict]] = None, channel_id: Optional[str] = None, thread_ts: Optional[str] = None, reply_broadcast: Optional[bool] = None) -> Optional[str]: if channel_id is None: channel_id = get_current_user_id() log.info(f'Sending message to channel {channel_id}: {text}') resp = await async_slack_request(method='chat_postMessage', payload={ 'text': text, 'channel': channel_id, 'blocks': blocks, 'thread_ts': thread_ts, 'reply_broadcast': reply_broadcast }) return resp.get('ts')
async def open_answer_view(collection: AsyncIOMotorCollection, q_id: str) -> None: metadata = action_metadata.get() assert metadata and metadata.actions, 'Must be called from action context' questionnaire = await get_questionnaire_by_id(collection, q_id) assert questionnaire is not None, "Couldn't find questionnaire by _id" user = get_current_user_id() if user not in _get_not_answered_recipients(questionnaire): return await send_ephemeral( 'You have already sent your feedback on this questionnaire.') await async_slack_request(method='views_open', payload={ 'trigger_id': metadata.trigger_id, 'view': build_answer_view(questionnaire['title'], questionnaire['questions'], q_id) })
def test_get_current_user_id_no_context() -> None: user_id = get_current_user_id() assert user_id is None
def test_get_current_user_id_action(test_action_metadata: Dict, ) -> None: user_id = get_current_user_id() assert user_id == test_action_metadata['user']['id']
def test_get_current_user_id_command(test_command_metadata: Dict, ) -> None: user_id = get_current_user_id() assert user_id == test_command_metadata['user_id']