|
1 | 1 | #!/usr/bin/env python3 |
2 | | -from pprint import pprint |
3 | 2 | import os |
4 | 3 | from os.path import join, dirname |
5 | 4 | from dotenv import load_dotenv |
6 | | -import vonage |
7 | | - |
8 | 5 |
|
9 | 6 | dotenv_path = join(dirname(__file__), "../.env") |
10 | 7 | load_dotenv(dotenv_path) |
11 | 8 |
|
12 | 9 | VONAGE_API_KEY = os.getenv('VONAGE_API_KEY') |
13 | 10 | VONAGE_API_SECRET = os.getenv('VONAGE_API_SECRET') |
14 | 11 |
|
15 | | -client = vonage.Client( |
16 | | - key=VONAGE_API_KEY, |
17 | | - secret=VONAGE_API_SECRET |
| 12 | +from vonage import Auth, Vonage |
| 13 | +from vonage_application import ( |
| 14 | + ApplicationConfig, |
| 15 | + ApplicationData, |
| 16 | + ApplicationUrl, |
| 17 | + Capabilities, |
| 18 | + Messages, |
| 19 | + MessagesWebhooks, |
| 20 | + Region, |
| 21 | + Verify, |
| 22 | + VerifyWebhooks, |
| 23 | + Voice, |
| 24 | + VoiceUrl, |
| 25 | + VoiceWebhooks, |
| 26 | +) |
| 27 | + |
| 28 | +client = Vonage(Auth(api_key=VONAGE_API_KEY, api_secret=VONAGE_API_SECRET)) |
| 29 | + |
| 30 | +# Voice application options |
| 31 | +voice = Voice( |
| 32 | + webhooks=VoiceWebhooks( |
| 33 | + answer_url=VoiceUrl( |
| 34 | + address='https://example.com/answer', |
| 35 | + http_method='POST', |
| 36 | + connect_timeout=500, |
| 37 | + socket_timeout=3000, |
| 38 | + ), |
| 39 | + fallback_answer_url=VoiceUrl( |
| 40 | + address='https://example.com/fallback', |
| 41 | + http_method='POST', |
| 42 | + connect_timeout=500, |
| 43 | + socket_timeout=3000, |
| 44 | + ), |
| 45 | + event_url=VoiceUrl( |
| 46 | + address='https://example.com/event', |
| 47 | + http_method='POST', |
| 48 | + connect_timeout=500, |
| 49 | + socket_timeout=3000, |
| 50 | + ), |
| 51 | + ), |
| 52 | + signed_callbacks=True, |
| 53 | + conversations_ttl=8000, |
| 54 | + leg_persistence_time=14, |
| 55 | + region=Region.NA_EAST, |
| 56 | +) |
| 57 | + |
| 58 | +# Messages application options |
| 59 | +messages = Messages( |
| 60 | + version='v1', |
| 61 | + webhooks=MessagesWebhooks( |
| 62 | + inbound_url=ApplicationUrl( |
| 63 | + address='https://example.com/inbound', http_method='POST' |
| 64 | + ), |
| 65 | + status_url=ApplicationUrl( |
| 66 | + address='https://example.com/status', http_method='POST' |
| 67 | + ), |
| 68 | + ), |
| 69 | + authenticate_inbound_media=True, |
18 | 70 | ) |
19 | 71 |
|
20 | | -response = client.application.create_application({ |
21 | | - "name": "Code Example App", |
22 | | - "capabilities": { |
23 | | - "messages": { |
24 | | - "webhooks": { |
25 | | - "inbound_url": { |
26 | | - "address": "https://example.com/webhooks/inbound", |
27 | | - "http_method": "POST" |
28 | | - }, |
29 | | - "status_url": { |
30 | | - "address": "https://example.com/webhooks/status", |
31 | | - "http_method": "POST" |
32 | | - } |
33 | | - } |
34 | | - } |
35 | | - } |
36 | | -}) |
37 | | - |
38 | | -pprint(response) |
| 72 | +# Verify application options |
| 73 | +verify = Verify( |
| 74 | + webhooks=VerifyWebhooks( |
| 75 | + status_url=ApplicationUrl(address='https://example.com/status', http_method='GET') |
| 76 | + ), |
| 77 | +) |
| 78 | + |
| 79 | +# Set the application capabilities |
| 80 | +capabilities = Capabilities(voice=voice, messages=messages, verify=verify) |
| 81 | + |
| 82 | +# Set the application configuration that will be applied |
| 83 | +params = ApplicationConfig( |
| 84 | + name='My Custom Application', |
| 85 | + capabilities=capabilities, |
| 86 | +) |
| 87 | + |
| 88 | +# Call the API |
| 89 | +response: ApplicationData = client.application.create_application(params) |
| 90 | + |
| 91 | +print(response) |
0 commit comments