Skip to content

Commit

Permalink
add n8n webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
madjid-asa committed Mar 11, 2024
1 parent 6f014b3 commit 750115b
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 19 deletions.
3 changes: 3 additions & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,9 @@
SLACK_WEBHOOK_C4_TENDER_CHANNEL = env.str("SLACK_WEBHOOK_C4_TENDER_CHANNEL", "set-it")
SLACK_WEBHOOK_C4_TECH_NOTIFICATIONS_CHANNEL = env.str("SLACK_WEBHOOK_C4_TECH_NOTIFICATIONS_CHANNEL", "set-it")

# api N8N
N8N_C4_TENDER_WEBHOOK = env.str("N8N_C4_TENDER_WEBHOOK", "set-it")

# API Marché APProch
MARCHE_APPROCH_TOKEN_RECETTE = env.str("MARCHE_APPROCH_TOKEN_RECETTE", "set-it")

Expand Down
56 changes: 56 additions & 0 deletions lemarche/utils/apis/api_n8n.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import logging

import requests
from django.conf import settings
from huey.contrib.djhuey import task


logger = logging.getLogger(__name__)


BASE_URL = "https://hooks.slack.com/services/"


def get_default_params():
return {}


def get_default_client(params={}):
params |= get_default_params()
headers = {
"user-agent": "betagouv-lemarche/0.0.1",
}
client = requests.Session()
client.params = params
client.headers = headers
return client


@task()
def send_data_to_webhook(
data: dict, webhook_url: str = settings.N8N_C4_TENDER_WEBHOOK, client: requests.Session = None
):
"""Huey task to send message to specific payload for specific slack service
Args:
payload (dict): payload for serivce
service_id (str): service id of the service (ex of service: Webhook)
client (requests.Session, optional): client to send requests. Defaults to None.
Raises:
e: requests.HTTPStatusError
"""
# if settings.SLACK_NOTIF_IS_ACTIVE:
if not client:
client = get_default_client()

try:
response = client.post(f"{webhook_url}", data=data)
response.raise_for_status()
print("N8N: send data to webhook")
logger.info("N8N: send data to webhook")
# logger.info(response.json()) // you'll receive a "HTTP 200" response with a plain text ok indicating that your message posted successfully # noqa
return True
except requests.exceptions.HTTPError as e:
logger.error("Error while fetching `%s`: %s", e.request.url, e)
raise e
42 changes: 23 additions & 19 deletions lemarche/www/tenders/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from lemarche.siaes.models import Siae
from lemarche.tenders.models import PartnerShareTender, Tender, TenderSiae
from lemarche.utils.apis import api_hubspot, api_mailjet, api_slack
from lemarche.utils.apis import api_hubspot, api_mailjet, api_n8n, api_slack
from lemarche.utils.data import date_to_string
from lemarche.utils.emails import send_mail_async, whitelist_recipient_list
from lemarche.utils.urls import get_domain_url, get_object_admin_url, get_object_share_url
Expand Down Expand Up @@ -479,32 +479,36 @@ def send_siae_interested_email_to_author(tender: Tender):
tender.save()


def get_data_to_notify_admin_tender(tender: Tender):
tender_admin_url = get_object_admin_url(tender)
data_to_send = {
"tender_id": tender.id,
"tender_title": tender.title,
"tender_kind": tender.get_kind_display(),
"tender_location": tender.location_display,
"tender_deadline_date": tender.deadline_date,
"tender_author_full_name": tender.contact_full_name,
"tender_author_email": tender.author.email,
"tender_author_company": tender.author.company_name,
"tender_scale_marche_useless": tender.get_scale_marche_useless_display(),
"tender_status": tender.get_status_display(),
"tender_source": tender.get_source_display(),
"tender_admin_url": tender_admin_url,
}
return data_to_send


def notify_admin_tender_created(tender: Tender):
email_subject = f"Marché de l'inclusion : dépôt de besoin, ajout d'un nouveau {tender.get_kind_display()}"
tender_admin_url = get_object_admin_url(tender)
email_body = render_to_string(
"tenders/create_notification_email_admin_body.txt",
{
"tender_id": tender.id,
"tender_title": tender.title,
"tender_kind": tender.get_kind_display(),
"tender_location": tender.location_display,
"tender_deadline_date": tender.deadline_date,
"tender_author_full_name": tender.contact_full_name,
"tender_author_company": tender.author.company_name,
"tender_scale_marche_useless": tender.get_scale_marche_useless_display(),
"tender_status": tender.get_status_display(),
"tender_source": tender.get_source_display(),
"tender_admin_url": tender_admin_url,
},
)
data_to_send = get_data_to_notify_admin_tender(tender=tender)
email_body = render_to_string("tenders/create_notification_email_admin_body.txt", data_to_send)
send_mail_async(
email_subject=email_subject,
email_body=email_body,
recipient_list=[settings.NOTIFY_EMAIL],
)

api_slack.send_message_to_channel(text=email_body, service_id=settings.SLACK_WEBHOOK_C4_TENDER_CHANNEL)
api_n8n.send_data_to_webhook(data=data_to_send)


def send_tenders_author_feedback_or_survey(tender: Tender, kind="feedback_30d"):
Expand Down

0 comments on commit 750115b

Please sign in to comment.