Skip to content
This repository has been archived by the owner on Nov 18, 2019. It is now read-only.

jamesseanwright/personality-forge-.net

Repository files navigation

Personality Forge.NET

A .NET library for The Personality Forge's Secure API.

Release Notes

They can be found here.

Usage

The latest release build is available via NuGet, or in this repo folder.

The 3 classes that'll permit you to use the API are PersonalityForge, Message, and Response (the last two being located in the Models namespace).

Once you've created an instance ofPersonalityForge, passing your API secret, key, and the remote bot's ID to its constructor, you can send messages to the API by calling Send. This simply takes a screen name and a message, and returns a Response object.

Here's the library's usage within a C# command line application:

class Program
{
    /*the IPersonalityForge interface is available publicly
     *in the namespace JamesWright.PersonalityForge.Interfaces
     *if you want to mock it in your unit tests!*/
    private static IPersonalityForge _personalityForge;

    static void Main(string[] args)
    {
        _personalityForge = new PersonalityForge(<API Secret>, <API Key>, <Bot ID>);

        Console.Write("Enter a username: ");
        string username = Console.ReadLine();
        Converse(username);
    }

    static void Converse(string username)
    {
        Console.Write("\nEnter a message!\n");

        Response response;

        while (true)
        {
            Console.Write(">");
            string message = Console.ReadLine();
            response = _personalityForge.Send(username, message);
            Console.WriteLine("{0}: {1}", response.Message.ChatBotName, response.Message.Text);
        }
    }
}

Error handling

Any exception thrown by the library will be of type PersonalityForgeException. This derives from System.Exception, thus you can get the InnerException property to determine the underlying exception:

try
{
    Response response = _personalityForge.Send(username, message);
}
catch (PersonalityForgeException e)
{
	Console.WriteLine(e.Message);
}

Asynchronous usage

The library also supports asynchronous calls to the service, using .NET 4.5's async/await syntax:

Response response = await _personalityForge.SendAsync(username, message);

See the JamesWright.PersonalityForge.WpfExample WPF program to see how asynchronous usage avoids disruption of the main thread of execution.

Unit tests

There is currently a handful of unit tests. There are more on the way!

About

.NET library for interacting with the Personality Forge Secure API

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages