Example #1
0
def message():
    """
    Handle incoming USSD messages, passed as HTTP requests via the vumi HTTP API.
    """

    logger.debug("MESSAGE endpoint called")

    # TODO: filter out service messages, such as "content": "state: wait_ussrc"

    if request.method == 'POST':

        tmp = request.get_json()
        msg = VumiMessage(tmp)
        if msg.content == "state: wait_ussrc":
            logger.debug("End of session message received.")
        else:
            logger.debug(msg)
            try:
                user_id = msg.from_addr  # user's cellphone number
                content = msg.content  # selected menu item, if any
                mark_online(user_id)
                selected_item = None
                try:
                    selected_item = int(content)
                except (ValueError, TypeError):
                    pass
                reply_content = generate_output(user_id, selected_item)
                if app.debug:
                    logger.debug(reply_content)
                else:
                    msg.reply(reply_content)
            except Exception as e:
                logger.exception(e)
                pass
    return make_response("OK")
Example #2
0
def response():
    """
    Send SMS response to an SMS query.
    """

    logger.debug("RESPONSE endpoint called")

    user = current_user
    content = request.form['content']
    query_id = request.form['query_id']

    # send reply
    msg = VumiMessage({'query_id': query_id})
    msg.send_reply(content, session_event=None, user=user)

    # update query status
    qry = Query.query.get(query_id)
    qry.status = "in_progress"
    db.session.add(qry)
    db.session.commit()

    return redirect('/admin/queryview/', code=302)
Example #3
0
def message():
    """
    Handle incoming messages, passed as HTTP requests via the vumi HTTP API.
    """

    logger.debug("MESSAGE endpoint called")

    try:
        tmp = request.get_json()
        msg = VumiMessage(tmp)
        if hasattr(msg, 'content') and msg.content == "state: wait_ussrc":
            logger.debug("End of session message received.")
        else:
            logger.debug(msg.msg_type + " message received.")
            if msg.msg_type == "ussd":
                user_id = msg.from_addr  # user's cellphone number
                content = msg.content  # selected menu item, if any
                mark_online(user_id)
                selected_item = None
                try:
                    selected_item = int(content)
                except (ValueError, TypeError):
                    pass
                logger.debug(selected_item)
                reply_content = generate_output(user_id, selected_item)
                if "Your number has been added to the list." in reply_content:
                    update_notification_list(msg.from_addr, "add")
                msg.send_reply(reply_content)
            elif msg.msg_type == "sms":
                msg.save_query()
                tmp = "Thank you for submitting your query. It will be attended to as soon as possible."
                msg.send_reply(tmp)
            else:
                logger.error("Incorrect message type encountered.")

    except Exception as e:
        logger.exception(e)
        raise
    return make_response("OK")