概要
TwilioとGoogleCloudSpeechAPIを使用し、Railsで通話内容を音声認識する
手順
GoogleCloudSpeechAPIを有効にする
https://console.cloud.google.com/
Googleサービスアカウントキーを作成する


Gemをインスールする
gem 'twilio-ruby'
gem 'google-cloud-speech'
Railsアプリを作成する
call_controller.rb
require 'google/cloud/speech'
class CallController < ApplicationController
def transcription
recording_url = params[:RecordingUrl]
voice = 'man'
language = 'ja-JP'
response = Twilio::TwiML::VoiceResponse.new
if recording_url.blank?
response.say('お名前をフルネームで教えてください。', voice: voice, language: language)
response.record(timeout: 5, max_length: 5)
else
project = 'プロジェクトID'
keyfile = 'サービスアカウントキーのファイルのパス'
speech = Google::Cloud::Speech.new project: project, keyfile: keyfile
audio = speech.audio open(recording_url),
encoding: :linear16,
language: 'ja-JP',
sample_rate: 8000
results = audio.recognize
result = results.first
if result.present?
transcript = result.transcript
response.say('あなたの名前は、', voice: voice, language: language)
response.say(transcript, voice: voice, language: language)
response.say('です。', voice: voice, language: language)
else
response.say('すみませんが、もう一度お願いします。', voice: voice, language: language)
response.record(timeout: 5, max_length: 5)
end
end
render :xml => response.to_s
end
end
Railsアプリを起動
bundle exec rails s
ngrokを起動する
ngrok http 3000

TwiML Appを作成する
https://www.twilio.com/
https://jp.twilio.com/docs/api/twiml
TwiML Appを確認する

