This in an advanced example of Syncano Sockets, showing an integration with Mailgun (https://mailgun.com/).
We will use Mailgun's sandbox environment, which allows sending up to 300 emails per day - it should be more than enough for testing.
We will implement two endpoints inside the Sockets - one for sending emails, and a second one to obtain some basic statistics.
The whole example can be found under: Syncano/custom-socket-advanced-example.
It can be installed to your Syncano Instance using the install from url functionality in the CLI. The url is:
https://github.com/Syncano/custom-socket-advanced-example/blob/master/socket.yml
- Syncano Account - Create one here.
- GIT - If you want to edit files locally, clone our repository using:
git clone git@github.com:Syncano/custom-socket-advanced-example.git-
Syncano CLI tool in version 0.5 or higher.
Note: It is nice to use virtualenv to separate your tools:
sudo pip install virtualenvThen create virtual env:virtualenv cliand active it:source cli/bin/activateInstall Syncano CLI:pip install syncano_cli>=0.5 -
Your favorite text editor.
name: mailgun_integration
description: An advanced example of Sockets - mailgun integration.
author:
name: Info at Syncano
email: info@syncano.com
endpoints:
send_mail:
POST:
script: send_mail
get_stats:
GET:
script: get_stats
dependencies:
scripts:
send_mail:
runtime_name: python_library_v5.0
file: scripts/send_mail.py
get_stats:
runtime_name: python_library_v5.0
file: scripts/get_stats.py
Above YAML file defines one Sockets with two endpoints:
send_mailfor sending emails, it's run on POST HTTP method call; we want to pass some basic information about who it should be sent to, what subject should be used and what text should be in the email itself;get_stats- second endpoint is for obtaining basic stats from Mailgun service.
There are also two script dependencies defined.
import requests
import json
api_key = CONFIG.get('mailgun_api_key')
to_email = ARGS.get('to_email')
subject = ARGS.get('subject')
email_body = ARGS.get('email_body')
response = requests.post(
"https://api.mailgun.net/v3/sandboxa8ccfb01296d4b19bace47fb8102d130.mailgun.org/messages",
auth=("api", api_key),
data={
"from": "Mailgun Sandbox <postmaster@sandboxa8ccfb01296d4b19bace47fb8102d130.mailgun.org>",
"to": to_email,
"subject": subject,
"text": email_body
}
)
if response.status_code == 200:
success_content = json.dumps(
{
'status_code': 200,
'info': u'Mail successfully sent to {}'.format(to_email)
}
)
set_response(HttpResponse(status_code=200, content=success_content, content_type='application/json'))
else:
fail_content = json.dumps(
{
'status_code': response.status_code,
'info': response.text
}
)
set_response(HttpResponse(status_code=400, content=fail_content, content_type='application/json'))
Script above will send a request to Mailgun service - and this service will send an email to a user.
It's worth noting the CONFIG variable - it's an Instance global config dictionary/map - you can define its content inside
Syncano Dasboard under Global Config menu on the left, or using Syncano Libraries - more about it can be found
in our docs or using a CLI:
syncano config add mailgun_api_key 99xx11
import requests
api_key = CONFIG.get('mailgun_api_key')
response = requests.get(
"https://api.mailgun.net/v3/sandboxa8ccfb01296d4b19bace47fb8102d130.mailgun.org/stats/total",
auth=("api", api_key),
params={
"event": ["accepted", "delivered", "failed"],
"duration": "1m"}
)
if response.status_code == 200:
set_response(HttpResponse(status_code=200, content=response.text, content_type='application/json'))
else:
set_response(HttpResponse(status_code=400, content=response.text, content_type='application/json'))
Above script will work as a proxy to the Mailgun service - will send a request about stats and pass the result as a response.
The directory structure in my favourite editor looks like this:
or in tree format:
.
├── scripts
│ ├── get_stats.py
│ └── send_mail.py
└── socket.yml
-
Assuming that you have Syncano CLI installed, log in using:
syncano login --instance-name your-instance-nameIn my case it is:syncano login --instance-name patient-resonance-4283Next you will see a prompt for
usernameandpassword; provide both and confirm withenter. -
There are two ways of installing a Sockets - one is using your local files and the second one is by using a URL.
To install the Sockets from a URL do:
syncano sockets install https://raw.githubusercontent.com/Syncano/custom-socket-advanced-example/master/socket.yml --name mailgun_integrationIn such scenario - you do not even need to clone the repository to your local machine. The
--nameparameter and name here are needed - because under the hood, empty Sockets is created - and code fetching from repository is done asynchronously in the second step.To install Sockets from local files do:
syncano sockets install <path_to_files>In my case it is:
syncano sockets install ../syncano_scripts/repos/custom-socket-advanced-example/ -
Try a newly created Sockets:
To list Sockets, do:
syncano sockets listIn the output you should find:
- socket: info: '' name: mailgun_integration status: okThis means that Sockets
mailgun_integrationwas created successfuly - the status isok. In any other case you will see anerrorand detailed information ininfoabout what went wrong.Now, list all defined endpoints:
syncano sockets list endpointsIn the output you should find:
- endpoint: methods: - GET name: mailgun_integration/get_stats path: /v1.1/instances/patient-resonance-4283/endpoints/sockets/mailgun_integration/get_stats/ - endpoint: methods: - POST name: mailgun_integration/send_mail path: /v1.1/instances/patient-resonance-4283/endpoints/sockets/mailgun_integration/send_mail/ -
Before you run an endpoint - be sure that you have Mailgun
api_keyinside your Instance Global Config. See thesend_mail.pydescription for more details. My config looks like this:{"mailgun_api_key": "key-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}Of course you need to replace
key-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxwith your Mailgun API Key.You can use CLI to add this config variable to your Instance:
syncano config add mailgun_api_key key-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx -
Run the endpoint defined in your Sockets:
First, run the stats endpoint - it's easier, as it is a simple GET request without any arguments required.
syncano sockets run mailgun_integration/get_statsIn the output you should see something like this (probably not formatted):
{ "stats": [ { "delivered": { "smtp": 5, "total": 5, "http": 0 }, "accepted": { "outgoing": 5, "total": 5, "incoming": 0 }, "time": "Mon, 01 Aug 2016 00:00:00 UTC", "failed": { "permanent": { "suppress-complaint": 0, "suppress-bounce": 0, "total": 0, "bounce": 0, "suppress-unsubscribe": 0 }, "temporary": { "espblock": 0 } } } ], "end": "Mon, 01 Aug 2016 00:00:00 UTC", "resolution": "month", "start": "Mon, 01 Aug 2016 00:00:00 UTC" }Above response is one-to-one to the response provided by Mailgun.
Now let's send an e-mail!
Run:
syncano sockets run mailgun_integration/send_mail POST -d subject="CustomSocket MailGun test" -d to_email="FirstName LastName <your_email>" -d email_body="So nice to create Sockets!"Do not forget to change e-mail address in JSON data.
It should return:
{ u'info': u'Mail successfully sent to {to_email_value}', u'status_code': 200 }You can now call stats again and see if anything changed.
-
To delete mailgun Sockets do:
syncano sockets delete mailgun_integration
Hope this was helpful! If you have any question or issues, do no hesitate to contact me directly: sebastian.opalczynski@syncano.com I am also available on the Syncano Slack community. See you there!
