Exemple #1
0
 def __init__(self, bot_client):
     self.bot_client = bot_client
     self.message_formatter = MessageFormatter()
     self.sym_message_parser = SymMessageParser()
     #hard code to the userId of bot you are using.
     self.bot_id = '349026222344891'
     self.default_message = self.message_formatter.format_message('type @karlPythonDemo help to view commands')
 def __init__(self, bot_client):
     self.bot_client = bot_client
     self.manager_submit = MessageFormatter().format_message(
         'Expense Report has been approved')
     self.approve_expense_message = MessageFormatter().format_message(
         'Your expense report has been approved')
     self.reject_expense_report = MessageFormatter().format_message(
         'Your expense report has been rejected')
Exemple #3
0
 def __init__(self, bot_client, msg):
     self.bot_client = bot_client
     self.msg = msg
     self.bot_id = '349026222344891'
     self.message_formatter = MessageFormatter()
     self.sym_message_parser = SymMessageParser()
     self.process(self.msg)
def save_image(bot_client, userId, expense):
    for name, date, price, description in expense:
        try:
            expense = ExpenseReport.objects(owner=str(userId), open=True)
            if expense:
                total = float(expense[0]['report_total'])
                total += float(price)
                expense.update(report_total=total)
                expense.update(
                    push__expenses=dict(expense_name=name,
                                        expense_date=date,
                                        expense_price=float(price),
                                        expense_description=description))
            else:
                newExpense = ExpenseReport(expenses=[
                    dict(expense_name=name,
                         expense_date=date,
                         expense_price=float(price),
                         expense_description=description)
                ],
                                           owner=str(userId),
                                           username=username,
                                           report_total=float(price),
                                           open=True).save()
        except ValueError:
            bot_client.get_message_client().send_msg(
                SymElementsParser().get_stream_id(action),
                MessageFormatter().format_message(
                    'Invalid price format, try again'))
def parse_attachment(msg, bot_client):
    attachment_body = bot_client.get_message_client().get_msg_attachment(msg['stream']['streamId'], msg['messageId'], msg['attachments'][0]['id'])
    decoded = base64.b64decode(attachment_body)
    response = client.detect_document_text(Document={'Bytes': decoded})

    text = ""
    for item in response["Blocks"]:
        if item["BlockType"] == "LINE":
            text = text + " " + item["Text"]

    entities = comprehend.detect_entities(LanguageCode="en", Text=text)
    print(entities)
    quantity = []
    try:
        for entity in entities["Entities"]:
            if entity.get("Type", "") == 'DATE':
                date = entity.get("Text")
            if entity.get("Type", "") == 'ORGANIZATION':
                description = entity.get("Text")
                org = entity.get("Text")
            if entity.get("Type", "") == 'QUANTITY':
                quantity.append(float(entity.get("Text").lstrip('$')))
        total = max(quantity)
    except ValueError:
        bot_client.get_message_client().send_msg(msg['stream']['streamId'], MessageFormatter().format_message('Invalid price format, try again'))
    return [(org, date, total, description)]
Exemple #6
0
class RoomProcessor:

    def __init__(self, bot_client):
        self.bot_client = bot_client
        self.message_formatter = MessageFormatter()
        self.sym_message_parser = SymMessageParser()
        #hard code to the userId of bot you are using.
        self.bot_id = '349026222344891'
        self.default_message = self.message_formatter.format_message('type @karlPythonDemo help to view commands')

    def process_room_message(self, msg):
        logging.debug('room_processor/process_room_message()')
        logging.debug(json.dumps(msg, indent=4))
        self.help_message = dict(message = """<messageML>
                                    <h2>This is an demo of how to create, update, and submit an expense form using Symphony Elements</h2>
                                    <p>Type @karlPythonDemo expense to view expense approval form</p>
                                              </messageML>
                            """)

        mentioned_users = self.sym_message_parser.get_mention_ids(msg)
        commands = self.sym_message_parser.get_text(msg)

        if mentioned_users:
            if mentioned_users[0] == self.bot_id and commands[0] == 'help':
                print('in room')
                self.bot_client.get_message_client().send_msg(msg['stream']['streamId'], self.help_message)
            if mentioned_users[0] == self.bot_id and commands[0] == 'expense':
                self.bot_client.get_message_client().send_msg(msg['stream']['streamId'], render_expense_approval_form('listeners/expense_approval_form/html/expense_approval_table.html'))

        else:
            self.bot_client.get_message_client().send_msg(msg['stream']['streamId'], self.default_message)
