为什么我的 flutter chatbot(使用 ibm watson assistant)不工作?

Why my flutter chatbot (using ibm watson assistant) is not working?

我是一名新的 flutter 开发人员。 我正在关注 this website,但我的代码无法正常工作。
我认为在我的 flutter 代码中提供凭据信息存在问题。我需要帮助。

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

IBM 详情

代码如下:

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();
  }
}

url 必须看起来像这样 https://gateway.watsonplatform.net/assistant/api/v2

您也可以删除用户名。

例子

WatsonAssistantV2Credential credential = WatsonAssistantV2Credential(
  version: '2019-02-28',
  apikey: 'xxxxxxxxxxxxxxxxxxxx',
  assistantID: 'xxxxx-xxx-xxxxx-xxxx-xxxx',
  url: 'https://gateway.watsonplatform.net/assistant/api/v2',
);

删除 init 函数中的 _callWatsonAssistant(); 并确保你有我使用的版本日期。

Flutter有IBM watson需要添加的依赖,请检查是否添加-

import 'package:flutter_ibm_watson/flutter_ibm_watson.dart';

这里是link了解更多详情。

让我使用第三方工具分享其他选项,例如 kommunicate

创建 Watson 助手后,通过提供 API 键、URL 和 Watson 助手 ID 集成到 Kommunicate 中。集成 Bot 之后,您可以从这里将其添加到您的网站和手机。

为了在Flutter中添加,Kommunicate提供了APP_ID需要在launch conversation中添加。要启动对话,您需要创建一个对话对象。该对象被传递给构建对话函数,并且基于对象的参数,对话是 created/launched.

以下是启动对话以添加 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());
});

将 Flutter SDK 添加到您的应用程序:

  1. 在您的 pubspec.yaml 文件中添加以下依赖项:

    kommunicate_flutter: ^1.1.6

  2. 通过运行将此命令安装为 flutter pub get

  3. 在您的 .dart 文件中导入 kommunicate_flutter 以使用 Kommunicate 中的方法:

    import 'package:kommunicate_flutter/kommunicate_flutter.dart';

  4. 对于 iOS,从终端导航到您的 App/iOS 目录并 运行 以下命令:

pod 安装

 platform :ios, '10.0'
 use_frameworks!

请查找随附的文档和链接以供参考。

Check out the blog for more details

A guide for installing flutter app