Skip to content

Commit 8afa613

Browse files
authored
Merge pull request #23 from nexmo-community/tony-contribs
Add new building blocks.
2 parents 71c2ce1 + aa029b9 commit 8afa613

13 files changed

+261
-7
lines changed

.gitignore

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,8 @@ ENV/
8888
# Rope project settings
8989
.ropeproject
9090

91-
# nexmo key file
92-
private.key
91+
# nexmo key files
92+
*.key
93+
94+
.DS_Store
95+
settings.json

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
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)
44

5-
The purpose of the quickstart guide is to provide simple examples focused
6-
on one goal. For example, sending and SMS, handling and incoming SMS webhook,
7-
making a Text to Speech call.
5+
The purpose of the Quickstart guide is to provide simple examples focused
6+
on one goal. For example, sending an SMS, handling an incoming SMS webhook,
7+
or making a Text to Speech call.
88

99
## Setup
1010

11-
To use this sample you will first need a [Nexmo account][sign-up]. Then rename
12-
the `.env-example` file to `.env` and set the values as required.
11+
To use the examples you will first need a [Nexmo account][sign-up]. Then rename
12+
the `example.env` file to `.env` and set the values as required.
1313

1414
For some of the examples you will need to [buy a number][buy-number].
1515

voice/connect-an-inbound-call.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env python3
2+
from flask import Flask, request, jsonify
3+
4+
app = Flask(__name__)
5+
6+
@app.route("/webhooks/answer")
7+
def answer_call():
8+
ncco = [
9+
{
10+
"action": "connect",
11+
"from": "NEXMO_NUMBER",
12+
"endpoint": [{
13+
"type": 'phone',
14+
"number": "RECIPIENT_NUMBER"
15+
}]
16+
}
17+
]
18+
return jsonify(ncco)
19+
20+
if __name__ == '__main__':
21+
app.run(port=3000)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env python3
2+
from flask import Flask, request, jsonify
3+
4+
app = Flask(__name__)
5+
6+
7+
@app.route("/webhooks/answer")
8+
def answer_call():
9+
ncco = [
10+
{
11+
"action": "talk",
12+
"text": "Please wait while we connect you to the conference"
13+
},
14+
{
15+
"action": "conversation",
16+
"name": CONF_NAME
17+
}]
18+
return jsonify(ncco)
19+
20+
21+
if __name__ == '__main__':
22+
app.run(port=3000)

voice/earmuff-a-call.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env python3
2+
import nexmo
3+
import time
4+
from pprint import pprint
5+
6+
client = nexmo.Client(
7+
application_id=APPLICATION_ID,
8+
private_key=APPLICATION_PRIVATE_KEY_PATH,
9+
)
10+
11+
response = client.update_call(UUID, action="earmuff")
12+
pprint(response)
13+
time.sleep(5)
14+
response = client.update_call(UUID, action="unearmuff")
15+
pprint(response)

voice/join-outbound-calls.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env python3
2+
from flask import Flask, request, jsonify
3+
4+
app = Flask(__name__)
5+
6+
@app.route("/webhooks/answer")
7+
def answer_call():
8+
ncco = [
9+
{
10+
"action": "connect",
11+
"from": "NEXMO_NUMBER",
12+
"endpoint": [{
13+
"type": 'phone',
14+
"number": "RECIPIENT_NUMBER_1"
15+
}]
16+
},
17+
{
18+
"action": "connect",
19+
"from": "NEXMO_NUMBER",
20+
"endpoint": [{
21+
"type": 'phone',
22+
"number": "RECIPIENT_NUMBER_2"
23+
}]
24+
}
25+
]
26+
return jsonify(ncco)
27+
28+
if __name__ == '__main__':
29+
app.run(port=3000)

voice/mute-a-call.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env python3
2+
import nexmo
3+
import time
4+
from pprint import pprint
5+
6+
client = nexmo.Client(
7+
application_id=APPLICATION_ID,
8+
private_key=APPLICATION_PRIVATE_KEY_PATH,
9+
)
10+
11+
response = client.update_call(UUID, action="mute")
12+
pprint(response)
13+
time.sleep(5)
14+
response = client.update_call(UUID, action="unmute")
15+
pprint(response)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env python3
2+
from flask import Flask, request, jsonify
3+
from pprint import pprint
4+
5+
app = Flask(__name__)
6+
7+
@app.route("/webhooks/answer")
8+
def answer_call():
9+
ncco = [
10+
{
11+
"action": "talk",
12+
"text": "Hi, we will shortly forward your call. This call is recorded for quality assurance purposes."
13+
},
14+
{
15+
"action": "record",
16+
"split" : "conversation",
17+
"eventUrl": ["https://demo.ngrok.io/webhooks/recordings"]
18+
},
19+
{
20+
"action": "connect",
21+
"eventUrl": ["https://demo.ngrok.io/webhooks/event"],
22+
"from": "NEXMO_NUMBER",
23+
"endpoint": [
24+
{
25+
"type": "phone",
26+
"number": "RECIPIENT_NUMBER"
27+
}
28+
]
29+
}
30+
]
31+
return jsonify(ncco)
32+
33+
@app.route("/webhooks/recordings", methods=['POST'])
34+
def recordings():
35+
data = request.get_json()
36+
pprint(data)
37+
return "Webhook received"
38+
39+
if __name__ == '__main__':
40+
app.run(port=3000)

