Skip to content

rogertalk/go-avs

Repository files navigation

Alexa Voice Service for Go

GoDoc

A simple package for communicating with Amazon’s HTTP/2 API for AVS.

Requires Go 1.8 or later.

Example

package main

import (
  "fmt"
  "io/ioutil"
  "os"

  "github.com/fika-io/go-avs"
)

// Put your access token below.
const ACCESS_TOKEN = "YOUR ACCESS TOKEN"

func main() {
  // Record your request into request.wav.
  audio, _ := os.Open("./request.wav")
  response, err := avs.PostRecognize(ACCESS_TOKEN, "abc123", "abc123dialog", audio)
  if err != nil {
    fmt.Printf("Failed to call AVS: %v\n", err)
    return
  }
  // AVS might not return any directives in some cases.
  if len(response.Directives) == 0 {
    fmt.Println("Alexa had nothing to say.")
    return
  }
  // A response can have multiple directives in the response.
  for _, directive := range response.Directives {
    switch d := directive.Typed().(type) {
    case *avs.ExpectSpeech:
      fmt.Printf("Alexa wants you to speak within %s!\n", d.Timeout())
    case *avs.Play:
      // The Play directive can point to attached audio or remote streams.
      if cid := d.Payload.AudioItem.Stream.ContentId(); cid != "" {
        save(response, cid)
      } else {
        fmt.Println("Remote stream:", d.Payload.AudioItem.Stream.URL)
      }
    case *avs.Speak:
      // The Speak directive always points to attached audio.
      save(response, d.ContentId())
    default:
      fmt.Println("No code to handle directive:", d)
    }
  }
}

var savedFiles = 0

// Function that saves an audio file to disk.
func save(resp *avs.Response, cid string) {
  savedFiles++
  filename := fmt.Sprintf("./response%d.mp3", savedFiles)
  ioutil.WriteFile(filename, resp.Content[cid], 0666)
  fmt.Println("Saved Alexa’s response to", filename)
}

Downchannels

You can open a downchannel with the CreateDownchannel method. It's implemented as a read-only channel of Message pointers.

package main

import (
  "fmt"

  "github.com/fika-io/go-avs"
)

// Put your access token below.
const ACCESS_TOKEN = "YOUR ACCESS TOKEN"

func main() {
  directives, err := avs.CreateDownchannel(ACCESS_TOKEN)
  if err != nil {
    fmt.Printf("Failed to open downchannel: %v\n", err)
    return
  }
  // Wait for directives to come in on the downchannel.
  for directive := range directives {
    switch d := directive.Typed().(type) {
    case *avs.DeleteAlert:
      fmt.Println("Unset alert:", d.Payload.Token)
    case *avs.SetAlert:
      fmt.Printf("Set alert %s (%s) for %s\n", d.Payload.Token, d.Payload.Type, d.Payload.ScheduledTime)
    default:
      fmt.Println("No code to handle directive:", d)
    }
  }
  fmt.Println("Downchannel closed. Bye!")
}

About

A simple package for communicating with Amazon’s HTTP/2 API for AVS.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages