2

I want to write a python bot and I know if it is possible to connect my bot to microsoft bot connector ?

Nima
  • 490
  • 2
  • 10
  • 19
  • 1
    You could have a look at the library I have been writing to connect to the Microsoft Bot Connector API. I couldn't find anything really simple when I was looking to write a bot for Microsoft Teams so I started my own. https://github.com/Grungnie/microsoftbotframework – Matthew Brown May 03 '17 at 14:06

1 Answers1

7

Yes it's possible. Please checkout Microsoft bot built on Django (python web framework) for implementation.

Here below is a python code to reply back to Microsoft bot connector

import requests
app_client_id = `<Microsoft App ID>`
app_client_secret = `<Microsoft App Secret>`
def sendMessage(serviceUrl,channelId,replyToId,fromData, recipientData,message,messageType,conversation):
    url="https://login.microsoftonline.com/common/oauth2/v2.0/token"
    data = {"grant_type":"client_credentials",
        "client_id":app_client_id,
        "client_secret":app_client_secret,
        "scope":"https://graph.microsoft.com/.default"
       }
    response = requests.post(url,data)
    resData = response.json()
    responseURL = serviceUrl + "v3/conversations/%s/activities/%s" % (conversation["id"],replyToId)
    chatresponse = requests.post(
                       responseURL,
                       json={
                        "type": messageType,
                        "timestamp": datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f%zZ"),
                        "from": fromData,
                        "conversation": conversation,
                        "recipient": recipientData,
                        "text": message,
                        "replyToId": replyToId
                       },
                       headers={
                           "Authorization":"%s %s" % (resData["token_type"],resData["access_token"])
                       }
                    )

In the above example please replace <Microsoft App ID> and <Microsoft App Secret> with appropriate App ID and App secret. for more API checkout Microsoft Bot Connector REST API - v3.0

Mr. A
  • 1,221
  • 18
  • 28
  • Any idea if this can be used to connect to the emulator? (not the external microsoft bot framework) – ShreyasG Oct 05 '17 at 11:18