Skip to content

Commit

Permalink
add inbound/outbound call snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkahan committed Jan 9, 2025
1 parent 39a1083 commit ecd37c6
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 59 deletions.
1 change: 1 addition & 0 deletions .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ START_DATE='START_DATE'

# Voice
CONFERENCE_NAME='NAME_OF_YOUR_CONFERENCE'
YOUR_SECOND_NUMBER='YOUR_SECOND_NUMBER'

# Numbers
COUNTRY_CODE='GB'
Expand Down
34 changes: 17 additions & 17 deletions voice/connect-an-inbound-call.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
#!/usr/bin/env python3
from flask import Flask, jsonify
import os
from os.path import join, dirname
from dotenv import load_dotenv
from fastapi import FastAPI
from vonage_voice.models import Connect, PhoneEndpoint

app = Flask(__name__)
VONAGE_NUMBER = os.environ.get('VONAGE_NUMBER')
YOUR_SECOND_NUMBER = os.environ.get('YOUR_SECOND_NUMBER')

dotenv_path = join(dirname(__file__), '../.env')
load_dotenv(dotenv_path)
app = FastAPI()

@app.route("/webhooks/answer")
def answer_call():

@app.get('/answer')
async def inbound_call():
ncco = [
{
"action": "connect",
"from": "VONAGE_NUMBER",
"endpoint": [{
"type": 'phone',
"number": "YOUR_SECOND_NUMBER"
}]
}
Connect(
endpoint=[PhoneEndpoint(number='123456789')], from_=VONAGE_NUMBER
).model_dump(by_alias=True, exclude_none=True)
]
return jsonify(ncco)


if __name__ == '__main__':
app.run(port=3000)
return ncco
32 changes: 21 additions & 11 deletions voice/make-an-outbound-call.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,36 @@
import os
from os.path import join, dirname
from pprint import pprint
import vonage
from dotenv import load_dotenv

dotenv_path = join(dirname(__file__), "../.env")
load_dotenv(dotenv_path)

VONAGE_APPLICATION_ID = os.environ.get("VONAGE_APPLICATION_ID")
VONAGE_APPLICATION_PRIVATE_KEY_PATH = os.environ.get("VONAGE_APPLICATION_PRIVATE_KEY_PATH")
VONAGE_APPLICATION_PRIVATE_KEY_PATH = os.environ.get(
"VONAGE_APPLICATION_PRIVATE_KEY_PATH"
)
FROM_NUMBER = os.environ.get("FROM_NUMBER")
TO_NUMBER = os.environ.get("TO_NUMBER")

client = vonage.Client(
application_id=VONAGE_APPLICATION_ID,
private_key=VONAGE_APPLICATION_PRIVATE_KEY_PATH,
from vonage import Auth, Vonage
from vonage_voice.models import CreateCallRequest, Phone, ToPhone

client = Vonage(
Auth(
application_id=VONAGE_APPLICATION_ID,
private_key=VONAGE_APPLICATION_PRIVATE_KEY_PATH,
)
)

response = client.voice.create_call({
'to': [{'type': 'phone', 'number': TO_NUMBER}],
'from': {'type': 'phone', 'number': FROM_NUMBER},
'answer_url': ['https://raw.githubusercontent.com/nexmo-community/ncco-examples/gh-pages/text-to-speech.json']
})
response = client.voice.create_call(
CreateCallRequest(
answer_url=[
'https://raw.githubusercontent.com/nexmo-community/ncco-examples/gh-pages/text-to-speech.json'
],
to=[ToPhone(number=TO_NUMBER)],
from_=Phone(number=FROM_NUMBER),
)
)

pprint(response)
pprint(response)
28 changes: 18 additions & 10 deletions voice/make-outbound-call-ncco.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,34 @@
import os
from os.path import join, dirname
from dotenv import load_dotenv
import vonage

dotenv_path = join(dirname(__file__), "../.env")
load_dotenv(dotenv_path)

VONAGE_APPLICATION_ID = os.environ.get("VONAGE_APPLICATION_ID")
VONAGE_APPLICATION_PRIVATE_KEY_PATH = os.environ.get("VONAGE_APPLICATION_PRIVATE_KEY_PATH")
VONAGE_APPLICATION_PRIVATE_KEY_PATH = os.environ.get(
"VONAGE_APPLICATION_PRIVATE_KEY_PATH"
)

VONAGE_NUMBER = os.environ.get("VONAGE_NUMBER")
TO_NUMBER = os.environ.get("TO_NUMBER")

client = vonage.Client(
application_id=VONAGE_APPLICATION_ID,
private_key=VONAGE_APPLICATION_PRIVATE_KEY_PATH,
from vonage import Auth, Vonage
from vonage_voice.models import CreateCallRequest, Phone, Talk, ToPhone

client = Vonage(
Auth(
application_id=VONAGE_APPLICATION_ID,
private_key=VONAGE_APPLICATION_PRIVATE_KEY_PATH,
)
)

response = client.voice.create_call({
'to': [{'type': 'phone', 'number': TO_NUMBER}],
'from': {'type': 'phone', 'number': VONAGE_NUMBER},
'ncco': [{'action': 'talk', 'text': 'This is a text to speech call from Nexmo'}]
})
response = client.voice.create_call(
CreateCallRequest(
ncco=[Talk(text='Hello world')],
to=[ToPhone(number=TO_NUMBER)],
from_=Phone(number=VONAGE_NUMBER),
)
)

pprint(response)
31 changes: 10 additions & 21 deletions voice/receive-an-inbound-call.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
#!/usr/bin/env python3
from fastapi import FastAPI, Query
from vonage_voice.models import Talk

from flask import Flask, request, jsonify
app = FastAPI()

app = Flask(__name__)


@app.route("/webhooks/answer")
def answer_call():
for param_key, param_value in request.args.items():
print("{}: {}".format(param_key, param_value))

from_ = request.args['from']

return jsonify([
{
"action": "talk",
"text": "Thank you for calling from " + " ".join(from_)
}
])


if __name__ == '__main__':
app.run(port=3000)
@app.get('/answer')
async def answer_call(from_: str = Query(..., alias='from')):
return [
Talk(text=f'Thank you for calling from {from_}').model_dump(
by_alias=True, exclude_none=True
)
]

0 comments on commit ecd37c6

Please sign in to comment.