stuncloud
2/7/2015 - 5:10 AM

PowerShellでおしゃべりする

PowerShellでおしゃべりする


function Send-QuestionToDocomo {
    [CmdletBinding()]
    param(
        [parameter(Mandatory,Position=0)]
        [string] $Question,
        [parameter(Mandatory=$false)]
        [string] $ResponseVariable = $null
    )
    trap {
        break
    }
    $uri = "https://api.apigw.smt.docomo.ne.jp/knowledgeQA/v1/ask?q=$([System.Web.HttpUtility]::UrlEncode($Question))&APIKEY=$($script:DocomoApiKey)"
    $response = Invoke-RestMethod -Method Get -Uri $uri
    if ($ResponseVariable) {
        Set-Variable -Name $ResponseVariable -Value $response -Scope Global
    }
    $Color = if ($response.code[0] -eq 'S') {'Green'} else {'Red'}
    Write-Host $response.message.textForDisplay -ForegroundColor $Color
    $response.answers | ? {
        $_.rank -eq 1
    } | % {
        if ($_.linkText) {Write-Host $_.linkText -ForegroundColor DarkGray}
        if ($_.linkUrl)  {Write-Host $_.linkUrl  -ForegroundColor DarkGray}
    }
}

function Start-TalkingWithDocomo {
    [CmdletBinding()]
    param(
        [parameter(Mandatory,Position=0)]
        [string] $WhatToTell,
        [switch] $ForceDialog,
        [parameter(Mandatory=$false)]
        [string] $ResponseVariable = $null
    )
    trap {
        break
    }
    if ($script:DocomoApiBody.Count -eq 0) {
        New-DocomoApiBody | Out-Null
    }
    if ($ForceDialog) {
        $script:DocomoApiBody.context = ''
        $script:DocomoApiBody.mode    = 'dialog'
    }
    $DocomoApiBody.utt = $WhatToTell
    $uri = "https://api.apigw.smt.docomo.ne.jp/dialogue/v1/dialogue?APIKEY=$($script:DocomoApiKey)"
    $json = $DocomoApiBody | ConvertTo-Json
    Write-Verbose $json
    $Body = [System.Text.Encoding]::UTF8.GetBytes($json)
    $response = Invoke-RestMethod -Method Post -Uri $uri -Body $Body -ContentType 'application/json'
    if ($ResponseVariable) {
        Set-Variable -Name $ResponseVariable -Value $response -Scope Global
    }
    Write-Host $response.utt -ForegroundColor Green
    Write-Verbose $response.context
    Write-Verbose $response.mode
    $script:DocomoApiBody.context = $response.context
    $script:DocomoApiBody.mode    = $response.mode
}

function New-DocomoApiBody {
    $script:DocomoApiBody = @{}
    Set-DocomoApiBody
}

function Set-DocomoApiBody {
    [CmdletBinding()]
    param(
        [ValidateLength(1,10)]
        [string] $NickName = 'すー',
        [ValidatePattern('^[ァ-ヶ]{1,20}$')]
        [string] $NickNameY = 'スー',
        [ValidateSet('男','女')]
        [string] $Sex = '女',
        [ValidateSet('A','B','AB','O')]
        [string] $BloodType = 'O',
        [ValidateScript({
            $_ -ge 1 -and $_ -le $(Get-Date).Year
        })]
        [int]    $BirthDateY = 2005,
        [ValidateRange(1,12)]
        [int]    $BirthDateM = 2,
        [ValidateRange(1,31)]
        [int]    $BirthDateD = 6,
        [ValidateRange(1,999)]
        [int]    $Age = 10,
        [ValidateSet('牡羊座','牡牛座','双子座','蟹座','獅子座','乙女座','天秤座','蠍座','射手座','山羊座','水瓶座','魚座')]
        [string] $Constellations = '水瓶座',
        [string] $Place = '東京',
        [ValidateSet('dialog','srtr')]
        [string] $Mode = 'dialog',
        [ValidateSet('関西弁','赤ちゃん')]
        [string] $Charactor = $null
    )
    $script:DocomoApiBody.utt     = ''
    $script:DocomoApiBody.context = ''
    if ($NickName)       { $script:DocomoApiBody.nickname       = $NickName      }
    if ($NickNameY)      { $script:DocomoApiBody.nickname_y     = $NickNameY     }
    if ($Sex)            { $script:DocomoApiBody.sex            = $Sex           }
    if ($BloodType)      { $script:DocomoApiBody.bloodtype      = $BloodType     }
    if ($BirthDateY)     { $script:DocomoApiBody.birthdateY     = $BirthDateY    }
    if ($BirthDateM)     { $script:DocomoApiBody.birthdateM     = $BirthDateM    }
    if ($BirthDateD)     { $script:DocomoApiBody.birthdateD     = $BirthDateD    }
    if ($Age)            { $script:DocomoApiBody.age            = $Age           }
    if ($Constellations) { $script:DocomoApiBody.constellations = $Constellations}
    if ($Place)          { $script:DocomoApiBody.place          = $Place         }
    if ($Mode)           { $script:DocomoApiBody.mode           = $Mode          }
    switch ($Charactor)  { 
        '関西弁'   {$script:DocomoApiBody.t = 20}
        '赤ちゃん' {$script:DocomoApiBody.t = 30}
    }

    return $script:DocomoApiBody
}

function Get-DocomoApiBody {
    return $script:DocomoApiBody
}

$DocomoApiBody = @{}
$DocomoApiKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

Set-Alias -Name ask    -Value Send-QuestionToDocomo
Set-Alias -Name talkie -Value Start-TalkingWithDocomo