Merge pull request #7168 from betagouv/dolist
Ajoute le prestataire de mail dolist
This commit is contained in:
commit
b35aacb6e8
6 changed files with 132 additions and 2 deletions
|
@ -134,7 +134,7 @@ GEM
|
||||||
bindex (0.8.1)
|
bindex (0.8.1)
|
||||||
bootsnap (1.9.3)
|
bootsnap (1.9.3)
|
||||||
msgpack (~> 1.0)
|
msgpack (~> 1.0)
|
||||||
brakeman (5.1.1)
|
brakeman (5.2.2)
|
||||||
browser (5.3.1)
|
browser (5.3.1)
|
||||||
builder (3.2.4)
|
builder (3.2.4)
|
||||||
byebug (11.1.3)
|
byebug (11.1.3)
|
||||||
|
|
|
@ -62,7 +62,8 @@ module Manager
|
||||||
|
|
||||||
email_services = [
|
email_services = [
|
||||||
Mailjet::API.new,
|
Mailjet::API.new,
|
||||||
Sendinblue::API.new
|
Sendinblue::API.new,
|
||||||
|
Dolist::API.new
|
||||||
]
|
]
|
||||||
|
|
||||||
@sent_mails = email_services
|
@sent_mails = email_services
|
||||||
|
|
90
app/lib/dolist/api.rb
Normal file
90
app/lib/dolist/api.rb
Normal file
|
@ -0,0 +1,90 @@
|
||||||
|
class Dolist::API
|
||||||
|
CONTACT_URL = "https://apiv9.dolist.net/v1/contacts/read?AccountID=%{account_id}"
|
||||||
|
EMAIL_LOGS_URL = "https://apiv9.dolist.net/v1/statistics/email/sendings/transactional/search?AccountID=%{account_id}"
|
||||||
|
EMAIL_KEY = 7
|
||||||
|
DOLIST_WEB_DASHBOARD = "https://campaign.dolist.net/#/%{account_id}/contacts/%{contact_id}/sendings"
|
||||||
|
|
||||||
|
def properly_configured?
|
||||||
|
client_key.present?
|
||||||
|
end
|
||||||
|
|
||||||
|
def sent_mails(email_address)
|
||||||
|
contact_id = fetch_contact_id(email_address)
|
||||||
|
if contact_id.nil?
|
||||||
|
Rails.logger.info "Dolist::API: no contact found for email address '#{email_address}'"
|
||||||
|
return []
|
||||||
|
end
|
||||||
|
|
||||||
|
dolist_messages = fetch_dolist_messages(contact_id)
|
||||||
|
|
||||||
|
dolist_messages.map { |m| to_sent_mail(email_address, contact_id, m) }
|
||||||
|
rescue StandardError => e
|
||||||
|
Rails.logger.error e.message
|
||||||
|
[]
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def headers
|
||||||
|
{
|
||||||
|
"Content-Type": 'application/json',
|
||||||
|
"Accept": 'application/json',
|
||||||
|
"X-API-Key": client_key
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def client_key
|
||||||
|
Rails.application.secrets.dolist[:api_key]
|
||||||
|
end
|
||||||
|
|
||||||
|
def account_id
|
||||||
|
Rails.application.secrets.dolist[:account_id]
|
||||||
|
end
|
||||||
|
|
||||||
|
# https://api.dolist.com/documentation/index.html#/b3A6Mzg0MTQ0MDc-rechercher-un-contact
|
||||||
|
def fetch_contact_id(email_address)
|
||||||
|
url = format(CONTACT_URL, account_id: account_id)
|
||||||
|
|
||||||
|
body = {
|
||||||
|
Query: { FieldValueList: [{ ID: EMAIL_KEY, Value: email_address }] }
|
||||||
|
}.to_json
|
||||||
|
|
||||||
|
response = Typhoeus.post(url, body: body, headers: headers)
|
||||||
|
|
||||||
|
JSON.parse(response.response_body)["ID"]
|
||||||
|
end
|
||||||
|
|
||||||
|
# https://api.dolist.com/documentation/index.html#/b3A6Mzg0MTQ4MDk-recuperer-les-statistiques-des-envois-pour-un-contact
|
||||||
|
def fetch_dolist_messages(contact_id)
|
||||||
|
url = format(EMAIL_LOGS_URL, account_id: account_id)
|
||||||
|
|
||||||
|
body = { SearchQuery: { ContactID: contact_id } }.to_json
|
||||||
|
|
||||||
|
response = Typhoeus.post(url, body: body, headers: headers)
|
||||||
|
|
||||||
|
JSON.parse(response.response_body)['ItemList']
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_sent_mail(email_address, contact_id, dolist_message)
|
||||||
|
SentMail.new(
|
||||||
|
from: ENV['DOLIST_NO_REPLY_EMAIL'],
|
||||||
|
to: email_address,
|
||||||
|
subject: dolist_message['SendingName'],
|
||||||
|
delivered_at: Time.zone.parse(dolist_message['SendDate']),
|
||||||
|
status: status(dolist_message),
|
||||||
|
service_name: 'Dolist',
|
||||||
|
external_url: format(DOLIST_WEB_DASHBOARD, account_id: account_id, contact_id: contact_id)
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
def status(dolist_message)
|
||||||
|
case dolist_message.fetch_values('Status', 'IsDelivered')
|
||||||
|
in ['Sent', true]
|
||||||
|
"delivered"
|
||||||
|
in ['Sent', false]
|
||||||
|
"sent (delivered ?)"
|
||||||
|
in [status, _]
|
||||||
|
status
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -112,3 +112,10 @@ MATOMO_IFRAME_URL="https://matomo.example.org/index.php?module=CoreAdminHome&act
|
||||||
# ACTIVE_STORAGE_FILE_SIZE_THRESHOLD_BEFORE_CUSTOM_UPLOAD=4294967296
|
# ACTIVE_STORAGE_FILE_SIZE_THRESHOLD_BEFORE_CUSTOM_UPLOAD=4294967296
|
||||||
# a custom script handling upload of big file
|
# a custom script handling upload of big file
|
||||||
# ACTIVE_STORAGE_BIG_FILE_UPLOADER_WITH_ENCRYPTION_PATH='/usr/local/bin/swift'
|
# ACTIVE_STORAGE_BIG_FILE_UPLOADER_WITH_ENCRYPTION_PATH='/usr/local/bin/swift'
|
||||||
|
|
||||||
|
# SMTP Provider: Dolist
|
||||||
|
# DOLIST_USERNAME=""
|
||||||
|
# DOLIST_PASSWORD=""
|
||||||
|
# DOLIST_ACCOUNT_ID=""
|
||||||
|
# DOLIST_NO_REPLY_EMAIL=""
|
||||||
|
# DOLIST_API_KEY=""
|
||||||
|
|
27
config/initializers/dolist.rb
Normal file
27
config/initializers/dolist.rb
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
ActiveSupport.on_load(:action_mailer) do
|
||||||
|
module Dolist
|
||||||
|
class SMTP < ::Mail::SMTP
|
||||||
|
def deliver!(mail)
|
||||||
|
mail.from(ENV['DOLIST_NO_REPLY_EMAIL'])
|
||||||
|
mail.sender(ENV['DOLIST_NO_REPLY_EMAIL'])
|
||||||
|
mail['X-ACCOUNT-ID'] = Rails.application.secrets.dolist[:account_id]
|
||||||
|
|
||||||
|
mail['X-Dolist-Message-Name'] = mail.subject # for tracking in Dolist UI
|
||||||
|
mail['X-Dolist-Sending-Type'] = 'TransactionalService' # send even if the target is not active
|
||||||
|
|
||||||
|
super(mail)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
ActionMailer::Base.add_delivery_method :dolist, Dolist::SMTP
|
||||||
|
|
||||||
|
ActionMailer::Base.dolist_settings = {
|
||||||
|
user_name: Rails.application.secrets.dolist[:username],
|
||||||
|
password: Rails.application.secrets.dolist[:password],
|
||||||
|
address: 'smtp.dolist.net',
|
||||||
|
port: 587,
|
||||||
|
authentication: 'plain',
|
||||||
|
enable_starttls_auto: true
|
||||||
|
}
|
||||||
|
end
|
|
@ -35,6 +35,11 @@ defaults: &defaults
|
||||||
mailjet:
|
mailjet:
|
||||||
api_key: <%= ENV['MAILJET_API_KEY'] %>
|
api_key: <%= ENV['MAILJET_API_KEY'] %>
|
||||||
secret_key: <%= ENV['MAILJET_SECRET_KEY'] %>
|
secret_key: <%= ENV['MAILJET_SECRET_KEY'] %>
|
||||||
|
dolist:
|
||||||
|
username: <%= ENV['DOLIST_USERNAME'] %>
|
||||||
|
password: <%= ENV['DOLIST_PASSWORD'] %>
|
||||||
|
account_id: <%= ENV['DOLIST_ACCOUNT_ID'] %>
|
||||||
|
api_key: <%= ENV['DOLIST_API_KEY'] %>
|
||||||
api_entreprise:
|
api_entreprise:
|
||||||
key: <%= ENV['API_ENTREPRISE_KEY'] %>
|
key: <%= ENV['API_ENTREPRISE_KEY'] %>
|
||||||
pipedrive:
|
pipedrive:
|
||||||
|
|
Loading…
Add table
Reference in a new issue