2016-12-02 4 views
0

私はwit.aiで新しいアプリケーションを作成しました。物語では、「User says」の後に、「Bot runs」を使ってgetReply()関数を追加し、そこにブランチを持つ2つのコンテキストキーを追加しました。両方のキーが利用可能な場合は、「Bot says」を使用してユーザーに返信します。次のステップに進むと、ユーザーに不足しているキーを要求します。返信にはコンテキストキーが初期化されていません - wit.ai

問題は、返信でコンテキストキーの1つのみを使用しています。そのキーが利用可能な場合、フローは機能します。他のキーについては考慮しません。私たちが両方のキーを返信に追加する場合にのみ、両方のキーをチェックしています。

コードでは、関数getReply()のキーをチェックしてから、コンテキストに追加します。

const actions = { 
send(request, response) { 
    ... 
    ... 
    ... 
    return new Promise(function(resolve, reject) { 
    ... 
    return resolve(); 
    }) 
    .then()) => null) 
    .catch((err) => { 
     'Error occurred', 
     id, 
     ':', 
     err.stack || err 
    ); 
    }); 
}, 
function getReply({context, entities}) { 
    return new Promise(function(resolve, reject) { 
    ... 
    ... 
    ... 
    context.key1 = value1; 
    context.key2 = value2; 
    return resolve(context); 
    } 
} 

何かが間違っていますか?コンテキストキーが応答に含まれていないと開始されない理由

ありがとうございました。

答えて

0

あなたの例では、いくつかのロジックに応じて、コンテキストでキーを追加または削除することはありません。私は、Pythonでユーザから与えられた電子メールをどのように検証すればよいか、そしてユーザが無効な電子メールを送信した場合のコンテキストキーをどう扱うべきかという少しの例を示しました。私はそれがあなたにコンテキストキーを使用する方法のいくつかのアイデアを与えることを願っています。

def save_email(br, cc): 
    """Save email on context. 

     Arguments: 
     - br (dict)   Bot Response 
     - cc (dict)   Conversation Context 

    Returns an updated context 
    """ 
    context = br.get('context', {}) 
    entities = br['entities'] 
    email = first_entity_value(entities, 'email') # Use wit/email builtin entity 

    # First clean the context, its possible you saved one of those two keys in 
    # previous iterations, so clean them all just to be safe. As we are going to 
    # run email validation in the code below its safe to remove this keys/clean the 
    # context at the top of this method. 
    for key in ['email', 'bad-email']: 
     if key in context: 
      del context[key] 

    # Now validate that email is valid. If its valid, we add an 'email' key 
    # to the context. If email is not valid, we add a 'bad-email' key to the 
    # context. In this way, we can handle then at Wit.ai, depending which key 
    # is present, what the bot should do next. 
    validator = EmailValidator() 
    if validator(email).is_valid(): 
     context['email'] = email 
    else: 
     context['bad-email'] = 1 # 1, 0, any value. What Wit will pay attention is 
           # that the 'bad-key' exists in the context. 

    # Finally, return the updated context 
    return context 

あなたはそれが文脈上の両方メール悪い-メールを置くことに意味を持っているdoesntの見ることができるように。私はその場合ウィットが何をするのか分かりません。 Witが次の動作をどのように予測するかを変更できるコンテキストキーを思い出させます。

私は

エミリアーノ、ベスト

、これは明確にし、あなたはあなたの問題を解決するのに役立ちます願っています。

関連する問題