Esempio n. 1
0
 def test_clear_all_log_handlers(self):
     app = App(
         client=self.web_client,
         signing_secret=self.signing_secret,
     )
     handler = SlackRequestHandler(app)
     handler.clear_all_log_handlers()
Esempio n. 2
0
    def test_events(self):
        app = App(
            client=self.web_client,
            signing_secret=self.signing_secret,
        )

        def event_handler():
            pass

        app.event("app_mention")(event_handler)

        input = {
            "token": "verification_token",
            "team_id": "T111",
            "enterprise_id": "E111",
            "api_app_id": "A111",
            "event": {
                "client_msg_id": "9cbd4c5b-7ddf-4ede-b479-ad21fca66d63",
                "type": "app_mention",
                "text": "<@W111> Hi there!",
                "user": "******",
                "ts": "1595926230.009600",
                "team": "T111",
                "channel": "C111",
                "event_ts": "1595926230.009600",
            },
            "type": "event_callback",
            "event_id": "Ev111",
            "event_time": 1595926230,
            "authed_users": ["W111"],
        }
        timestamp, body = str(int(time())), json.dumps(input)
        event = {
            "body": body,
            "queryStringParameters": {},
            "headers": self.build_headers(timestamp, body),
            "requestContext": {
                "http": {
                    "method": "POST"
                }
            },
            "isBase64Encoded": False,
        }
        response = SlackRequestHandler(app).handle(event, self.context)
        assert response["statusCode"] == 200
        assert self.mock_received_requests["/auth.test"] == 1

        event = {
            "body": body,
            "queryStringParameters": {},
            "headers": self.build_headers(timestamp, body),
            "requestContext": {
                "httpMethod": "POST"
            },
            "isBase64Encoded": False,
        }
        response = SlackRequestHandler(app).handle(event, self.context)
        assert response["statusCode"] == 200
        assert self.mock_received_requests["/auth.test"] == 1
Esempio n. 3
0
    def test_shortcuts(self):
        app = App(
            client=self.web_client,
            signing_secret=self.signing_secret,
        )

        def shortcut_handler(ack):
            ack()

        app.shortcut("test-shortcut")(shortcut_handler)

        input = {
            "type": "shortcut",
            "token": "verification_token",
            "action_ts": "111.111",
            "team": {
                "id": "T111",
                "domain": "workspace-domain",
                "enterprise_id": "E111",
                "enterprise_name": "Org Name",
            },
            "user": {
                "id": "W111",
                "username": "******",
                "team_id": "T111"
            },
            "callback_id": "test-shortcut",
            "trigger_id": "111.111.xxxxxx",
        }

        timestamp, body = str(int(
            time())), f"payload={quote(json.dumps(input))}"
        event = {
            "body": body,
            "queryStringParameters": {},
            "headers": self.build_headers(timestamp, body),
            "requestContext": {
                "http": {
                    "method": "POST"
                }
            },
            "isBase64Encoded": False,
        }
        response = SlackRequestHandler(app).handle(event, self.context)
        assert response["statusCode"] == 200
        assert self.mock_received_requests["/auth.test"] == 1

        event = {
            "body": body,
            "queryStringParameters": {},
            "headers": self.build_headers(timestamp, body),
            "requestContext": {
                "httpMethod": "POST"
            },
            "isBase64Encoded": False,
        }
        response = SlackRequestHandler(app).handle(event, self.context)
        assert response["statusCode"] == 200
        assert self.mock_received_requests["/auth.test"] == 1
Esempio n. 4
0
    def test_commands(self):
        app = App(
            client=self.web_client,
            signing_secret=self.signing_secret,
        )

        def command_handler(ack):
            ack()

        app.command("/hello-world")(command_handler)

        input = (
            "token=verification_token"
            "&team_id=T111"
            "&team_domain=test-domain"
            "&channel_id=C111"
            "&channel_name=random"
            "&user_id=W111"
            "&user_name=primary-owner"
            "&command=%2Fhello-world"
            "&text=Hi"
            "&enterprise_id=E111"
            "&enterprise_name=Org+Name"
            "&response_url=https%3A%2F%2Fhooks.slack.com%2Fcommands%2FT111%2F111%2Fxxxxx"
            "&trigger_id=111.111.xxx")
        timestamp, body = str(int(time())), input
        event = {
            "body": body,
            "queryStringParameters": {},
            "headers": self.build_headers(timestamp, body),
            "requestContext": {
                "http": {
                    "method": "POST"
                }
            },
            "isBase64Encoded": False,
        }
        response = SlackRequestHandler(app).handle(event, self.context)
        assert response["statusCode"] == 200
        assert self.mock_received_requests["/auth.test"] == 1

        event = {
            "body": body,
            "queryStringParameters": {},
            "headers": self.build_headers(timestamp, body),
            "requestContext": {
                "httpMethod": "POST"
            },
            "isBase64Encoded": False,
        }
        response = SlackRequestHandler(app).handle(event, self.context)
        assert response["statusCode"] == 200
        assert self.mock_received_requests["/auth.test"] == 1
