Airflow-MS-Teams-Operator icon indicating copy to clipboard operation
Airflow-MS-Teams-Operator copied to clipboard

Method not allowed

Open lauge-kongstad opened this issue 4 years ago • 6 comments

Hi I'm trying to follow your example but without success. The logs says it is trying to POST to webhook URL, but then right after it says that GET is not allowed. Any advice?

Task definition

    notify_teams_success = MSTeamsWebhookOperator(
        task_id='ms_team_notification',
        http_conn_id='msteams_webhook_url',
        message = f"Airflow: Dag {dag_name} finished succesfully.",
        subtitle = "What a **great** achievement.",
        theme_color = "00FF00",
        button_text = "Link to Airflow UI",
        button_url = "http://localhost:8080/admin/airflow/tree",
        #proxy = "https://yourproxy.domain:3128/",
        dag=dag
    )

Log

[2021-07-06 13:59:37,184] {base.py:78} INFO - Using connection to: id: msteams_webhook_url. Host: maersk.webhook.office.com/webhookb2/xxx, Port: None, Schema: http, Login: , Password: None, extra: {}
[2021-07-06 13:59:37,252] {base.py:78} INFO - Using connection to: id: msteams_webhook_url. Host: maersk.webhook.office.com/webhookb2/xxx, Port: None, Schema: http, Login: , Password: None, extra: {}
[2021-07-06 13:59:37,254] {logging_mixin.py:104} INFO - {}
[2021-07-06 13:59:37,258] {logging_mixin.py:104} INFO - Proxy is :
[2021-07-06 13:59:37,326] {base.py:78} INFO - Using connection to: id: msteams_webhook_url. Host: maersk.webhook.office.com/webhookb2/xxx, Port: None, Schema: http, Login: , Password: None, extra: {}
[2021-07-06 13:59:37,378] {http.py:140} INFO - Sending 'POST' to url: http://maersk.webhook.office.com/webhookb2/xxx
[2021-07-06 13:59:41,910] {http.py:154} ERROR - HTTP error: Method Not Allowed
[2021-07-06 13:59:41,920] {http.py:155} ERROR - Invalid webhook request - GET not supported
[2021-07-06 13:59:42,004] {taskinstance.py:1481} ERROR - Task failed with exception
Traceback (most recent call last):
  File "/home/airflow/.local/lib/python3.6/site-packages/airflow/providers/http/hooks/http.py", line 152, in check_response
    response.raise_for_status()
  File "/home/airflow/.local/lib/python3.6/site-packages/requests/models.py", line 943, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 405 Client Error: Method Not Allowed for url: https://maersk.webhook.office.com/webhookb2/xxx

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/airflow/.local/lib/python3.6/site-packages/airflow/models/taskinstance.py", line 1137, in _run_raw_task
    self._prepare_and_execute_task_with_callbacks(context, task)
  File "/home/airflow/.local/lib/python3.6/site-packages/airflow/models/taskinstance.py", line 1311, in _prepare_and_execute_task_with_callbacks
    result = self._execute_task(context, task_copy)
  File "/home/airflow/.local/lib/python3.6/site-packages/airflow/models/taskinstance.py", line 1341, in _execute_task
    result = task_copy.execute(context=context)
  File "/opt/airflow/plugins/operators/ms_teams_webhook_operator.py", line 90, in execute
    self.hook.execute()
  File "/opt/airflow/plugins/hooks/ms_teams_webhook_hook.py", line 136, in execute
    extra_options={'proxies': proxies})
  File "/home/airflow/.local/lib/python3.6/site-packages/airflow/providers/http/hooks/http.py", line 141, in run
    return self.run_and_check(session, prepped_request, extra_options)
  File "/home/airflow/.local/lib/python3.6/site-packages/airflow/providers/http/hooks/http.py", line 191, in run_and_check
    self.check_response(response)
  File "/home/airflow/.local/lib/python3.6/site-packages/airflow/providers/http/hooks/http.py", line 156, in check_response
    raise AirflowException(str(response.status_code) + ":" + response.reason)
airflow.exceptions.AirflowException: 405:Method Not Allowed
[2021-07-06 13:59:42,069] {taskinstance.py:1531} INFO - Marking task as FAILED. dag_id=ms_teams_notification, task_id=ms_team_notification, execution_date=20151012T000000, start_date=20210706T135936, end_date=20210706T135942
[2021-07-06 13:59:42,283] {local_task_job.py:151} INFO - Task exited with return code 1

lauge-kongstad avatar Jul 06 '21 14:07 lauge-kongstad

I can post to my teams channel using the following code successfully using pymsteams pip package which uses requests library under the hood

import pymsteams
myTeamsMessage = pymsteams.connectorcard("https://maersk.webhook.office.com/webhookb2/xxx")
myTeamsMessage.text("Hello world")
myTeamsMessage.send()

lauge-kongstad avatar Jul 06 '21 14:07 lauge-kongstad

This is the strange bit.

[2021-07-06 13:59:37,378] {http.py:140} INFO - Sending 'POST' to url: http://maersk.webhook.office.com/webhookb2/xxx [2021-07-06 13:59:41,910] {http.py:154} ERROR - HTTP error: Method Not Allowed [2021-07-06 13:59:41,920] {http.py:155} ERROR - Invalid webhook request - GET not supported

It's sending a POST but being rejected for sending a GET?

Is there something stripping the body out?

I noticed the message property is:

message = f"Airflow: Dag {dag_name} finished succesfully.",

Can you try removing the { } bit and test it normally first?

message = "Hello World",

mendhak avatar Jul 08 '21 20:07 mendhak

Thanks for the reply, I'll give it a go and get back.

lauge-kongstad avatar Jul 09 '21 05:07 lauge-kongstad

I have the same problem. I tried to remove { } but still not works.

skynuccioc avatar Jan 12 '22 08:01 skynuccioc

I am wondering if @lauge-kongstad maybe they found a solution?

Just making another guess. Could the problem be the http:// is the webhook somehow redirecting to its https:// version and that's causing an error.

mendhak avatar Jan 13 '22 22:01 mendhak

Sorry for not getting back to you -- I went away from airflow, so haven't tried anything.

lauge-kongstad avatar Jan 14 '22 08:01 lauge-kongstad