diff --git a/.gitignore b/.gitignore index 19e815f..22b6e42 100644 --- a/.gitignore +++ b/.gitignore @@ -88,5 +88,8 @@ ENV/ # Rope project settings .ropeproject -# nexmo key file -private.key +# nexmo key files +*.key + +.DS_Store +settings.json diff --git a/README.md b/README.md index 9011ad6..3aa3ef7 100644 --- a/README.md +++ b/README.md @@ -2,14 +2,14 @@ Quickstarts also available for: [Java](https://github.com/nexmo-community/nexmo-java-quickstart), [.NET](https://github.com/nexmo-community/nexmo-dotnet-quickstart), [Node.js](https://github.com/nexmo-community/nexmo-node-quickstart), [PHP](https://github.com/nexmo-community/nexmo-php-quickstart), [Ruby](https://github.com/nexmo-community/nexmo-ruby-quickstart) -The purpose of the quickstart guide is to provide simple examples focused -on one goal. For example, sending and SMS, handling and incoming SMS webhook, -making a Text to Speech call. +The purpose of the Quickstart guide is to provide simple examples focused +on one goal. For example, sending an SMS, handling an incoming SMS webhook, +or making a Text to Speech call. ## Setup -To use this sample you will first need a [Nexmo account][sign-up]. Then rename -the `.env-example` file to `.env` and set the values as required. +To use the examples you will first need a [Nexmo account][sign-up]. Then rename +the `example.env` file to `.env` and set the values as required. For some of the examples you will need to [buy a number][buy-number]. diff --git a/voice/connect-an-inbound-call.py b/voice/connect-an-inbound-call.py new file mode 100755 index 0000000..39c7e16 --- /dev/null +++ b/voice/connect-an-inbound-call.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python3 +from flask import Flask, request, jsonify + +app = Flask(__name__) + +@app.route("/webhooks/answer") +def answer_call(): + ncco = [ + { + "action": "connect", + "from": "NEXMO_NUMBER", + "endpoint": [{ + "type": 'phone', + "number": "RECIPIENT_NUMBER" + }] + } + ] + return jsonify(ncco) + +if __name__ == '__main__': + app.run(port=3000) diff --git a/voice/connect-callers-to-a-conference.py b/voice/connect-callers-to-a-conference.py new file mode 100644 index 0000000..abe517c --- /dev/null +++ b/voice/connect-callers-to-a-conference.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 +from flask import Flask, request, jsonify + +app = Flask(__name__) + + +@app.route("/webhooks/answer") +def answer_call(): + ncco = [ + { + "action": "talk", + "text": "Please wait while we connect you to the conference" + }, + { + "action": "conversation", + "name": CONF_NAME + }] + return jsonify(ncco) + + +if __name__ == '__main__': + app.run(port=3000) diff --git a/voice/earmuff-a-call.py b/voice/earmuff-a-call.py new file mode 100644 index 0000000..e174171 --- /dev/null +++ b/voice/earmuff-a-call.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python3 +import nexmo +import time +from pprint import pprint + +client = nexmo.Client( + application_id=APPLICATION_ID, + private_key=APPLICATION_PRIVATE_KEY_PATH, +) + +response = client.update_call(UUID, action="earmuff") +pprint(response) +time.sleep(5) +response = client.update_call(UUID, action="unearmuff") +pprint(response) diff --git a/voice/join-outbound-calls.py b/voice/join-outbound-calls.py new file mode 100755 index 0000000..d02bcb1 --- /dev/null +++ b/voice/join-outbound-calls.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 +from flask import Flask, request, jsonify + +app = Flask(__name__) + +@app.route("/webhooks/answer") +def answer_call(): + ncco = [ + { + "action": "connect", + "from": "NEXMO_NUMBER", + "endpoint": [{ + "type": 'phone', + "number": "RECIPIENT_NUMBER_1" + }] + }, + { + "action": "connect", + "from": "NEXMO_NUMBER", + "endpoint": [{ + "type": 'phone', + "number": "RECIPIENT_NUMBER_2" + }] + } + ] + return jsonify(ncco) + +if __name__ == '__main__': + app.run(port=3000) diff --git a/voice/mute-a-call.py b/voice/mute-a-call.py new file mode 100644 index 0000000..bfc96b7 --- /dev/null +++ b/voice/mute-a-call.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python3 +import nexmo +import time +from pprint import pprint + +client = nexmo.Client( + application_id=APPLICATION_ID, + private_key=APPLICATION_PRIVATE_KEY_PATH, +) + +response = client.update_call(UUID, action="mute") +pprint(response) +time.sleep(5) +response = client.update_call(UUID, action="unmute") +pprint(response) diff --git a/voice/record-a-call-with-split-audio.py b/voice/record-a-call-with-split-audio.py new file mode 100755 index 0000000..5894383 --- /dev/null +++ b/voice/record-a-call-with-split-audio.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 +from flask import Flask, request, jsonify +from pprint import pprint + +app = Flask(__name__) + +@app.route("/webhooks/answer") +def answer_call(): + ncco = [ + { + "action": "talk", + "text": "Hi, we will shortly forward your call. This call is recorded for quality assurance purposes." + }, + { + "action": "record", + "split" : "conversation", + "eventUrl": ["https://demo.ngrok.io/webhooks/recordings"] + }, + { + "action": "connect", + "eventUrl": ["https://demo.ngrok.io/webhooks/event"], + "from": "NEXMO_NUMBER", + "endpoint": [ + { + "type": "phone", + "number": "RECIPIENT_NUMBER" + } + ] + } + ] + return jsonify(ncco) + +@app.route("/webhooks/recordings", methods=['POST']) +def recordings(): + data = request.get_json() + pprint(data) + return "Webhook received" + +if __name__ == '__main__': + app.run(port=3000) diff --git a/voice/record-a-call.py b/voice/record-a-call.py new file mode 100755 index 0000000..33c2185 --- /dev/null +++ b/voice/record-a-call.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 +from flask import Flask, request, jsonify +from pprint import pprint + +app = Flask(__name__) + +@app.route("/webhooks/answer") +def answer_call(): + ncco = [ + { + "action": "talk", + "text": "Hi, we will shortly forward your call. This call is recorded for quality assurance purposes." + }, + { + "action": "record", + "eventUrl": ["https://demo.ngrok.io/webhooks/recordings"] + }, + { + "action": "connect", + "eventUrl": ["https://demo.ngrok.io/webhooks/event"], + "from": "NEXMO_NUMBER", + "endpoint": [ + { + "type": "phone", + "number": "RECIPIENT_NUMBER" + } + ] + } + ] + return jsonify(ncco) + +@app.route("/webhooks/recordings", methods=['POST']) +def recordings(): + data = request.get_json() + pprint(data) + return "webhook received" + +if __name__ == '__main__': + app.run(port=3000) diff --git a/voice/record-a-conversation.py b/voice/record-a-conversation.py new file mode 100755 index 0000000..05ddc64 --- /dev/null +++ b/voice/record-a-conversation.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 +# `eventMethod` is a required workaround currently, otherwise `/webhooks/recordings` is never called. +from flask import Flask, request, jsonify +from pprint import pprint + +app = Flask(__name__) + +@app.route("/webhooks/answer") +def answer_call(): + ncco = [ + { + "action": "conversation", + "name": "CONF_NAME", + "record": "true", + "eventMethod": "POST", + "eventUrl": ["https://demo.ngrok.io/webhooks/recordings"] + } + ] + return jsonify(ncco) + +@app.route("/webhooks/recordings", methods=['POST']) +def recordings(): + data = request.get_json() + pprint(data) + return "Webhook received" + +if __name__ == '__main__': + app.run(port=3000) diff --git a/voice/retrieve-info-for-a-call.py b/voice/retrieve-info-for-a-call.py new file mode 100644 index 0000000..52b2445 --- /dev/null +++ b/voice/retrieve-info-for-a-call.py @@ -0,0 +1,12 @@ +#!/usr/bin/env python3 +import nexmo +from pprint import pprint + +client = nexmo.Client( + application_id=APPLICATION_ID, + private_key=APPLICATION_PRIVATE_KEY_PATH, +) + +# Note call can be made to current call or a completed call +response = client.get_call("NEXMO_CALL_UUID") +pprint(response) diff --git a/voice/retrieve-info-for-all-calls.py b/voice/retrieve-info-for-all-calls.py new file mode 100755 index 0000000..25cb4e4 --- /dev/null +++ b/voice/retrieve-info-for-all-calls.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 +import nexmo +from pprint import pprint +from datetime import datetime, timedelta + +client = nexmo.Client( + application_id=APPLICATION_ID, + private_key=APPLICATION_PRIVATE_KEY_PATH, +) + +NOW = datetime.utcnow() +DATE_END = NOW.replace(microsecond=0).isoformat()+"Z" +DATE_START = (NOW - timedelta(hours=24, minutes=00)).replace(microsecond=0).isoformat()+"Z" + +response = client.get_calls(date_start=DATE_START, date_end=DATE_END) +calls = response['_embedded']['calls'] +for call in calls: + pprint(call) diff --git a/voice/transfer-a-call.py b/voice/transfer-a-call.py new file mode 100755 index 0000000..9abb8ea --- /dev/null +++ b/voice/transfer-a-call.py @@ -0,0 +1,12 @@ +#!/usr/bin/env python3 +import nexmo +from pprint import pprint + +client = nexmo.Client( + application_id=APPLICATION_ID, + private_key=APPLICATION_PRIVATE_KEY_PATH, +) + +dest = {"type": "ncco", "url": ["https://developer.nexmo.com/ncco/tts.json"]} +response = client.update_call(UUID, action="transfer", destination=dest) +pprint(response)