Giter Site home page Giter Site logo

alexa_tweet's Introduction

alexa_tweet

Amazon echoでツイッターにツイートするスキルのサンプルプログラムです.以下の内容を含みます.

  • ツイッターアカウントとのアカウントリンク(ApiGateway + Lambda を利用したOAuth認証)
  • ツイートするスキル本体

解説記事

https://qiita.com/hiroyky/items/b8375292a62ee5e4f646

ビルド & デプロイ

nodejsを実行できる環境が必要です.

ビルド

$ npm install
$ npm build

デプロイ

下記コマンドでApiGateway, Lambdaを作成します.

コンポーネントを構築

aws cloudformation package --template-file deploy/sam.yml --s3-bucket ${S3バケット名} --output-template-file ./artifacts/sam.yml 
aws cloudformation deploy --template-file ./artifacts/sam.yml --stack-name alexa-tweet --capabilities CAPABILITY_NAMED_IAM

Parameter Storeに値を設定

TwitterのConsumerKey, ConsumerSecretをSSM Parameter Storeに登録します.

aws ssm put-parameter --name TWITTER_CONSUMER_KEY --value {consumer key} --type SecureString
aws ssm put-parameter --name TWITTER_CONSUMER_SECRET --value {consumer secret} --type SecureString

Lambdaの環境変数追加

Lambda: alexa-oauth_twitter の環境変数「CALLBACK_URL」にAPI Gatewayの/oauth/callbackのURLを登録します.(本来なら固定のドメインを取得してsamファイルで設定するのが理想)

ツイッターの設定

ツイッターのアプリ管理画面を開き,Callback URLsにAPI Gatewayの /oauth/request_token のURLを追加する.

  • 最低2つのURLを指定する必要があるようなので,API GatewayのStageとdefualtのバージョンを指定する.

Amazon Alexa Skillの設定

アカウントリンク

認証種別としてImplicit Grantを選択し,認証画面URIにAPI Gatewayの/oauth/request_tokenのURLを設定する.

エンドポイント

エンドポイントに,alexa-tweet_skillのLambdaを設定する

インテント

TweetRequestとしてツイートの発話のリクエストを作成する.

構成

ソースコードのエントリポイント

  • src/oauth/index.ts: OAuth認証をするためのREST API.
  • src/skill/index.ts: Amazon echoスキル本体コード.

OAuth認証

Amazon echoのスキルがツイートをするためにツイッターのOAuth認証を行います. Amazon echoはOAuth2が必要ですが,2018年7月29日現在ツイッターのツイート用APIはOAuth1認証です. そこで,認証時にトンネリングすることで,OAuth1, OAuth2の変換を行っています.

トンネリングを行うために,ApiGateway + LambdaでAmazon echoからの認証をリクエストを受け付け認証処理を仲介します.

依存パラメータの取得

ツイッターのAPIキー(Consumer Key) 及びAPI秘密キー(Consumer Secret)はSSMのParameterStoreから取得します. ソースコード上では,envのプロパティから取得しています.

内容 パラメータストア ソースコード内での取得
Consumer Key TWITTER_CONSUMER_KEY env.twitter.oauth.key
Consumer Secret TWITTER_CONSUMER_SECRET env.twitter.oauth.secret

alexa_tweet's People

Contributors

hiroyky avatar

Watchers

 avatar  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.