Esempio n. 1
0
def handle_message(event):
    # When receiving special msg, inspect admin identity, and invoke admin cmd
    adminHandler = adminMsgHandlerMapping.get(event.message.text)
    if adminHandler and invokeLambda(LAMBDAS.AIRTABLE, {
            'eventType': AIR_EVENT_TYPES.IS_ADMIN,
            'lineUserId': event.source.user_id
    }):
        logger.info(LogMsg(adminHandler, stage=HS.STARTING))
        adminHandler(event)
        logger.debug(LogMsg(adminHandler, stage=HS.FINISHED))

    # If not admin command, respond with default
    else:
        logger.info(
            LogMsg(MessageEvent,
                   stage=HS.STARTING,
                   msgContent=event.message.text))

        # TODO: Better default reply
        # Default reply replicates the incoming message
        invokeLambda(
            LAMBDAS.LINE, {
                'eventType': LINE_EVENT_TYPES.REPLY,
                'lineReplyToken': event.reply_token,
                'replyMessage': event.message.text
            })
        logger.debug(LogMsg(MessageEvent, stage=HS.FINISHED))
def cmd_classHistory(event):
    '''
    Success response =
    [{'Status': 'handle_classHistory: OK', 'Data': [{
        'classIid': 'recvQFMu2DOSqwuBm', 'classTime': '1226', 'classLocation': '學員家', 'classDate': '0900', 'classTrainer': 'James'}, {
        'classIid': 'recNO0A5FQCxMopYZ', 'classTime': '0927', 'classLocation': '學員主要戶外場地', 'classDate': '0930', 'classTrainer': 'James'
    }]}]
    '''
    # Get class history
    resPayload = invokeLambda(LAMBDAS.AIRTABLE, {
        'eventType': AIR_EVENT_TYPES.CLASS_HISTORY,
        'lineUserId': event.source.user_id
    })
    resData = resPayload and resPayload[0]['Data']
    logger.debug('Class history info: {}'.format(resData))

    # Reply to the request
    invokeLambda(LAMBDAS.LINE, {
        'lineReplyToken': event.reply_token,
        **({
            'eventType': LINE_EVENT_TYPES.REPLY_FLEX,
            'replyTemplate': LINE_MESSAGE_TEMPLATES.CLASS_HISTORY,
            'replyContent': resData
        } if resData else {
            'eventType': LINE_EVENT_TYPES.REPLY,
            'replyMessage': LINE_MESSAGE_TEXTS.CLASS_HISTORY_NO_RECORD
        })
    })
    logger.debug('Replied with class history info.')
Esempio n. 3
0
def handle_follow(event):
    logger.info(LogMsg(FollowEvent, stage=HS.STARTING))
    resPayload = invokeLambda(
        LAMBDAS.LINE, {
            'eventType': LINE_EVENT_TYPES.GET_PROFILE,
            'lineUserId': event.source.user_id
        })
    userDisplayName = resPayload['Data']['displayName']
    userProfilePic = resPayload['Data']['pictureUrl']

    logger.debug(
        LogMsg(FollowEvent,
               stage=HS.IN_PROCESS,
               userDisplayName=userDisplayName))
    invokeLambda(
        LAMBDAS.LINE, {
            'eventType': LINE_EVENT_TYPES.REPLY,
            'lineReplyToken': event.reply_token,
            'replyMessage': LINE_MESSAGE_TEXTS.FOLLOW_GREETING,
            'replyContent': {
                'userName': userDisplayName
            }
        })

    invokeLambda(
        LAMBDAS.AIRTABLE, {
            'eventType': AIR_EVENT_TYPES.FOLLOW,
            'lineUserId': event.source.user_id,
            'lineDisplayName': userDisplayName,
            'lineProfilePic': userProfilePic
        })
    logger.debug(LogMsg(FollowEvent, stage=HS.FINISHED))
def cmd_homework(event):
    '''
    Success response =
    [{'Status': 'handle_homework: OK', 'Data': [{
        hwIid: 'rec123456',
        noOfSet: 3,
        personalTip: '越像蟲越好',
        image: 'https://dl.airtable.com/.attachmentThumbnails/2e4132d7206ae8edddc79c6fd9525e78/62d6d3c6',
        video: 'https://www.youtube.com/watch?v=t_qk5ZhRHIs',
        isFinished: false,
        member: 'Bingo',
        baseMove: '滾筒按摩'
    }]}]
    '''
    # Get homework info
    resPayload = invokeLambda(LAMBDAS.AIRTABLE, {
        'eventType': AIR_EVENT_TYPES.HOMEWORK,
        'lineUserId': event.source.user_id
    })
    resData = resPayload and resPayload[0]['Data']
    logger.debug('Homework info: {}'.format(resData))

    # Reply to the request
    invokeLambda(LAMBDAS.LINE, {
        'lineReplyToken': event.reply_token,
        **({
            'eventType': LINE_EVENT_TYPES.REPLY_CAROUSEL,
            'replyTemplate': LINE_MESSAGE_TEMPLATES.HOMEWORK,
            'replyContent': resData
        } if resData else {
            'eventType': LINE_EVENT_TYPES.REPLY,
            'replyMessage': LINE_MESSAGE_TEXTS.HOMEWORK_NO_RECORD
        })
    })
    logger.debug('Replied with homework info.')
