Chatbot SDK for xamarin

Installation#

Xamarin#

Open NuGet package manager

  • Search for YmChat
  • Install the package for
    • Xamarin.Forms
    • Android
    • iOS

Pre-requisite#

Setting up Xamarin.Forms#

  • Go to App.xaml.cs
  • import namespace YmChat
    using YmChat;
  • Accept parameter IYmChat in the App constructor and pass down to the class where you are using the SDK
    public App(IYmChat iymchat)
    {
    InitializeComponent();
    MainPage = new MainPage(iymchat);
    }

Setting up Android#

  • Go to MainActivity.cs
  • import namespace YmChat
    using YmChat;
  • Inside onCreate function add this snippet at the end
    YmChatImplementation ymchat = new YmChatImplementation();
    LoadApplication(new App(ymchat));

Setting up iOS#

  • Go to AppDelegate.cs
  • import namespace YmChat
    using YmChat;
  • Inside FinishedLaunching function add this snippet before return
    YmChatImplementation ymchat = new YmChatImplementation();
    LoadApplication(new App(ymchat));

Usage#

Set botId#

This is the first and compulsory step.

ymChatInterface.setBotId("botId");

YM AuthenticationToken#

ymAuthenticationToken is used to associate an identity of the user with the chat bot.

Whenever chatbot is launched with ymAuthenticationToken it will load the previous chats associated with this user since inception.

ymChatInterface.setAuthenticationToken("token");

Note: History will load only when Show history flag is enabled in the channel settings

Push Notifications#

YMChat supports firebase notifications. Pass your FCM token in setDeviceToken method.

ymChatInterface.setDeviceToken("token");

Note: Firebase service account key is required to send notifications. You can share the service account key with us. More info here

Payload#

Additional information can be passed in the form of key value pair from app to bot using payload.

var Payload = new Dictionary<string, object> { { "Name ", "Ym" } };
ymChatInterface.setPayLoad(Payload);

Payload can be used to pass information from host app to bot. The payload dictionary should be JSON compatible else an error will be thrown

For passing data from bot to app refer bot Bot Events

payload security

Payload is securely passed in HTTPS post request to protect the information passed in it

On-Prem Deployments#

YmChat supports bots with on-prem deployments. For the bot to work, pass the on-prem URL to setCustomURL() method.

ymChatInterface.setCustomURL("https://your-on-prem-url.com");

Speech to Text#

Speech to text can be enabled and disabled by calling setEnableSpeech(). Default value is false

ymChatInterface.setEnableSpeech(true);

iOS#

If you are supporting Speech recognition, add following snippet to Info.plist of the host app

<key>NSMicrophoneUsageDescription</key>
<string>Your microphone will be used to record your speech when you use the Voice feature.</string>
<key>NSSpeechRecognitionUsageDescription</key>
<string>Speech recognition will be used to determine which words you speak into this device&apos;s microphone.</string>

Present chatbot#

Chat bot can be presented by calling startChatbot(). This method will display full screen chat view

ymChatInterface.startChatBot();

Bot Events#

Bot events are used to pass information from bot to app. For passing events from app to bot refer Payload

ymChatInterface.onEventFromBot((botEvent) =>
{
Console.WriteLine(botEvent["code"]);
Console.WriteLine(botEvent["data"]);
});

Bot close event#

Bot close event is separately sent and it can be handled in following way.

ymChatInterface.onBotClose(() =>
{
Console.WriteLine("Chatbot closed");
});

Close bot#

Bot can be closed by tapping on cross button at top, and they can be programmatically closed using closeBot() function

ymChatInterface.closeBot();

Unlink Device Token#

If you want to stop receiving push notifications you can unlink the device token. Device token typically is unlinked when the user logs out of the app.

ymChatInterface.unLinkDeviceToken(
botId,
apiKey,
deviceToken,
(isDeviceTokenUnlinked) {
Console.WriteLine("Device token unlinked");
},
(failureMessage) {
Console.WriteLine(failureMessage);
});

Demo App#

A demo app can be used as a reference to better understand how this SDK can be integrated in the app https://github.com/yellowmessenger/ymchat-xamarin-demo