-
Notifications
You must be signed in to change notification settings - Fork 85
POC: Add email sender with API:Emailuser #1465
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
suecarmol
wants to merge
4
commits into
WikipediaLibrary:master
Choose a base branch
from
suecarmol:suecarmol/T383070-email-api-poc
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
127 changes: 127 additions & 0 deletions
127
TWLight/emails/management/commands/send_email_via_api.py
This file contains hidden or 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,127 @@ | ||
| import logging | ||
| import os | ||
| import requests | ||
|
|
||
| from django.core.management.base import BaseCommand, CommandError | ||
|
|
||
| logger = logging.getLogger("django") | ||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
| help = "Command that sends emails via API:Emailuser in MediaWiki" | ||
|
|
||
| def info(self, msg): | ||
| # Log and print so that messages are visible | ||
| # in docker logs (log) and cron job logs (print) | ||
| logger.info(msg) | ||
| self.stdout.write(msg) | ||
| self.stdout.flush() | ||
|
|
||
| def add_arguments(self, parser): | ||
| # Named (optional) arguments | ||
| # TODO: Check if we can add multiple targets, or if we have to make one request at a time | ||
| parser.add_argument( | ||
| "--target", | ||
| type=str, | ||
| help="The Wikipedia username you want to send the email to.", | ||
| ) | ||
| parser.add_argument( | ||
| "--subject", | ||
| type=str, | ||
| help="The subject of the email.", | ||
| ) | ||
| parser.add_argument( | ||
| "--body", | ||
| type=str, | ||
| help="The body of the email.", | ||
| ) | ||
|
|
||
| def handle(self, *args, **options): | ||
| if not options["target"]: | ||
| raise CommandError("You need to specify a user to send the email to") | ||
|
|
||
| if not options["subject"]: | ||
| raise CommandError("You need to specify the subject of the email") | ||
|
|
||
| if not options["body"]: | ||
| raise CommandError("You need to specify the body of the email") | ||
|
|
||
| target = options["target"] | ||
| subject = options["subject"] | ||
| body = options["body"] | ||
|
|
||
| email_bot_username = os.environ.get("EMAILWIKIBOTUSERNAME", None) | ||
| email_bot_password = os.environ.get("EMAILWIKIBOTPASSWORD", None) | ||
|
|
||
| if email_bot_username is None or email_bot_password is None: | ||
| # Bot credentials not provided; exiting gracefully | ||
| raise CommandError( | ||
| "The email bot username and/or password were not provided. Exiting..." | ||
| ) | ||
| # Code taken in part from https://www.mediawiki.org/wiki/API:Emailuser#Python | ||
| # TODO: Add solution in case of rate-limiting | ||
| session = requests.Session() | ||
| # TODO: See if we need to change this to Meta or the user's home wiki? | ||
| # Or is this wiki fine? | ||
| url = "https://test.wikipedia.org/w/api.php" | ||
|
|
||
| # Step 1: GET request to fetch login token | ||
| login_token_params = { | ||
| "action": "query", | ||
| "meta": "tokens", | ||
| "type": "login", | ||
| "format": "json", | ||
| } | ||
|
|
||
| self.info("Getting login token...") | ||
| response_login_token = session.get(url=url, params=login_token_params) | ||
| if response_login_token.status_code != 200: | ||
| raise CommandError( | ||
| "There was an error in the request for obtaining the login token." | ||
| ) | ||
| login_token_data = response_login_token.json() | ||
|
|
||
| login_token = login_token_data["query"]["tokens"]["logintoken"] | ||
|
|
||
| if not login_token: | ||
| raise CommandError("There was an error obtaining the login token.") | ||
|
|
||
| # Step 2: POST request to log in. Use of main account for login is not | ||
| # supported. Obtain credentials via Special:BotPasswords | ||
| # (https://www.mediawiki.org/wiki/Special:BotPasswords) for lgname & lgpassword | ||
| login_params = { | ||
| "action": "login", | ||
| "lgname": email_bot_username, | ||
| "lgpassword": email_bot_password, | ||
| "lgtoken": login_token, | ||
| "format": "json", | ||
| } | ||
|
|
||
| self.info("Signing in...") | ||
| login_response = session.post(url, data=login_params) | ||
| if login_response.status_code != 200: | ||
| raise CommandError("There was an error in the request for the login.") | ||
|
|
||
| # Step 3: GET request to fetch Email token | ||
| email_token_params = {"action": "query", "meta": "tokens", "format": "json"} | ||
|
|
||
| self.info("Getting email token...") | ||
| email_token_response = session.get(url=url, params=email_token_params) | ||
| email_token_data = email_token_response.json() | ||
|
|
||
| email_token = email_token_data["query"]["tokens"]["csrftoken"] | ||
|
|
||
| # Step 4: POST request to send an email | ||
| email_params = { | ||
| "action": "emailuser", | ||
| "target": target, | ||
| "subject": subject, | ||
| "text": body, | ||
| "token": email_token, | ||
| "format": "json", | ||
| } | ||
|
|
||
| self.info("Sending email...") | ||
| email_response = session.post(url, data=email_params) | ||
| if email_response.status_code != 200: | ||
| raise CommandError("There was an error when trying to send the email.") | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,7 +1,5 @@ | ||
| --- | ||
|
|
||
| version: '3.4' | ||
|
|
||
| services: | ||
| twlight: | ||
| build: | ||
|
|
||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,7 +1,5 @@ | ||
| --- | ||
|
|
||
| version: '3.4' | ||
|
|
||
| # Local environment should mount plaintext files as secrets | ||
| secrets: | ||
| DJANGO_DB_NAME: | ||
|
|
||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,7 +1,5 @@ | ||
| --- | ||
|
|
||
| version: '3.4' | ||
|
|
||
| secrets: | ||
| DJANGO_DB_NAME: | ||
| external: true | ||
|
|
||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,7 +1,5 @@ | ||
| --- | ||
|
|
||
| version: '3.4' | ||
|
|
||
| secrets: | ||
| DJANGO_DB_NAME: | ||
| external: true | ||
|
|
||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,7 +1,5 @@ | ||
| --- | ||
|
|
||
| version: '3.4' | ||
|
|
||
| volumes: | ||
| mysql: | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.