Beispiel #1
0
def run(content_config, env, variables):
    """
    content_config schema:

    Platform independent message:
    {
        'messages': [Message, ...]
    }

    Platform dependent message:
    {
        'message': {
            'Facebook': [Message,  ... ]
            'Line': [Message, ... ]
            ...
        }
    }
    """
    messages = content_config.get('messages')
    msgs = []

    if not isinstance(messages, list):
        platform_type = env['platform_type'].value
        messages = messages[platform_type]

    for message in messages:
        msgs.append(Message.FromDict(message, variables))

    for quick_reply in content_config.get('quick_replies', []):
        msgs[-1].add_quick_reply(Message.QuickReply.FromDict(quick_reply))

    return msgs
Beispiel #2
0
def run(content_config, unused_env, variables):
    msgs = []  # The output messages.

    url = Render(content_config['url'], variables)
    method = content_config.get('method', 'post')
    params = content_config.get('params', [])
    params = [
        (Render(x[0], variables), Render(x[1], variables))
        for x in params]

    try:
        js = FetchData(url=url, params=params, method=method)

        for m in js:
            msg = Message.FromDict(m)
            msgs.append(msg)

    except Exception as e:
        _LOG.exception(e)
        msgs.append(Message(u'我好像壞掉了,快通知管理員。'))

    return msgs
Beispiel #3
0
def run(parser_config, user_input, unused_as_root):
    """
    {
      "links": [
        {
          "action_ident": "continue",
          "end_node_id": null,
          "ack_message": ""
        },
        {
          "action_ident": "done",
          "end_node_id": "[[root]]",
          "ack_message": ""
        }
      ]
    }
    """
    if user_input.text:
        if u'重設' in user_input.text or 'reset' in user_input.text.lower():
            Memory.Clear()
            return ParseResult(ack_message=u'讓我們重新開始吧!')

    if user_input.event:
        event = user_input.event
        if event.key == 'CONTROL_FLOW':
            if event.value == 'reset':
                Memory.Clear()
                return ParseResult(parser_config['done'], ack_message=u'放棄操作')
        elif event.key == 'SELECT_BOT':
            Memory.Set('bot', event.value)
        elif event.key == 'SELECT_OP':
            Memory.Set('operation', event.value)
        elif event.key == 'MESSAGE_INPUT':
            if event.value == 'done':
                Memory.Set('status', 'preview_message')
            elif event.value == 'restart':
                Memory.Set('broadcast_message', None)
                Memory.Set('status', None)
        elif event.key == 'CONFIRM_MESSAGE':
            if event.value:
                bot = Bot.get_by(id=Memory.Get('bot'), single=True)
                if not bot:
                    return ParseResult(ack_message=u'發生錯誤,找不到對應的' u'機器人')
                msgs = [
                    Message.FromDict(raw_msg)
                    for raw_msg in Memory.Get('broadcast_message')
                ]
                BroadcastMessage(bot, msgs)
                Memory.Clear()
                return ParseResult(parser_config['done'],
                                   ack_message=u'您的訊息已送出!')
            else:
                Memory.Set('broadcast_message', None)

    status = Memory.Get('status')
    if status == 'input_broadcast_message':
        broadcast_message = Memory.Get('broadcast_message') or []
        broadcast_message.append(user_input.raw_message)
        Memory.Set('broadcast_message', broadcast_message)

    return ParseResult(skip_content_module=False)
Beispiel #4
0
def run(content_config, unused_env, variables):
    """
    content_config schema:
    {
        "bot_admins": {
            "platform_user_ident_1": ["bot_id_1", "bot_id_2" ..],
            ...
        }
    }
    """
    user = g.user

    bots = content_config['bot_admins'].get(user.platform_user_ident)
    if not bots:
        return []

    bot = Memory.Get('bot')
    if not bot:
        msgs = [
            Message(u'嗨 {{user.first_name}},你想要操作哪隻機器人呢?', variables=variables)
        ]
        page = 1
        m = Message()
        msgs.append(m)
        bubble = Message.Bubble('第 %d 頁' % page)

        for bot_id in bots:
            bot = Bot.get_by(id=bot_id, single=True)
            if not bot:
                continue

            bubble.add_button(
                Message.Button(Message.ButtonType.POSTBACK,
                               title=bot.name,
                               payload=EventPayload('SELECT_BOT', bot_id)))

            if len(bubble.buttons) == 3:
                m.add_bubble(bubble)
                page += 1
                bubble = Message.Bubble(u'第 %d 頁' % page)

        if len(bubble.buttons):
            m.add_bubble(bubble)

        msgs[-1].add_quick_reply(
            Message.QuickReply(Message.QuickReplyType.TEXT,
                               u'放棄',
                               payload=EventPayload('CONTROL_FLOW', 'reset'),
                               acceptable_inputs=['(?i)giveup', '(?i)reset']))
        return msgs

    operation = Memory.Get('operation')
    if not operation:
        m = Message(buttons_text=u'你想要執行什麼動作呢?')
        m.add_button(
            Message.Button(Message.ButtonType.POSTBACK,
                           title=u'廣播訊息',
                           payload=EventPayload('SELECT_OP', 'broadcast')))
        m.add_button(
            Message.Button(Message.ButtonType.POSTBACK,
                           title=u'放棄',
                           payload=EventPayload('CONTROL_FLOW', 'reset')))
        return [m]

    if operation == 'broadcast':
        broadcast_message = Memory.Get('broadcast_message')
        status = Memory.Get('status')
        if status == 'input_broadcast_message':
            m = Message(u'你可以繼續輸入下一則訊息:')
            m.add_quick_reply(
                Message.QuickReply(
                    Message.QuickReplyType.TEXT,
                    u'完成',
                    payload=EventPayload('MESSAGE_INPUT', 'done'),
                    acceptable_inputs=[u'好了', u'(?i)done', '(?i)y']))
            m.add_quick_reply(
                Message.QuickReply(
                    Message.QuickReplyType.TEXT,
                    u'重來',
                    payload=EventPayload('MESSAGE_INPUT', 'restart'),
                    acceptable_inputs=[u'好了', u'(?i)restart', '(?i)cancel']))
            m.add_quick_reply(
                Message.QuickReply(
                    Message.QuickReplyType.TEXT,
                    u'放棄',
                    payload=EventPayload('CONTROL_FLOW', 'reset'),
                    acceptable_inputs=['(?i)giveup', '(?i)reset']))
            return [m]
        elif not broadcast_message:
            Memory.Set('status', 'input_broadcast_message')
            return [Message(u'請輸入你要廣播的訊息:')]
        elif status == 'preview_message':
            msgs = [Message(u'請確認你要廣播的訊息:')]
            for raw_msg in broadcast_message:
                msgs.append(Message.FromDict(raw_msg))

            m = msgs[-1]
            m.add_quick_reply(
                Message.QuickReply(Message.QuickReplyType.TEXT,
                                   u'確認',
                                   payload=EventPayload(
                                       'CONFIRM_MESSAGE', True),
                                   acceptable_inputs=[u'是', '(?i)y',
                                                      '(?i)ok']))
            m.add_quick_reply(
                Message.QuickReply(
                    Message.QuickReplyType.TEXT,
                    u'取消',
                    payload=EventPayload('CONFIRM_MESSAGE', False),
                    acceptable_inputs=[u'是', '(?i)no', '(?i)cancel']))

            return msgs

    return [Message(u'錯誤的操作')]