Skip to content
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

Add Africa's talking adapter #70

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion healthtools/search/nurses.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def get_nurses_from_nc_registry(query):
nurses = {'hits': [], 'total': 0}
try:
response = requests.get(url)
if 'No results' in response.content:
if b'No results' in response.content:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the "b" for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It turns the text in front of it into a bytes literal.
After updating the app to python3, it throws an error if 'No results' in response.content: TypeError: a bytes-like object is required, not 'str'

return nurses

# make soup for parsing out of response and get the table
Expand Down
2 changes: 2 additions & 0 deletions healthtools/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
SMS_MTECH_PASS = os.getenv('HTOOLS_SMS_MTECH_PASS')
SMS_MTECH_SHORTCODE = os.getenv('HTOOLS_SMS_MTECH_SHORTCODE', '22495')

SMS_AFRICASTALKING_USER = os.getenv('HTOOLS_SMS_AFRICASTALKING_USER')
SMS_AFRICASTALKING_KEY = os.getenv('HTOOLS_SMS_AFRICASTALKING_KEY')

# TGBOT: TElegram Bot
TGBOT = {
Expand Down
18 changes: 15 additions & 3 deletions healthtools/sms/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import logging

from healthtools.sms import twilio, mtech
from healthtools.sms import twilio, mtech, africastalking
from healthtools.search import run_query

log = logging.getLogger(__name__)


def process_sms(args, adapter='mtech'):

def process_sms(request, adapter='mtech'):
args = request.args
if adapter == "africastalking":
args = {
'phoneNumber': request.values.get('from'),
'message': request.values.get('text')
}
msg = args.get('message')
phone_no = args.get('phoneNumber')

Expand Down Expand Up @@ -52,6 +57,13 @@ def create_sms(result, doc_type):
'5. Health Facility: HF KITALE'
return response

if (doc_type == 'nurses'):
response += 'We found ' + str(result['total']) + ' matches:'
for hit in result['hits'][:3]:
response += '\n' + "{}. {}".format(str(result_count), hit['name'])
result_count += 1
return response

# TODO: Figure out singular vs plural
response += 'Here are the top 3 matches your query returned'
for hit in result['hits'][:3]:
Expand Down
20 changes: 20 additions & 0 deletions healthtools/sms/africastalking.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import logging
from africastalking.AfricasTalkingGateway import (AfricasTalkingGateway, AfricasTalkingGatewayException)
from flask import current_app

log = logging.getLogger(__name__)

def send_sms(msg, phone_no):
username = current_app.config.get('SMS_AFRICASTALKING_USER')
apikey = current_app.config.get('SMS_AFRICASTALKING_KEY')
to = phone_no
message = msg

results = ''
gateway = AfricasTalkingGateway(username, apikey)

try:
results = gateway.sendMessage(to, message)
except AfricasTalkingGatewayException as e:
log.error('Encountered an error while sending: %s' ,str(e))
return results
2 changes: 1 addition & 1 deletion healthtools/views/sms.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@blueprint.route('/sms', methods=['GET', 'POST'])
@blueprint.route('/sms/<adapter>', methods=['GET', 'POST'])
def index(adapter='mtech'):
result = process_sms(request.args, adapter)
result = process_sms(request, adapter)

# Error with process_sms (process_sms returns false result)
if(not result):
Expand Down
6 changes: 6 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
AfricastalkingGateway==1.9
beautifulsoup4==4.6.0
bs4==0.0.1
certifi==2017.7.27.1
Expand All @@ -10,11 +11,16 @@ idna==2.6
itsdangerous==0.24
Jinja2==2.9.6
MarkupSafe==1.0
nested-lookup==0.1.3
prompt-toolkit==1.0.15
PyJWT==1.5.3
PySocks==1.6.7
pytz==2017.2
requests==2.18.4
requests-aws4auth==0.9
six==1.11.0
twilio==6.6.3
urllib3==1.22
wcwidth==0.1.7
Werkzeug==0.12.2
wit==4.3.0
2 changes: 1 addition & 1 deletion runtime.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
python-2.7.13
python-3.6.2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andela-ookoro will add this changes in his PR that upgrades to python3