[英]Why my flutter chatbot (using ibm watson assistant) is not working?
I'm a new flutter developer.我是一名新的 flutter 开发人员。 I was following this website , but my code is not working.
我一直在关注这个网站,但我的代码不起作用。
I think there is a problem with giving credential information in my flutter code.我认为在我的 flutter 代码中提供凭证信息存在问题。 I want help.
我需要帮助。
watson credential details (It is a dummy workspace, that's why I'm sharing credential information) watson 凭证详细信息(这是一个虚拟工作区,这就是我共享凭证信息的原因)
Skill ID: 2b1bd4ed-848e-47b0-b23e-87c2e3efad15
Legacy v1 workspace URL: https://gateway.watsonplatform.net/assistant/api/v1/workspaces/2b1bd4ed-848e-47b0-b23e-87c2e3efad15/message
Service credentials name: auto-generated-apikey-41a95c12-c0f8-4752-bd47-6c52b524f399
API key: PHXjwNDFRQIaTD4j-7WvKsH3g0e5GWsQba_dyX5687lI
code is below:代码如下:
import 'package:flutter/material.dart';
import 'package:watson_assistant_v2/watson_assistant_v2.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Watson Assistant Example'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String _text;
WatsonAssistantV2Credential credential = WatsonAssistantV2Credential(
version: '2021-07-01',
username: 'Customer Care SampleSkill',
apikey: 'PHXjwNDFRQIaTD4j-7WvKsH3g0e5GWsQba_dyX5687lI',
assistantID: '2b1bd4ed-848e-47b0-b23e-87c2e3efad15',
url: 'https://gateway.watsonplatform.net/assistant/api/v1/workspaces/2b1bd4ed-848e-47b0-b23e-87c2e3efad15/message',
);
WatsonAssistantApiV2 watsonAssistant;
WatsonAssistantResponse watsonAssistantResponse;
WatsonAssistantContext watsonAssistantContext =
WatsonAssistantContext(context: {});
final myController = TextEditingController();
void _callWatsonAssistant() async {
watsonAssistantResponse = await watsonAssistant.sendMessage(
myController.text, watsonAssistantContext);
setState(() {
_text = watsonAssistantResponse.resultText;
});
watsonAssistantContext = watsonAssistantResponse.context;
myController.clear();
}
@override
void initState() {
super.initState();
watsonAssistant =
WatsonAssistantApiV2(watsonAssistantCredential: credential);
_callWatsonAssistant();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Watson_Assistant_V2'),
centerTitle: true,
actions: <Widget>[
IconButton(
icon: Icon(
Icons.restore,
),
onPressed: () {
watsonAssistantContext.resetContext();
setState(() {
_text = null;
});
},
)
],
),
body: Scaffold(
backgroundColor: Colors.white,
body: Padding(
padding: EdgeInsets.symmetric(horizontal: 24.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
TextField(
controller: myController,
decoration: InputDecoration(
hintText: 'Your Input to the chatbot',
contentPadding:
EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0),
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(32.0)),
),
enabledBorder: OutlineInputBorder(
borderSide:
BorderSide(color: Colors.lightBlueAccent, width: 1.0),
borderRadius: BorderRadius.all(Radius.circular(32.0)),
),
focusedBorder: OutlineInputBorder(
borderSide:
BorderSide(color: Colors.lightBlueAccent, width: 2.0),
borderRadius: BorderRadius.all(Radius.circular(32.0)),
),
),
),
SizedBox(
height: 8.0,
),
Text(
_text != null ? '$_text' : 'Watson Response Here',
style: Theme.of(context).textTheme.display1,
),
SizedBox(
height: 24.0,
),
],
),
),
),
floatingActionButton: FloatingActionButton(
onPressed: _callWatsonAssistant,
child: Icon(Icons.send),
),
);
}
@override
void dispose() {
myController.dispose();
super.dispose();
}
}
Flutter has dependencies that needs to be added for IBM watson, please check if you have added - Flutter有依赖需要为IBM watson添加,请检查是否添加-
import 'package:flutter_ibm_watson/flutter_ibm_watson.dart';
Here is the link for more details.这是有关更多详细信息的链接。
Let me share other option using third-party tool like kommunicate让我使用第三方工具(如kommunicate )分享其他选项
After creating Watson assistant, integrate in Kommunicate by providing the API key, URL, and Watson Assistant ID.创建 Watson Assistant 后,通过提供 API 密钥、URL 和 Watson Assistant ID 来集成 Kommunicate。 After that Bot will be integrated, from here you can add it to your website & mobile.
之后,Bot 将被集成,您可以从这里将其添加到您的网站和移动设备中。
For adding in Flutter, Kommunicate provides APP_ID which needs to be added in launch conversation.为了在 Flutter 中添加,Kommunicate 提供了 APP_ID,需要在启动对话中添加。 To launch the conversation you need to create a conversation object.
要启动对话,您需要创建对话 object。 This object is passed to the build conversation function and based on the parameters of the object the conversation is created/launched.
这个 object 被传递给构建对话 function 并基于 object 的参数创建/启动对话。
Below is an example of launch conversation to add the APP_id,下面是一个添加 APP_id 的启动对话示例,
dynamic conversationObject = {
'appId': '<APP_ID>',// The [APP_ID](https://dashboard.kommunicate.io/settings/install) obtained from kommunicate dashboard.
};
KommunicateFlutterPlugin.buildConversation(conversationObject)
.then((clientConversationId) {
print("Conversation builder success : " + clientConversationId.toString());
}).catchError((error) {
print("Conversation builder error : " + error.toString());
});
To Add Flutter SDK to your app:要将 Flutter SDK 添加到您的应用程序:
Add the below dependency in your pubspec.yaml
file:在您的
pubspec.yaml
文件中添加以下依赖项:
kommunicate_flutter: ^1.1.6
Install the package by running this command as flutter pub get
通过运行此命令安装 package 作为
flutter pub get
Import kommunicate_flutter in your.dart file to use the methods from Kommunicate:在 your.dart 文件中导入 kommunicate_flutter 以使用 Kommunicate 中的方法:
import 'package:kommunicate_flutter/kommunicate_flutter.dart';
For iOS, navigate to your App/iOS directory from the terminal and run the below command:对于 iOS,从终端导航到您的 App/iOS 目录并运行以下命令:
pod install吊舱安装
platform :ios, '10.0'
use_frameworks!
Please find the attached docs & links for reference.请查找随附的文档和链接以供参考。
Check out the blog for more details 查看博客了解更多详情
A guide for installing flutter app flutter 应用程序安装指南
Thank You谢谢你
The url has to look like this https://gateway.watsonplatform.net/assistant/api/v2
. url 必须看起来像这样
https://gateway.watsonplatform.net/assistant/api/v2
。
You can also remove the username too.您也可以删除用户名。
Example例子
WatsonAssistantV2Credential credential = WatsonAssistantV2Credential(
version: '2019-02-28',
apikey: 'xxxxxxxxxxxxxxxxxxxx',
assistantID: 'xxxxx-xxx-xxxxx-xxxx-xxxx',
url: 'https://gateway.watsonplatform.net/assistant/api/v2',
);
Get rid of _callWatsonAssistant();
摆脱
_callWatsonAssistant();
in the init function and make sure you have the version date that I used.在 init function 中并确保你有我使用的版本日期。
Let me know if this helped!让我知道这是否有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.