The basic tools you need to prepare are:
1. Create MS Application. You can create by the link https://dev.botframework.com/bots/provision
After registration you should keep 2 parameters:
APP_ID = ‘your-application-id’
APP_PASSWORD = ‘your-application-password’
2. MS Bot Framework SDK. https://dev.botframework.com/ Our tutorial in based on Python environment, so you need to import Python MS Bot Framework SDK https://github.com/microsoft/botbuilder-python
#!/usr/bin/python
import ssl
from aiohttp import web
import asyncio
import aiohttp
from botbuilder.core import (BotFrameworkAdapter, BotFrameworkAdapterSettings,
TurnContext, MemoryStorage)
from botbuilder.schema import (Attachment, Activity, ActivityTypes)
APP_ID = '' #1 add your APP ID
APP_PASSWORD = '' #2 add you APP Password
SSL_CONTEXT = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
SSL_CONTEXT.load_cert_chain('', '') #3 add your SSL certificates
PORT = 8000
SETTINGS = BotFrameworkAdapterSettings(APP_ID, APP_PASSWORD)
ADAPTER = BotFrameworkAdapter(SETTINGS)
async def create_reply_activity(request_activity: Activity, text: str, attachment: Attachment = None) -> Activity:
activity = Activity(
type=ActivityTypes.message,
channel_id=request_activity.channel_id,
conversation=request_activity.conversation,
recipient=request_activity.from_property,
from_property=request_activity.recipient,
attachment_layout='carousel',
text=text,
service_url=request_activity.service_url)
if attachment:
activity.attachments = attachment
return activity
async def handle_conversation_update(context: TurnContext) -> web.Response:
if context.activity.members_added[0].id != context.activity.recipient.id:
response = await create_reply_activity(context.activity, "Hello world!")
await context.send_activity(response)
return web.Response(status=200)
async def unhandled_activity() -> web.Response:
return web.Response(status=404)
async def api_connect(text):
url = 'https://api.nlsql.com/sap' #4 add your API url here
headers = {'Authorization': 'Token API KEY', 'Content-Type': 'application/json'} #5 add your API Key
payload = {'message': text}
async with aiohttp.ClientSession() as session:
r = await session.post(url, headers=headers, json=payload)
data = await r.json()
return data
# MAIN function for bot
async def request_handler(context: TurnContext) -> web.Response:
if context.activity.type == 'message':
try:
data = await api_connect(context.activity.text)
# Make connection to own internal database and take data using received sql
# Build separate logic for different API reply message types, if you need it.
# data_type = data["data_type"]
# if data_type == 'graph':
# ...
# elif data_type == 'message':
# ...
# elif data_type == 'buttons':
# ...
# elif data_type == 'error':
# ...
# etc.
data_type = data["data_type"]
if data_type == 'ytd':
result = data['sql']['sql1'] + ' \n' + data['sql']['sql2']
else:
result = data['sql']
response = await create_reply_activity(context.activity, result)
await context.send_activity(response)
except KeyError:
pass
elif context.activity.type == 'conversationUpdate':
return await handle_conversation_update(context)
elif context.activity.action == 'add':
return await handle_conversation_update(context)
else:
return await unhandled_activity()
async def messages(request) -> web.Response:
body = await request.json()
# print("request: ", request.headers)
# print("body: ", body)
activity = Activity().deserialize(body)
auth_header = request.headers['Authorization'] if 'Authorization' in request.headers else ''
web.Response(status=200)
try:
return await ADAPTER.process_activity(activity, auth_header, request_handler)
except Exception as exc:
raise exc
async def init_app(loop):
app = web.Application(loop=loop)
app.add_routes([web.post('/api/messages', messages)])
return app
try:
loop = asyncio.get_event_loop()
app = loop.run_until_complete(init_app(loop))
web.run_app(app, host='127.0.0.1', port=PORT)
except Exception as e:
raise e