【问题标题】:Dialogflow bot not responding in flutter appDialogflow bot在flutter应用程序中没有响应
【发布时间】:2021-04-08 22:18:35
【问题描述】:

我已经从 Github https://github.com/neon97/chatbot_dialogflow 克隆了这个项目,但我没有收到机器人的回复。

我已将正确的 Dialogflow JSON 凭据添加到 YAML 文件夹,它们似乎没问题。 任何帮助将不胜感激。

下面我添加main.dart和pubspec的代码

Main.dart

import 'package:bubble/bubble.dart';
import 'package:flutter/material.dart';
import 'package:flutter_dialogflow/dialogflow_v2.dart';

void main() {
  runApp(MaterialApp(
    home: MyApp(),
    debugShowCheckedModeBanner: false,
  ));
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  void response(query) async {
    AuthGoogle authGoogle = await AuthGoogle(
            fileJson: "assets/smartstudentguide-vfml-3892228c2f8e.json")
        .build();
    Dialogflow dialogflow =
        Dialogflow(authGoogle: authGoogle, language: Language.english);
    AIResponse aiResponse = await dialogflow.detectIntent(query);
    setState(() {
      messsages.insert(0, {
        "data": 0,
        "message": aiResponse.getListMessage()[0]["text"]["text"][0].toString()
      });
    });
  }

  final messageInsert = TextEditingController();
  // ignore: deprecated_member_use
  List<Map> messsages = List();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          "Smart Student Guide",
        ),
        backgroundColor: Colors.deepOrange,
      ),
      body: Container(
        child: Column(
          children: <Widget>[
            Flexible(
                child: ListView.builder(
                    reverse: true,
                    itemCount: messsages.length,
                    itemBuilder: (context, index) => chat(
                        messsages[index]["message"].toString(),
                        messsages[index]["data"]))),
            Divider(
              height: 5.0,
              color: Colors.deepOrange,
            ),
            Container(
              padding: EdgeInsets.only(left: 15.0, right: 15.0),
              margin: const EdgeInsets.symmetric(horizontal: 8.0),
              child: Row(
                children: <Widget>[
                  Flexible(
                      child: TextField(
                    controller: messageInsert,
                    decoration: InputDecoration.collapsed(
                        hintText: "Send your message",
                        hintStyle: TextStyle(
                            fontWeight: FontWeight.bold, fontSize: 18.0)),
                  )),
                  Container(
                    margin: EdgeInsets.symmetric(horizontal: 4.0),
                    child: IconButton(
                      
                        icon: Icon(
                          
                          Icons.send,
                          size: 30.0,
                          color: Colors.deepOrange,
                        ),
                        onPressed: () {
                          if (messageInsert.text.isEmpty) {
                            print("empty message");
                          } else {
                            setState(() {
                              messsages.insert(0,
                                  {"data": 1, "message": messageInsert.text});
                            });
                            response(messageInsert.text);
                            messageInsert.clear();
                          }
                        }),
                  )
                ],
              ),
            ),
            SizedBox(
              height: 15.0,
            )
          ],
        ),
      ),
    );
  }

  //for better one i have use the bubble package check out the pubspec.yaml

  Widget chat(String message, int data) {
    return Padding(
      padding: EdgeInsets.all(10.0),
      child: Bubble(
          radius: Radius.circular(15.0),
          color: data == 0 ? Colors.deepOrange : Colors.orangeAccent,
          elevation: 0.0,
          alignment: data == 0 ? Alignment.topLeft : Alignment.topRight,
          nip: data == 0 ? BubbleNip.leftBottom : BubbleNip.rightTop,
          child: Padding(
            padding: EdgeInsets.all(2.0),
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                CircleAvatar(
                  backgroundImage: AssetImage(
                      data == 0 ? "assets/bot.png" : "assets/user.png"),
                ),
                SizedBox(
                  width: 10.0,
                ),
                Flexible(
                    child: Text(
                  message,
                  style: TextStyle(
                      color: Colors.white, fontWeight: FontWeight.bold),
                ))
              ],
            ),
          )),
    );
  }
}

pubspec.yaml

name: chatbot_dialogflow
description: A new Flutter project.
publish_to: 'none' 

version: 1.0.0+1

environment:
  sdk: ">=2.7.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^0.1.3
  flutter_dialogflow: ^0.1.3
  bubble: ^1.1.9+1

dev_dependencies:
  flutter_test:
    sdk: flutter


flutter:


  uses-material-design: true

  assets:
    - assets/smartstudentguide-vfml-3892228c2f8e.json
    - assets/bot.png
    - assets/user.png

我创建了服务帐户并创建了 JSON 凭据并下载了文件并将其添加到资产文件夹中,应用程序运行成功,但机器人没有响应查询。请帮助我,我是 Flutter 的新手,我刚刚开始学习它。 提前致谢。

【问题讨论】:

    标签: flutter dart bots dialogflow-es


    【解决方案1】:

    我不知道您的代码中的确切问题。只是为了贡献。 ,我想提一下这个回购。这种方法非常适合我在我的 Flutter 应用中集成对话流聊天机器人

    https://github.com/Aditsyal/flutter_chatBot

    只需从 lib 复制 dialog_flow.dart 和 fact_message.dart ,按下小部件时导航到 FlutterFactsChatBot() 类。下面提到了我使用的自定义聊天小部件。

    // 第 3 页在 main.dart 中提到 FlutterFactsChatbot() 作为路由。 小部件 factBot(BuildContext 上下文){ 返回容器( 对齐:Alignment.bottomRight, 孩子:浮动动作按钮( materialTapTargetSize: MaterialTapTargetSize.padded, 孩子:中心( 孩子:图标(Icons.chat), ), 海拔:4.0, 背景颜色:颜色.蓝色, onPressed: () => Navigator.pushNamed(context, '/page3'), ) ); }

    【讨论】:

      【解决方案2】:

      如果您仍然面临将 Dialogflow 聊天机器人集成到 Flutter 应用中的问题,那么有很多平台支持集成 Dialogflow 聊天机器人。

      以下是实现相同的步骤:

      1. 将 Dialogflow 聊天机器人集成到第三方平台,例如 Kommunicate

      2. 完成机器人集成后,将聊天功能添加到您的 Flutter 应用中。 查看documentation 将聊天集成到 Flutter 应用中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-16
        • 1970-01-01
        • 2019-03-02
        • 1970-01-01
        • 2021-04-15
        • 1970-01-01
        • 2013-07-18
        相关资源
        最近更新 更多