예제 #1
0
 def __call__(
     self,
     text: Union[str, dict] = "",
     blocks: Optional[List[Union[dict, Block]]] = None,
     attachments: Optional[List[Union[dict, Attachment]]] = None,
     response_type: Optional[str] = None,
     replace_original: Optional[bool] = None,
     delete_original: Optional[bool] = None,
 ) -> WebhookResponse:
     if self.response_url is not None:
         client = WebhookClient(self.response_url)
         text_or_whole_response: Union[str, dict] = text
         if isinstance(text_or_whole_response, str):
             text = text_or_whole_response
             message = _build_message(
                 text=text,
                 blocks=blocks,
                 attachments=attachments,
                 response_type=response_type,
                 replace_original=replace_original,
                 delete_original=delete_original,
             )
             return client.send_dict(message)
         elif isinstance(text_or_whole_response, dict):
             message = _build_message(**text_or_whole_response)
             return client.send_dict(message)
         else:
             raise ValueError(
                 f"The arg is unexpected type ({type(text_or_whole_response)})"
             )
     else:
         raise ValueError(
             "respond is unsupported here as there is no response_url")
예제 #2
0
 def test_user_agent_customization_issue_769(self):
     client = WebhookClient(
         url="http://localhost:8888/user-agent-this_is-test",
         user_agent_prefix="this_is",
         user_agent_suffix="test",
     )
     resp = client.send_dict({"text": "hi!"})
     self.assertEqual(resp.body, "ok")
예제 #3
0
 def test_if_it_uses_custom_logger_issue_921(self):
     logger = CustomLogger("test-logger")
     client = WebhookClient(url="http://localhost:8888", logger=logger)
     client.send_dict({"text": "hi!"})
     self.assertTrue(logger.called)
예제 #4
0
 def test_proxy_issue_714(self):
     client = WebhookClient(url="http://localhost:8888",
                            proxy="http://invalid-host:9999")
     with self.assertRaises(urllib.error.URLError):
         client.send_dict({"text": "hello!"})
예제 #5
0
 def test_error_response(self):
     client = WebhookClient(url="http://localhost:8888/error")
     resp: WebhookResponse = client.send_dict({"text": "hello!"})
     self.assertEqual(500, resp.status_code)
     self.assertTrue(resp.body.startswith("<!DOCTYPE html>"))
예제 #6
0
 def test_timeout_issue_712(self):
     client = WebhookClient(url="http://localhost:8888/timeout", timeout=1)
     with self.assertRaises(socket.timeout):
         client.send_dict({"text": "hello!"})
예제 #7
0
 def test_send_dict(self):
     client = WebhookClient("http://localhost:8888")
     resp: WebhookResponse = client.send_dict({"text": "hello!"})
     self.assertEqual(200, resp.status_code)
     self.assertEqual("ok", resp.body)