powershell PowerShell中でおしゃべりする

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

以上是关于powershell PowerShell中でおしゃべりする的主要内容,如果未能解决你的问题,请参考以下文章

text PowerShell中で卷曲の実行

powershell和powershell ise到底分别干啥用,powershell命令那么长怎么记

powershell怎么运行

powershell PowerShell:启动PowerShell作为其他用户提升

powershell [提示输入powershell] #powershell #input #prompt

powershell 测量PowerShell命令或PowerShell脚本的速度