【发布时间】: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