def cmd_nextClass(event):
    '''
    Success response =
    [{'Status': 'handle_nextClass: OK', 'Data': {'memberIid': 'recMgb6f5sfuhVWAs', 'classId': '1900322', 'classTime': '1114 09:00', 'classLocation': 'home', 'classTrainer': 'CY'}}]
    '''
    # Get next class info
    resPayload = invokeLambda(LAMBDAS.AIRTABLE, {
        'eventType': AIR_EVENT_TYPES.NEXT_CLASS,
        'lineUserId': event.source.user_id
    })
    resData = resPayload and resPayload[0]['Data']
    logger.debug('Next class info: {}'.format(resData))

    # Reply to the message
    invokeLambda(LAMBDAS.LINE, {
        'eventType': LINE_EVENT_TYPES.REPLY,
        'lineReplyToken': event.reply_token,
        **({
            'replyMessage': LINE_MESSAGE_TEXTS.NEXT_CLASS_RECORD,
            'replyContent': resData
        } if resData else {
            'replyMessage': LINE_MESSAGE_TEXTS.NEXT_CLASS_NO_RECORD
        })
    })
    logger.debug('Replied with next class info.')
def btn_classRecord(event):
    '''
    Success response =
    [{'Status': 'handle_classRecord: OK', 'Data': [{'baseMove': '橋式 Bridge'}, {'baseMove': '死蟲 Dead Bug'}, {'performanceRec': '四足跪姿;膝支撐平板夾背', 'image': 'https://dl.airtable.com/.attachmentThumbnails/2e4132d7206ae8edddc79c6fd9525e78/62d6d3c6', 'video': 'http://youtube.com', 'baseMove': '滾筒按摩'}]
    }]
    '''
    # Get class record
    resPayload = invokeLambda(LAMBDAS.AIRTABLE, {
        'eventType': AIR_EVENT_TYPES.CLASS_RECORD,
        'classIid': re.search('classIid=(.+?)(;|$)', event.postback.data)[1]
    })
    resData = resPayload and resPayload[0]['Data']
    logger.debug('Class record info: {}'.format(resData))

    # Reply to the request
    invokeLambda(LAMBDAS.LINE, {
        'lineReplyToken': event.reply_token,
        **({
            'eventType': LINE_EVENT_TYPES.REPLY_FLEX,
            'replyTemplate': LINE_MESSAGE_TEMPLATES.CLASS_RECORD,
            'replyContent': resData
        } if resData else {
            'eventType': LINE_EVENT_TYPES.REPLY,
            'replyMessage': LINE_MESSAGE_TEXTS.CLASS_RECORD_NO_RECORD
        })
    })
    logger.debug('Replied with class record info.')
Esempio n. 7
0
def adm_reminder(event):
    '''
    Success response =
    [{'Status': 'handle_reminder: OK', 'Data': [{'iid': 'recGPvFMiUDaoO4', 'lineUserId': 'U9ae6458c650504a3e8380a1046e0f', 'lineDisplayName': 'CY', 'messageTime': '2019-10-28T13:13:00.000Z', 'messageContent': "Hello, this is a response from air."}]}]
    '''
    # Get targets to send reminder
    resPayload = invokeLambda(LAMBDAS.AIRTABLE,
                              {'eventType': AIR_EVENT_TYPES.REMINDER})
    resData = resPayload and resPayload[0]['Data']
    logger.debug('Targets to send reminder: {}'.format(resData))

    # Send reminders to the targets
    if resData:
        remindedInd = []
        for target in resData:
            invokeLambda(
                LAMBDAS.LINE, {
                    'eventType': LINE_EVENT_TYPES.PUSH,
                    'lineUserId': target['lineUserId'],
                    'pushMessage': target['messageContent']
                })
            remindedInd.append(target['lineDisplayName'])
    logger.debug('Pushed reminders to targets.')

    # Reply to the admin
    invokeLambda(
        LAMBDAS.LINE, {
            'eventType':
            LINE_EVENT_TYPES.REPLY,
            'lineReplyToken':
            event.reply_token,
            **({
                'replyMessage': LINE_MESSAGE_TEXTS.REMINDER_SUCCESS,
                'replyContent': {
                    'remindedInds': ', '.join(remindedInd)
                }
            } if resData else {
                   'replyMessage': LINE_MESSAGE_TEXTS.REMINDER_NO_TARGET
               })
        })
    logger.debug('Replied with sent reminder target ids.')
def btn_finishHomework(event):
    invokeLambda(LAMBDAS.AIRTABLE, {
        'eventType': AIR_EVENT_TYPES.FINISH_HOMEWORK,
        'hwIid': re.search('hwIid=(.+?)(;|$)', event.postback.data)[1]
    })
    logger.debug('Updated homework finish status.')