How To Write Reusable and Testable Code with Microsoft Bot Framework

Alex Zaitsev
Chatbots Magazine
Published in
2 min readJun 9, 2017

--

Image source: docs.microsoft.com

How often do you think about the architecture of your project? I believe that every class should be written with the architecture in mind. And it seems that people at Microsoft think the same way. But still, there is a gap in the documentation to Microsoft Bot Framework. In order to grasp how to write code using this framework correctly, you should dig into their samples.

Looking through the samples I understood that the Microsoft guys made their framework very flexible — we just need to learn how to use it. Pretty soon, I understood that we can build scalable software using the so-called libraries concept.

A library consists of related dialogs used for routing purposes. Libraries can be chained together to make the development of complex bots possible.

Awesome! There is a natural way to write the “loosely coupled” software. This means you can unite all your dialogs by their logical purpose. For instance, you can have a bunch of welcome dialogs and create the welcome library for them.

Benefits

First of all, uniting your dialogs by a logical purpose makes libraries reusable for other projects. Moreover, your code becomes much easier to test.

Sample

I’ll provide you with a very quick sample written in JavaScript. Just to be sure that we’re on the same wave.

Here is an example of a very basic library:

To include a library into your bot, just call the library method and specify the path to your script. Calling a dialog from the included library is super-simple and can be done with the usual beginDialog method. Just specify the name of your library (in my sample, its’ welcome), add the colon symbol, slash, and the name of the dialog from the library.

Super-easy!

For further reading, check the documentation to the Library class.

Conclusion

Writing low-coupled code helps you to understand one of the software development design guidelines: the Law of Demeter. It’s the principle of least knowledge, and it reduces dependencies allowing you to build reusable software components that are easier to maintain and test.

Thanks for reading. If you find this article helpful, please click ❤ to recommend.

If this article gets feedback, I’ll continue writing about Microsoft Bot Framework.

Stay tuned for the upcoming articles.

--

--