voice/record-a-call.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python3
2+
from flask import Flask, request, jsonify
3+
from pprint import pprint
4+
5+
app = Flask(__name__)
6+
7+
@app.route("/webhooks/answer")
8+
def answer_call():
9+
ncco = [
10+
{
11+
"action": "talk",
12+
"text": "Hi, we will shortly forward your call. This call is recorded for quality assurance purposes."
13+
},
14+
{
15+
"action": "record",
16+
"eventUrl": ["https://demo.ngrok.io/webhooks/recordings"]
17+
},
18+
{
19+
"action": "connect",
20+
"eventUrl": ["https://demo.ngrok.io/webhooks/event"],
21+
"from": "NEXMO_NUMBER",
22+
"endpoint": [
23+
{
24+
"type": "phone",
25+
"number": "RECIPIENT_NUMBER"
26+
}
27+
]
28+
}
29+
]
30+
return jsonify(ncco)
31+
32+
@app.route("/webhooks/recordings", methods=['POST'])
33+
def recordings():
34+
data = request.get_json()
35+
pprint(data)
36+
return "webhook received"
37+
38+
if __name__ == '__main__':
39+
app.run(port=3000)

voice/record-a-conversation.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python3
2+
# `eventMethod` is a required workaround currently, otherwise `/webhooks/recordings` is never called.
3+
from flask import Flask, request, jsonify
4+
from pprint import pprint
5+
6+
app = Flask(__name__)
7+
8+
@app.route("/webhooks/answer")
9+
def answer_call():
10+
ncco = [
11+
{
12+
"action": "conversation",
13+
"name": "CONF_NAME",
14+
"record": "true",
15+
"eventMethod": "POST",
16+
"eventUrl": ["https://demo.ngrok.io/webhooks/recordings"]
17+
}
18+
]
19+
return jsonify(ncco)
20+
21+
@app.route("/webhooks/recordings", methods=['POST'])
22+
def recordings():
23+
data = request.get_json()
24+
pprint(data)
25+
return "Webhook received"
26+
27+
if __name__ == '__main__':
28+
app.run(port=3000)

voice/retrieve-info-for-a-call.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env python3
2+
import nexmo
3+
from pprint import pprint
4+
5+
client = nexmo.Client(
6+
application_id=APPLICATION_ID,
7+
private_key=APPLICATION_PRIVATE_KEY_PATH,
8+
)
9+
10+
# Note call can be made to current call or a completed call
11+
response = client.get_call("NEXMO_CALL_UUID")
12+
pprint(response)

voice/retrieve-info-for-all-calls.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env python3
2+
import nexmo
3+
from pprint import pprint
4+
from datetime import datetime, timedelta
5+
6+
client = nexmo.Client(
7+
application_id=APPLICATION_ID,
8+
private_key=APPLICATION_PRIVATE_KEY_PATH,
9+
)
10+
11+
NOW = datetime.utcnow()
12+
DATE_END = NOW.replace(microsecond=0).isoformat()+"Z"
13+
DATE_START = (NOW - timedelta(hours=24, minutes=00)).replace(microsecond=0).isoformat()+"Z"
14+
15+
response = client.get_calls(date_start=DATE_START, date_end=DATE_END)
16+
calls = response['_embedded']['calls']
17+
for call in calls:
18+
pprint(call)

voice/transfer-a-call.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env python3
2+
import nexmo
3+
from pprint import pprint
4+
5+
client = nexmo.Client(
6+
application_id=APPLICATION_ID,
7+
private_key=APPLICATION_PRIVATE_KEY_PATH,
8+
)
9+
10+
dest = {"type": "ncco", "url": ["https://developer.nexmo.com/ncco/tts.json"]}
11+
response = client.update_call(UUID, action="transfer", destination=dest)
12+
pprint(response)

0 commit comments

Comments
 (0)