def post_method(url, header, body, response, i):
    print("this is post method")
    #print(header,"**********************")
    #request_json = json.loads(body)
    header_json = header

    if (isinstance(header_json, str)):
        header_json = json.loads(header)

    #header_string = json.dumps(header)
    #print(type(header_string))
    #json_header = json.dumps(header_string)
    #print(type(header))
    #print(header)
    response_after_post = requests.post(url, body, headers=header)
    print(response_after_post.json())
    print(response_after_post.status_code)
    #checking status code
    if (response_after_post.status_code == 201
            or response_after_post.status_code == 200):
        status_post = "posted succussfully"
    else:
        status_post = "fail to post"
    #print(status_post)

    #writing status code
    update(i, status_post)
def delete_method(url, header, body, response, i):
    response_after_delete = requests.delete(url, headers=header)
    # fetch responce code
    print(response_after_delete.status_code)
    if (response_after_delete.status_code == 204
            or response_after_delete.status_code == 200):
        status_delete = "successfully deleted"
    else:
        status_delete = "fail to delete"
    #print(status_delete)

    #writing status code
    update(i, status_delete)
def update(request, html_cache_id):
    """Update record"""

    data, code, valid = validator.update(request, html_cache_id)

    if valid:
        if settings.ENV == 'production':
            try:
                data, code, item = resource.update(request, html_cache_id)
            except:
                return {'code': 'html_cache/update/fail'}, 404
        else:
            data, code, item = resource.update(request, html_cache_id)

    return data, code
def update(request, tag_id):
    """Update record"""

    data, code, valid = validator.update(request)

    if valid:
        if settings.ENV == "production":
            try:
                data, code, item = resource.update(request, tag_id)
            except:
                return {"code": "tag/update/fail"}, 404
        else:
            data, code, item = resource.update(request, tag_id)

    return data, code
def update(request):
    """Update record"""

    data, code, valid = validator.update(request)

    if valid:
        if settings.ENV == 'production':
            try:
                data, code, user = resource.update(request)
            except:
                return {'code': 'account/update/fail'}, 404
        else:
            data, code, user = resource.update(request)

    return data, code
def put_method(url, header, body, response, i):
    print("this is put method")
    requests_json_put = json.loads(body)
    print(type(header))
    #time.sleep(5)
    response_after_put = requests.put(url, body, headers=header)
    print(response_after_put.json())
    print(response_after_put.status_code)

    #checking status code
    if (response_after_put.status_code == 200):
        status_put = "modified succfully"
    else:
        status_put = "fail to modify"
    #print(status_put)

    #writing status code
    update(i, status_put)
def get_method(url, header, body, response, i):

    #print(type(header))
    print("This is get request")
    #print(header,"**************************")

    response_after_get = requests.get(url, headers=header)

    print(response_after_get.json())
    #json.dumps(header)
    #print(type(header))

    print(response_after_get.status_code)
    if (response_after_get.status_code == 200):
        status_get = "get response successfully"
    else:
        status_get = "fail to get response"
    #print(status_get)

    #writing status code
    update(i, status_get)
def post_method_login(url, header, body, response, i):
    #print(type(header))
    print("this is post method")
    #print(header,"**********************")
    request_json = json.loads(body)
    header_json = header

    if (isinstance(header_json, str)):
        header_json = json.loads(header)

    header_string = json.dumps(header)
    #print(type(header_string))
    json_header = json.dumps(header_string)
    #print(header_string,'**')
    #response_after_post = requests.post(url, request_json, headers=header_json)
    #print(type(header_json))
    #print(type(header))
    response_after_post = requests.post(url, body, headers=header_json)
    print(response_after_post.json())
    #if('Authorization' in response_after_post.headers):
    token_recieve_from_header_response = response_after_post.headers[
        'Authorization']
    user_Id = request_json['emailId']
    local_data = {
        'token': token_recieve_from_header_response,
        'userId': user_Id
    }

    print(response_after_post.status_code)
    #checking status code
    if (response_after_post.status_code == 201
            or response_after_post.status_code == 200):
        status_post = "posted succussfully"
    else:
        status_post = "fail to post"
    #print(status_post)

    #writing status code
    update(i, status_post)

    ####################################################
    #after this post(login) every api get call
    ####################################################
    #print(data,"********************")
    print(row_count)
    for i in range(1, row_count):
        print(i + 1, "->")
        url = data[i][0]
        method = data[i][1]
        header = local_data
        body = data[i][3]
        response = data[i][4]

        # checking the method
        if (method == "get"):
            get_method(url, header, body, response, i)
        elif (method == "post"):
            post_method(url, header, body, response, i)
        elif (method == "put"):
            put_method(url, header, body, response, i)
        else:
            delete_method(url, header, body, response, i)
Example #9
0
def _message_actions():
    """
    /slack/message_actions end point to receive HTTP POST Requests from Slack
    
    After Analysing the type of payload received whether a button click,
    dialogue,
    interactive message,
    dialogue submission repective response will be sent back to
    the user provoked the action
    """

    message_action = json.loads(request.form["payload"])

    user_id = message_action["user"]["id"]
    channel_id = message_action['channel']['id']

    if message_action["type"] == "interactive_message":

        # Add the message_ts to the user's order info
        for actions in message_action['actions']:
            if actions['name'] == "workform":

                # Show the ordering dialog to the user
                slack_client.api_call("chat.postMessage",
                                      channel=channel_id,
                                      icon_url=icon,
                                      text=f"Hey <@{user_id}> Let's Start",
                                      attachments=[])
                slack_client.api_call("chat.postMessage",
                                      channel=channel_id,
                                      text=chainmessages[0],
                                      icon_url=icon,
                                      attachments=[])

    elif message_action["type"] == "block_actions":
        action = message_action.get('actions')

        actiontype = action[0]['type']

        if actiontype == 'channels_select':
            channel = action[0]['selected_channel']

        if actiontype == 'button':
            text = action[0]['text']['text']
            value = action[0]['value']
            if text == "Delete":
                if value.startswith('resource'):
                    del_id = value[8:]
                    val = resourcemodule.delete(del_id)

                    slack_client.api_call("chat.postMessage",
                                          channel=channel_id,
                                          text="",
                                          icon_url=icon,
                                          attachments=[{
                                              "text":
                                              val,
                                              "color":
                                              "#3AA3E3",
                                              "attachment_type":
                                              "default",
                                          }])
                if value.startswith('timer'):
                    del_id = value[5:]
                    val = remind.deletereminder(del_id)
                    slack_client.api_call("chat.postMessage",
                                          channel=channel_id,
                                          text="",
                                          icon_url=icon,
                                          attachments=[{
                                              "text":
                                              val,
                                              "color":
                                              "#3AA3E3",
                                              "attachment_type":
                                              "default",
                                          }])

            if value == 'resourceadd':
                open_dialog = slack_client.api_call(
                    "dialog.open",
                    trigger_id=message_action["trigger_id"],
                    dialog={
                        "title":
                        "Add a resource",
                        "submit_label":
                        "Submit",
                        "callback_id":
                        'resourceadd',
                        "elements": [
                            {
                                "type": "text",
                                "label": "name",
                                "name": "resource"
                            },
                            {
                                "type": "text",
                                "label": "links/description",
                                "name": "link"
                            },
                        ]
                    })

            if text == 'Update':
                if value.startswith('resource'):
                    del_id = value[8:]
                    open_dialog = slack_client.api_call(
                        "dialog.open",
                        trigger_id=message_action["trigger_id"],
                        dialog={
                            "title":
                            "Daily Syncup",
                            "submit_label":
                            "Submit",
                            "callback_id":
                            'update' + del_id,
                            "elements": [
                                {
                                    "type": "text",
                                    "label": "name",
                                    "name": "resource"
                                },
                                {
                                    "type": "text",
                                    "label": "links/description",
                                    "name": "link"
                                },
                            ]
                        })

                    update_id = value[8:]

        if actiontype == 'static_select':
            option = action[0]['selected_option']['value']

            if option == 'dayoff':

                val = progress.dayoff(user_id)
                slack_client.api_call("chat.postMessage",
                                      channel=channel_id,
                                      icon_url=icon,
                                      text=val,
                                      attachments=[])
                blocks.divider(slack_client, channel_id)
            if option == "resources":

                resourcemodule.showresources(slack_client, channel_id)
                blocks.divider(slack_client, channel_id)
            if option == 'report':

                slack_client.api_call(
                    "chat.postMessage",
                    channel=channel_id,
                    icon_url=icon,
                    text=f":thumbsup: <@{user_id}> Please let us \
know your progress",
                    attachments=[])
                slack_client.api_call("chat.postMessage",
                                      channel=channel_id,
                                      icon_url=icon,
                                      text=chainmessages[0],
                                      attachments=[])

            if option == 'joke':

                url = "https://official-joke-api.appspot.com/random_joke"

                try:
                    data = requests.get(url=url)
                    data = data.content
                    data = json.loads(data)

                    slack_client.api_call(
                        "chat.postMessage",
                        channel=channel_id,
                        text=data['setup'] + " " + data['punchline'],
                        icon_url=icon,
                    )
                except:
                    slack_client.api_call(
                        "chat.postMessage",
                        channel=channel_id,
                        text="Failed to load",
                        icon_url=icon,
                    )
                blocks.divider(slack_client, channel_id)
            if option == 'show':

                progress.report(user_id, channel_id, slack_client)
                blocks.divider(slack_client, channel_id)
            return make_response("", 200)

    elif message_action["type"] == "dialog_submission":

        formdata = message_action['submission']

        callbackid = message_action['callback_id']

        if callbackid.startswith('updateresource'):
            update_id = callbackid[14:]
            resourcemodule.update(update_id, formdata['resource'],
                                  formdata['link'])
            val = 'Under Construction'
        elif callbackid.startswith('resourceadd'):
            formdata = message_action['submission']
            val = resourcemodule.insert(formdata['resource'], formdata['link'],
                                        user_id)

        slack_client.api_call("chat.postMessage",
                              channel=channel_id,
                              icon_url=icon,
                              text="",
                              attachments=[{
                                  "text": val,
                                  "color": "#3AA3E3",
                                  "attachment_type": "default",
                              }])

    return make_response("", 200)