对话流回复

时间:2019-01-18 08:14:35

标签: android dialogflow dialogflow-fulfillment

当我们在android studio中安装dialoglow bot时,能否获得在firebase的内联基本编辑器中创建的履行答复?
显然,我们可以使用.getFulfillment.getSpeech()方法从Bot的意图中获得回复。

那么我们有什么可以做的吗?

打包com.example.ft.aidt;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.nfc.Tag;
import android.speech.tts.TextToSpeech;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.JsonElement;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Map;

import ai.api.AIListener;
import ai.api.android.AIConfiguration;
import ai.api.android.AIService;
import ai.api.model.AIError;
import ai.api.model.AIResponse;
import ai.api.model.Result;
import ai.api.ui.AIDialog;

public class ai extends AppCompatActivity implements AIListener {
    public static final String TAG = ai.class.getName();
    private Button bu,nu;
    private TextView resp;
    private  AIService aiService;
    private TextView a;
    private ImageView yu;
    private  TextToSpeech joi;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ai);
        yu  =  (ImageView) findViewById(R.id.imageView20);
        int permi = ContextCompat.checkSelfPermission(this,Manifest.permission.RECORD_AUDIO);
         if(permi != PackageManager.PERMISSION_GRANTED)
         {
             Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show();
             MakeRequest();
         }
        final AIConfiguration config = new AIConfiguration("b0369e8530c14cc0990cccab8b9f0289",
                AIConfiguration.SupportedLanguages.English,
                AIConfiguration.RecognitionEngine.System);

         aiService = AIService.getService(this, config);
        aiService.setListener(this);
        bu =(Button) findViewById(R.id.button12);
        resp=(TextView) findViewById(R.id.textView10);
        a = (TextView) findViewById(R.id.textView12);
            joi = new TextToSpeech(ai.this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {

            }
        });
        yu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(ai.this,"Here you can talk to AIDT and converse with it. If you fail to get a reply, please check your internet connection.",Toast.LENGTH_LONG).show();
            }
        });
        bu.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                resp.setText("ERROR");
                return false;
            }
        });
        bu.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 aiService.startListening();
             }
         });
    }
    protected void MakeRequest() {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO},007);

    }
    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case 007: {

                if (grantResults.length == 0
                        || grantResults[0] !=
                        PackageManager.PERMISSION_GRANTED) {


                } else {
                }
                return;
            }}}

    @Override
    public void onResult( final  AIResponse response) {


            Log.i("", response.toString());
            ArrayList<String> ap = new ArrayList<>();
            ArrayList<String> apk = new ArrayList<>();
            final Result result1 = response.getResult();
            String parameterString = "";
            if (result1.getParameters() != null &&!result1.getParameters().isEmpty()) {
                for (final Map.Entry<String, JsonElement> entry : result1.getParameters().entrySet()) {
                    parameterString += "(" + entry.getKey() + ", " + entry.getValue() + ") ";
                }
            }
            final String sppech = result1.getFulfillment().getSpeech();
            String ae = result1.getResolvedQuery().toString();

            a.setText("\n Baymax: " + result1.getFulfillment().getSpeech());
            resp.setText("\n You: " + result1.getResolvedQuery());
            // Show results in TextView.


            joi.speak(result1.getFulfillment().getSpeech(), TextToSpeech.QUEUE_FLUSH, null, null);
        }


    @Override
    public void onError(AIError error)
    {
     resp.setText(error.toString());
    }

    @Override
    public void onAudioLevel(float level) {

    }

    @Override
    public void onListeningStarted() {


    }

    @Override
    public void onListeningCanceled() {

    }

    @Override
    public void onListeningFinished() {

    }
}

0 个答案:

没有答案