Connecting SAS Conversation Designer to Telegram

For the SAS Hackathon, Notilyze created a chatbot for camp managers of refugee camps to obtain information from our data and analytics in an accessible way and suitable for a low bandwidth. We used SAS Conversation Designer to create the chatbot. To connect our chatbot to a suitable communication application, Christiaan created a custom integration using Python. Our chatbot is now connected to Telegram.

Read more on how we did this:

The first version of the SAS Conversation Designer, released end of 2020, has a main focus to automate support questions. In our project we want to use this product as an interface for camp managers, to provide SAS analytics in places where bandwidth is restricted to 2G. SAS provided a sample on how you could connect the SAS Conversation Designer to a web app, but this would require someone to load in a webpage which would be bigger in size than only the text from the messages themself. This is why we chose to use Telegram as our bots interface.

First of all to make the Telegram bot work, we need a connector to the SAS environment. Since the SAS Conversation Designer is quite new there is not a lot of documentation on how to do that, except the previous mentioned sample, but its use case is limited to the web app. We created a Python library which can be used to connect to SAS Conversation Designer. The main advantage of this library is that this solution is easily transferable to another platform next to Telegram, e.g. WhatsApp or Discord. This is because Python is a very generic programming language that is being used and supported by a lot of applications. Most platforms have an API that works in Python, making this library useful for all of these platforms.

We made the library in an Object Oriented style. You only have to import it and then make an instance with the following variables: SAS environment URL, Bot ID, and Bot revision ID. If the revision ID is left empty it will automatically use the latest published version of the bot. Once the instance is created it will ask you to give it a OAuth code and provide a link to get it. Once given it will get the authorization token from SAS and the connection is established. If the auth token of a previous session is still valid, it will use that one.

[Pro tip: At this point the best way on getting a bot ID is by using the network capture feature in the development tools in any browser and look at the requests that get made when talking in the “Try Now” chat, but once the connection is made it works like a charm]

In the script that uses the connector we would recommend to have at least two threads. One will poll all events that get sent from Telegram and the other would be all the events that gets polled from the SAS side. This way, if one of the sides gets disconnected the bot will still keep polling on the other side and could be restarted. The advantage of this design, is that all events that were not processed up to that point will get processed when the connection to Telegram or SAS will get reestablished.