Coder Social home page Coder Social logo

go-avs's Introduction

Alexa Voice Service for Go

GoDoc

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

Requires Go 1.6 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!")
}

go-avs's People

Contributors

bhmorse avatar blixt avatar brentnd avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.