Exemple #7
0
    def process_im_action(self, action):
        
        logging.debug('action_processor/im_process')
        logging.debug(json.dumps(action, indent=4))

        if SymElementsParser().get_form_values(action)['action'] == 'submit_button':
            self.bot_client.get_message_client().send_msg(SymElementsParser().get_stream_id(action), self.action_processed_message)
            form_contents = SymElementsParser().get_form_values(action)
            print(form_contents)
            self.response_message = MessageFormatter().format_message('Captured: {}'.format(form_contents))
            self.bot_client.get_message_client().send_msg(SymElementsParser().get_stream_id(action), self.response_message)
    def __init__(self, bot_id):
        self.finance_approval_message = dict(message="""<messageML>
                                            <span><p>Your Expense was approved, waiting on finance approval.</p></span>
                                       </messageML>""".format(bot_id))

        self.help_message = dict(message="""<messageML>
                                    <h3>Use ExpenseBot to create, update, and submit an expense form using Symphony Elements</h3>
                                        <ul>
                                            <li><mention uid="{0}"/> help</li>
                                            <li><mention uid="{0}"/> create</li>
                                            <li><mention uid="{0}"/> end</li>
                                        </ul>
                                </messageML>
                    """.format(bot_id))

        self.clear_message = dict(message="""<messageML>
                                        <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
                                      </messageML>""")

        self.create_message = dict(message="""<messageML>
                                          <form id="form_id">
                                            <span><p>Create and fill up the report and I'll send it over!</p></span>
                                            <br />
                                            <div style='padding-top:1px;padding-left:5px;'>
                                                <button name="create-report" type="action">CREATE REPORT</button>
                                            </div>
                                          </form>
                                       </messageML>""".format(bot_id))

        self.manager_submit_message = MessageFormatter().format_message(
            'Now I need to know who is the finance responsible for the report and reimbursement.'
        )
        self.start_report_message = MessageFormatter().format_message(
            'Create and fill up the report then I will send it over!')
        self.choose_boss_message = MessageFormatter().format_message(
            'Please select your manager and I will send them your expense report'
        )
        self.review_message = MessageFormatter().format_message(
            'Here is the expense report, please review and send your response.'
        )
        self.room_message = MessageFormatter().format_message(
            'You can only interact with a bot inside an IM, please open an IM chat with me!'
        )
        self.reject_message = MessageFormatter().format_message(
            'Expenses Rejected, contact your manager')
        self.end_message = dict(
            message=
            """<messageML><p>You already have an active report, type <mention uid="{0}"/> end</p></messageML>"""
            .format(bot_id))
        self.instruction_message = dict(
            message=
            """<messageML><p>Type <mention uid="{0}"/> create</p></messageML>"""
            .format(bot_id))
Exemple #9
0
 def __init__(self, bot_client):
     self.bot_client = bot_client
     self.action_processed_message = MessageFormatter().format_message('Your action has been processed')
Exemple #10
0
 def __init__(self, bot_client):
     self.bot_client = bot_client
     self.message_formatter = MessageFormatter()
     self.sym_message_parser = SymMessageParser()
     self.stream_client = StreamClient(bot_client)
    def process_im_action(self, action):
        logging.debug('action_processor/im_process')
        logging.debug(json.dumps(action, indent=4))
        if SymElementsParser().get_form_values(action)['action'] == 'create-report':
            self.bot_client.get_message_client().send_msg(SymElementsParser().get_stream_id(action), render_expense_approval_form(action, './listeners/expense_approval_form/html/create_expense_approval_form.html'))

        elif SymElementsParser().get_form_values(action)['action'] == 'add-expense-form':
            self.bot_client.get_message_client().send_msg(SymElementsParser().get_stream_id(action), render_add_expense_form(action, './listeners/expense_approval_form/html/add_expense_form.html'))

        elif SymElementsParser().get_form_values(action)['action'] == 'add-expense-button':
            form_contents = SymElementsParser().get_form_values(action)
            save_expense(self.bot_client, action, [(form_contents['add-vendor-textfield'], form_contents['add-date-textfield'], form_contents['add-price-textfield'], form_contents['add-description'])])
            self.bot_client.get_message_client().send_msg(SymElementsParser().get_stream_id(action), render_expense_approval_form(action, './listeners/expense_approval_form/html/create_expense_approval_form.html'))

        elif SymElementsParser().get_form_values(action)['action'] == 'approve':
            self.bot_client.get_message_client().send_msg(SymElementsParser().get_stream_id(action), self.messages.choose_boss_message)
            self.bot_client.get_message_client().send_msg(SymElementsParser().get_stream_id(action), render_select_boss_form(action, './listeners/expense_approval_form/html/select_boss.html'))

        elif SymElementsParser().get_form_values(action)['action'] == 'send-report':
            self.employee_id = SymElementsParser().get_initiator_user_id(action)
            self.employee_name = SymElementsParser().get_initiator_display_name(action)
            self.employee_stream = SymElementsParser().get_stream_id(action)

            self.manager_id = SymElementsParser().get_form_values(action)['person-selector']
            self.manager_username = self.bot_client.get_user_client().get_user_from_id(self.manager_id)['displayName']
            expense = ExpenseReport.objects(owner=str(self.employee_id), open=True)
            expense.update(manager=str(self.manager_id[0]))

            self.submit_message = MessageFormatter().format_message('Your expense has been submitted to {}'.format(self.manager_username) + ' I will inform you when your manager answers')
            self.manager_recieve_message = dict(message = """<messageML>
                                                        <span><p>Hey <mention uid="{}"/>, you have one expense report from <mention uid="{}"/> </p></span>
                                                   </messageML>""".format(self.manager_id[0], self.employee_id))

            self.bot_client.get_message_client().send_msg(self.employee_stream, self.submit_message)
            self.im_stream = self.bot_client.get_stream_client().create_im(SymElementsParser().get_form_values(action)['person-selector'])

            self.bot_client.get_message_client().send_msg(self.im_stream['id'], self.manager_recieve_message)
            self.bot_client.get_message_client().send_msg(self.im_stream['id'], self.messages.review_message)
            self.bot_client.get_message_client().send_msg(self.im_stream['id'], render_expense_approval_form(action, './listeners/expense_approval_form/html/manager_expense_approval_form.html'))

        elif SymElementsParser().get_form_values(action)['action'].startswith('approve-expense'):
            user_id = SymElementsParser().get_form_values(action)['action'].split()[1]
            self.manager_stream = SymElementsParser().get_stream_id(action)
            self.bot_client.get_message_client().send_msg(self.manager_stream, self.messages.manager_submit_message)
            self.bot_client.get_message_client().send_msg(SymElementsParser().get_stream_id(action), render_select_finance_form('./listeners/expense_approval_form/html/select_finance.html'))

            expense = ExpenseReport.objects(open=True, owner=str(user_id), manager=str(self.manager_id[0]))[0]
            logging.debug(expense.to_json())
            expense.open=False
            expense.save()
            logging.debug(expense.to_json())

        elif SymElementsParser().get_form_values(action)['action'] == 'send-finance':
            self.finance_id = SymElementsParser().get_form_values(action)['person-selector']
            self.send_finance_message = dict(message = """<messageML>
                                                        <span><p>Thanks, I'll share this with <mention uid="{}"/></p></span>
                                                   </messageML>""".format(self.finance_id[0]))
            self.bot_client.get_message_client().send_msg(SymElementsParser().get_stream_id(action), self.send_finance_message)

            self.bot_client.get_message_client().send_msg(self.employee_stream, self.messages.finance_approval_message)

        elif SymElementsParser().get_form_values(action)['action'].startswith('remove'):
            expense_name = SymElementsParser().get_form_values(action)['action'].split()[-1]
            expense_price = float(SymElementsParser().get_form_values(action)['action'].split()[1])
            remove_item(action, expense_name, expense_price)
            self.bot_client.get_message_client().send_msg(SymElementsParser().get_stream_id(action), render_expense_approval_form(action, './listeners/expense_approval_form/html/create_expense_approval_form.html'))

        elif SymElementsParser().get_form_values(action)['action'] == 'reject-expense':
            self.bot_client.get_message_client().send_msg(self.employee_stream, self.messages.reject_message)
    def process_im_action(self, action):
        logging.debug('action_processor/im_process')
        logging.debug(json.dumps(action, indent=4))
        # self.im_stream = self.bot_client.get_stream_client().create_im([SymElementsParser().get_initiator_user_id(action)])
        if SymElementsParser().get_form_values(
                action)['action'] == 'add-expense-form':
            self.bot_client.get_message_client().send_msg(
                SymElementsParser().get_stream_id(action),
                render_add_expense_form(
                    'listeners/expense_approval_form/html/add_expense_form.html'
                ))

        elif SymElementsParser().get_form_values(
                action)['action'] == 'add-expense-button':
            form_contents = SymElementsParser().get_form_values(action)
            upload_expense([(form_contents['add-vendor-textfield'],
                             form_contents['add-date-textfield'],
                             float(form_contents['add-price-textfield']))])
            self.bot_client.get_message_client().send_msg(
                SymElementsParser().get_stream_id(action),
                render_expense_approval_form(
                    'listeners/expense_approval_form/html/create_expense_approval_form.html'
                ))

        elif SymElementsParser().get_form_values(
                action)['action'] == 'submit-expense':
            self.employee_id = SymElementsParser().get_initiator_user_id(
                action)
            self.employee_name = SymElementsParser(
            ).get_initiator_display_name(action)
            self.employee_stream = SymElementsParser().get_stream_id(action)

            self.manager_id = SymElementsParser().get_form_values(
                action)['person-selector']
            self.manager_username = self.bot_client.get_user_client(
            ).get_user_from_id(self.manager_id)['displayName']

            self.submit_message = MessageFormatter().format_message(
                'Your expense has been submitted to {}'.format(
                    self.manager_username))
            self.manager_recieve_message = MessageFormatter().format_message(
                '{} submitted an expense report that needs your approval: '.
                format(self.employee_name))

            self.bot_client.get_message_client().send_msg(
                self.employee_stream, self.submit_message)
            self.im_stream = self.bot_client.get_stream_client().create_im(
                SymElementsParser().get_form_values(action)['person-selector'])

            self.bot_client.get_message_client().send_msg(
                self.im_stream['id'], self.manager_recieve_message)
            self.bot_client.get_message_client().send_msg(
                self.im_stream['id'],
                render_expense_approval_form(
                    'listeners/expense_approval_form/html/manager_expense_approval_form.html'
                ))

        elif SymElementsParser().get_form_values(
                action)['action'] == 'approve-expense':
            self.manager_stream = SymElementsParser().get_stream_id(action)
            self.bot_client.get_message_client().send_msg(
                self.manager_stream, self.manager_submit)
            self.bot_client.get_message_client().send_msg(
                self.employee_stream, self.approve_expense_message)