Skip to content

Commit

Permalink
Merge pull request #2 from a-company-jp/feature/call_vonage
Browse files Browse the repository at this point in the history
電話を呼ぶAPI
  • Loading branch information
Shion1305 authored Oct 11, 2024
2 parents 45b4361 + 3dd5cab commit 6ec6cce
Showing 1 changed file with 103 additions and 0 deletions.
103 changes: 103 additions & 0 deletions cmd/vonage/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"github.com/joho/godotenv"
"io/ioutil"
"net/http"
"os"
)

const (
API_URL = "https://studio-api-us.ai.vonage.com/telephony/make-call"
)

func main() {
phoneNumber := "1234567890"
err := CallPhoneApi(phoneNumber)
if err != nil {
fmt.Println("Error making API call:", err)
}
}

func CallPhoneApi(phoneNumber string) error {
XKey, err := FetchEnvVar("XKey")
if err != nil {
return err
}

AgentId, err := FetchEnvVar("AGENT_ID")
if err != nil {
return err
}

requestBody, err := CreateRequestBody(AgentId, phoneNumber)
if err != nil {
return err
}

responseBody, err := SendApiRequest(requestBody, XKey)
if err != nil {
return err
}

fmt.Println("Response Body:", responseBody)
return nil
}

func FetchEnvVar(key string) (string, error) {
err := godotenv.Load(".env")
if err != nil {
fmt.Println("Error loading .env file:", err)
return "", err
}

value := os.Getenv(key)
if value == "" {
return "", fmt.Errorf("%s is not set in the .env file", key)
}
return value, nil
}

func CreateRequestBody(agentId, phoneNumber string) ([]byte, error) {
requestBody, err := json.Marshal(map[string]string{
"agent_id": agentId,
"to": phoneNumber,
})
if err != nil {
fmt.Println("Error marshalling JSON:", err)
return nil, err
}
return requestBody, nil
}

func SendApiRequest(requestBody []byte, XKey string) (string, error) {
req, err := http.NewRequest("POST", API_URL, bytes.NewBuffer(requestBody))
if err != nil {
fmt.Println("Error creating request:", err)
return "", err
}

req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Vgai-Key", XKey)

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error sending request:", err)
return "", err
}
defer resp.Body.Close()

fmt.Println("Response Status:", resp.Status)

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading response body:", err)
return "", err
}

return string(body), nil
}

0 comments on commit 6ec6cce

Please sign in to comment.