Microsoft's Bot Framework SDK for Python

by Michael Szul on

Recently, Microsoft announced the Python SDK for the Bot Framework, so now all the Pythonistas can get in on the fun of building chatbots using Microsoft's BotBuilder technology and the Azure Bot Service. It's important to note that this SDK is experimental at the moment, as it is based on SDK v4, which is still evolving.

I had the pleasure of playing around with the example code a little bit while at the Microsoft MVP Summit, and I managed to get the EchoBot up-and-running (the Python SDK docs give you this code), but with Python favoring localized development environments using a specific form of virtualization (really just setting up specific environment variables), there was a little initial ambiguity in the instruction, since you have the build the SDK from the source.

For those who want to play around with it, here are the steps you can take to get things started.

Installation

Make sure you have Python installed (obviously). I recommend the Anaconda distribution, as it comes a lot of the mathematics, data science, and machine learning libraries used for artificial intelligence.

Virtual Environment

Create a folder to house your development code. Let's say you put your repositories in a folder called x on the C:\ drive, and you're project is called py-bot. Create the py-bot folder in C:\x\ then.

I actually have a repo called this that I'm using to mess around with the Python SDK. This blog post is based on the README file that I added to that repository.

Python recommends using virtual environments to keep your dependencies coupled to your project rather than polluting the global namespace.

cd py-bot
      python -m venv .\
      

Now you have a virtual environment set up in the py-bot directory. Don't do anything with it just yet.

The Python SDK is experimental, so you're going to have to build it from the source.

Clone the botbuilder-python repository:

cd ..\
      git clone https://github.com/Microsoft/botbuilder-python
      

Note that I moved outside of the py-bot directory. I put the py-bot working directory and the botbuilder-python directory at the same level in the filesystem to make it easier to navigate and document.

There are couple of things that you will want to do globally, because you are going to need them in your Python workflow anyway. You should already have pip installed, since it hopefully came with your distribution of Python. Now you need to install wheel and make sure a few other libraries are up-to-date.

pip install wheel
      python -m pip install --upgrade pip setuptools wheel
      

If these utilities are not up-to-date, you may get an error when attempting to install the botbuilder output. The most common error is one that doesn't recognize bdist_wheel

Now that your Python environment is up-to-date, you will need to build the botbuilder SDK.

cd botbuilder-python\libraries\botbuilder-schema
      python setup.py bdist_wheel
      
      cd ..\botframework-connector
      python setup.py bdist_wheel
      
      cd ..\botbuilder-core
      python setup.py bdist_wheel
      

Once built, you can install the botbuilder library, but you will want to do that in the virtual environment.

First, go back to the root of the py-bot project, and activate your virtual environment:

.\Scripts\activate
      

Then install the botbuilder library:

pip install ..\botbuilder-python\botbuilder-core\dist\botbuilder_core-4.0.0a0-py3-none-any.whl
      

You can test your environment by running pip list and checking the output. It should look something like this:

Package                Version
      ---------------------- ---------
      asn1crypto             0.24.0
      botbuilder-core        4.0.0a0
      botbuilder-schema      4.0.0a0
      botframework-connector 4.0.0a0
      certifi                2018.1.18
      cffi                   1.11.5
      chardet                3.0.4
      cryptography           2.1.4
      idna                   2.6
      isodate                0.6.0
      msrest                 0.4.26
      oauthlib               2.0.6
      pip                    9.0.1
      pycparser              2.18
      PyJWT                  1.6.0
      requests               2.18.4
      requests-oauthlib      0.8.0
      setuptools             28.8.0
      six                    1.11.0
      urllib3                1.22
      

Now you are ready to run the endpoint, but first you need to grab the example code from the Python SDK wiki, or you can just grab the main.py file from my py-bot repo (it's the same code).

python .\main.py
      

The output will say that the service is listening on localhost at port 9000. Now boot the Bot Emulator and connection to it. The echo bot in main.py is what is on the botbuilder-python SDK wiki, and it will echo back the statements you give it.

Using Visual Studio Code

My preference is to use Visual Studio Code (and the Python extension) for Python development This requires two small changes to get up-and-running with the virtual environment correctly.

In your settings.json file, add the following configuration:

    {
              "python.pythonPath": "C:\\PATH_TO_PYBOT\\Scripts\\python.exe",
              "python.autoComplete.extraPaths": [
                  "C:\\PATH_TO_PYBOT\\Lib\\site-packages"
              ]
          }
      

In this snippet, PATH_TO_PYBOT represents the path to the project root. You may have to reboot Visual Studio Code after making this change. I had difficulty getting the Python extension to recognize the virtual environment as the correct interpreter until after I rebooted. Also, the Python extension will look for pylint, and if it doesn't find it installed, it will attempt to install it.

The Future?

As I mentioned earlier, the Python SDK is based on v4 of the Bot Framework SDK, so it is still in flux, and trails behind the C# and Node.JS SDKs when it comes to completion, but from what I've seen in the source code, it's a great start, and will allow fans of Python to easily build chatbots that can integration with various other Python data science SDKs.