def lambda_handler(event, context):
    try:
        payload = base64.b64decode(str(event["body"]))
        payload_2 = parse_qs(payload.decode("utf-8"))
        log.info(payload_2)
        log.info('sending payload to the interactive response')
        slack_handler = SlackRequestHandler(app=app)
        log.info('response sent')
        return slack_handler.handle(event, context)
    except Exception as e:
        log.info(" something goes wrong: " + str(e))
        return {'statusCode': 401}
Esempio n. 6
0
    def test_lazy_listeners(self):
        app = App(
            client=self.web_client,
            signing_secret=self.signing_secret,
        )

        def command_handler(ack):
            ack()

        def say_it(say):
            say("Done!")

        app.command("/hello-world")(ack=command_handler, lazy=[say_it])

        input = (
            "token=verification_token"
            "&team_id=T111"
            "&team_domain=test-domain"
            "&channel_id=C111"
            "&channel_name=random"
            "&user_id=W111"
            "&user_name=primary-owner"
            "&command=%2Fhello-world"
            "&text=Hi"
            "&enterprise_id=E111"
            "&enterprise_name=Org+Name"
            "&response_url=https%3A%2F%2Fhooks.slack.com%2Fcommands%2FT111%2F111%2Fxxxxx"
            "&trigger_id=111.111.xxx")
        timestamp, body = str(int(time())), input
        headers = self.build_headers(timestamp, body)
        headers["x-slack-bolt-lazy-only"] = "1"
        headers["x-slack-bolt-lazy-function-name"] = "say_it"
        event = {
            "body": body,
            "queryStringParameters": {},
            "headers": headers,
            "requestContext": {
                "http": {
                    "method": "NONE"
                }
            },
            "isBase64Encoded": False,
        }
        response = SlackRequestHandler(app).handle(event, self.context)
        assert response["statusCode"] == 200
        assert self.mock_received_requests["/auth.test"] == 1
        assert self.mock_received_requests["/chat.postMessage"] == 1
Esempio n. 7
0
    def test_oauth(self):
        app = App(
            client=self.web_client,
            signing_secret=self.signing_secret,
            oauth_settings=OAuthSettings(
                client_id="111.111",
                client_secret="xxx",
                scopes=["chat:write", "commands"],
            ),
        )

        event = {
            "body": "",
            "queryStringParameters": {},
            "headers": {},
            "requestContext": {
                "http": {
                    "method": "GET"
                }
            },
            "isBase64Encoded": False,
        }
        response = SlackRequestHandler(app).handle(event, self.context)
        assert response["statusCode"] == 200
        assert response["headers"][
            "content-type"] == "text/html; charset=utf-8"
        assert response["headers"]["content-length"] == "565"
        assert response.get("body") is not None

        event = {
            "body": "",
            "queryStringParameters": {},
            "headers": {},
            "requestContext": {
                "httpMethod": "GET"
            },
            "isBase64Encoded": False,
        }
        response = SlackRequestHandler(app).handle(event, self.context)
        assert response["statusCode"] == 200
        assert response["headers"][
            "content-type"] == "text/html; charset=utf-8"
        assert response["headers"]["content-length"] == "565"
        assert "https://slack.com/oauth/v2/authorize?state=" in response.get(
            "body")
Esempio n. 8
0
def handler(event, context):
    slack_handler = SlackRequestHandler(app=app)
    return slack_handler.handle(event, context)
Esempio n. 9
0
# process_before_response must be True when running on FaaS
app = App(process_before_response=True)


@app.event("app_mention")
def handle_app_mentions(payload, say, logger):
    logger.info(payload)
    say("What's up?")


@app.command("/hello-bolt-python-lambda")
def respond_to_slack_within_3_seconds(ack):
    ack("Thanks!")


SlackRequestHandler.clear_all_log_handlers()
logging.basicConfig(format="%(asctime)s %(message)s", level=logging.DEBUG)


def handler(event, context):
    slack_handler = SlackRequestHandler(app=app)
    return slack_handler.handle(event, context)


# export SLACK_SIGNING_SECRET=***
# export SLACK_BOT_TOKEN=xoxb-***

# rm -rf vendor && cp -pr ../../src/* vendor/
# pip install python-lambda
# lambda deploy --config-file aws_lambda_config.yaml --requirements requirements.txt
Esempio n. 10
0
    logger.info(f"{output=}")
    unfurl_dict = dict(
        map(lambda x: (x["Payload"]["url"], x["Payload"]["attachement"]),
            output))
    logger.info(f"sending unfurl. {unfurl_dict}")
    client.chat_unfurl(
        channel=event["channel"],
        ts=event["message_ts"],
        unfurls=json.dumps(unfurl_dict),
    )
    logger.info(f"unfurl done. {unfurl_dict}")


slack_app.event("link_shared")(ack=fast_ack, lazy=[unfurl_kibela])

SlackRequestHandler.clear_all_log_handlers()
# logging.basicConfig(format="%(asctime)s %(message)s", level=logging.DEBUG)
logging.basicConfig(format="%(asctime)s %(message)s", level=logging.INFO)

slack_handler = SlackRequestHandler(app=slack_app)

bottle_app = Bottle()


@bottle_app.route("/")
def index():
    return {"OK": True}


@bottle_app.route("/kibela/webhook", method="POST")
def kibela_webhook():