-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added output adapter for mailgun api.
- Loading branch information
1 parent
e076eca
commit 4e9ef44
Showing
4 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from chatterbot.adapters.output import OutputAdapter | ||
import requests | ||
|
||
|
||
class Mailgun(OutputAdapter): | ||
|
||
def __init__(self, **kwargs): | ||
super(Mailgun, self).__init__(**kwargs) | ||
|
||
# Use the bot's name for the name of the sender | ||
self.name = kwargs.get("name") | ||
self.from_address = kwargs.get("mailgun_from_address") | ||
self.api_key = kwargs.get("mailgun_api_key") | ||
self.endpoint = kwargs.get("mailgun_api_endpoint") | ||
self.recipients = kwargs.get("mailgun_recipients") | ||
|
||
def send_message(self, subject, text, from_address, recipients): | ||
""" | ||
* subject: Subject of the email. | ||
* text: Text body of the email. | ||
* from_email: The email address that the message will be sent from. | ||
* recipients: A list of recipient email addresses. | ||
""" | ||
return requests.post( | ||
self.endpoint, | ||
auth=("api", self.api_key), | ||
data={ | ||
"from": "%s <%s>" % (self.name, from_address), | ||
"to": recipients, | ||
"subject": subject, | ||
"text": text | ||
}) | ||
|
||
def process_response(self, statement): | ||
""" | ||
Send the response statement as an email. | ||
""" | ||
subject = "Message from %s" % (self.name) | ||
|
||
self.send_message( | ||
subject, | ||
statement.text, | ||
self.from_address, | ||
self.recipients | ||
) | ||
|
||
return statement |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from chatterbot import ChatBot | ||
from settings import MAILGUN | ||
|
||
''' | ||
To use this example, create a new file called settings.py. | ||
In settings.py define the following: | ||
MAILGUN = { | ||
"CONSUMER_KEY": "my-mailgun-api-key", | ||
"API_ENDPOINT": "https://api.mailgun.net/v3/my-domain.com/messages" | ||
} | ||
''' | ||
|
||
# Change these to match your own email configuration | ||
FROM_EMAIL = "mailgun@salvius.org" | ||
RECIPIENTS = ["gunthercx@gmail.com"] | ||
|
||
bot = ChatBot( | ||
"Mailgun Example Bot", | ||
mailgun_from_address=FROM_EMAIL, | ||
mailgun_api_key=MAILGUN["CONSUMER_KEY"], | ||
mailgun_api_endpoint=MAILGUN["API_ENDPOINT"], | ||
mailgun_recipients=RECIPIENTS, | ||
output_adapter="chatterbot.adapters.io.Mailgun", | ||
storage_adapter="chatterbot.adapters.storage.JsonDatabaseAdapter", | ||
database="../database.db" | ||
) | ||
|
||
# Send an example email to the address provided | ||
response = bot.get_response("How are you?") | ||
print("Check your inbox at ", RECIPIENTS) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from unittest import TestCase | ||
from chatterbot.adapters.output import Mailgun | ||
|
||
|
||
class MailgunAdapterTestCase(TestCase): | ||
|
||
def setUp(self): | ||
super(MailgunAdapterTestCase, self).setUp() | ||
self.adapter = Mailgun() |