Sending a message to microsoft teams bot using python

%3CLINGO-SUB%20id%3D%22lingo-sub-1049114%22%20slang%3D%22en-US%22%3ESending%20a%20message%20to%20microsoft%20teams%20bot%20using%20python%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1049114%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20created%20a%20bot%20and%20subscribed%20it%20to%20Microsoft%20Teams%2C%20I%20have%20done%20all%20the%20configuration%20settings%20such%20that%20I%20am%20getting%20all%20the%20messages%20to%20my%20web-app%20from%20MSTeams.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CP%3EFollowing%20to%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fbot-service%2Frest-api%2Fbot-framework-rest-connector-send-and-receive-messages%3Fview%3Dazure-bot-service-4.0%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3Ethis%26nbsp%3B%3C%2FA%3Edocumentation%2C%20I%20am%20trying%20to%20hit%20an%20Http%20post%20request%20to%20send%20the%20reply%20message%20to%20the%20same%20bot.%20But%20I%20am%20getting%20401%20i.e%20unauthorized%20error.%20Maybe%20I%20am%20not%20getting%20the%20correct%20token.%20could%20someone%20help%20me%20telling%20how%20can%20we%20generate%20the%20token%20in%20order%20to%20do%20an%20Http%20request%20to%20the%20MSTeams%3F%3C%2FP%3E%3CP%3Ebelow%20is%20the%20code%20snippet%20that%20I%20am%20using.%3C%2FP%3E%3CPRE%3Eteam_message%20%3D%20json.loads(request.data.decode(%3CSPAN%3E%22utf-8%22%3C%2FSPAN%3E))%3CBR%20%2F%3Econversion_id%20%3D%20team_message.get(%3CSPAN%3E%22conversation%22%3C%2FSPAN%3E).get(%3CSPAN%3E%22id%22%3C%2FSPAN%3E)%3CBR%20%2F%3Eactivity_id%20%3D%20team_message.get(%3CSPAN%3E%22channelData%22%3C%2FSPAN%3E).get(%3CSPAN%3E%22clientActivityID%22%3C%2FSPAN%3E)%3CBR%20%2F%3Eservice_url%20%3D%20team_message.get(%3CSPAN%3E%22serviceUrl%22%3C%2FSPAN%3E)%3CBR%20%2F%3Eurl%20%3D%20service_url%20%2B%20%3CSPAN%3E%22%2Fv3%2Fconversations%2F%7B%7D%2Factivities%2F%7B%7D%22%3C%2FSPAN%3E.format(conversion_id%2C%20activity_id)%3CBR%20%2F%3Eres%20%3D%20requests.post(%3CSPAN%3Eurl%3C%2FSPAN%3E%3Durl%2C%20headers%3D%7B%22%3CSPAN%3EAuthorization%3C%2FSPAN%3E%22%3A%20%22%3CSPAN%3EBearer%20abcdfjkdsa%3C%2FSPAN%3E%22)%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1049114%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EChat%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMicrosoft%20Teams%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1055288%22%20slang%3D%22en-US%22%3ERe%3A%20Sending%20a%20message%20to%20microsoft%20teams%20bot%20using%20python%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1055288%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F478570%22%20target%3D%22_blank%22%3E%40barun69%3C%2FA%3E%26nbsp%3B%2C%3C%2FP%3E%0A%3CP%3ECan%20you%20please%20share%20how%20did%20you%20generate%20the%20Bearer%20Token%3F%20It%20has%20to%20be%20a%20valid%20access%20token.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Visitor

I have created a bot and subscribed it to Microsoft Teams, I have done all the configuration settings such that I am getting all the messages to my web-app from MSTeams.

Following to this documentation, I am trying to hit an Http post request to send the reply message to the same bot. But I am getting 401 i.e unauthorized error. Maybe I am not getting the correct token. could someone help me telling how can we generate the token in order to do an Http request to the MSTeams?

below is the code snippet that I am using.

team_message = json.loads(request.data.decode("utf-8"))
conversion_id = team_message.get("conversation").get("id")
activity_id = team_message.get("channelData").get("clientActivityID")
service_url = team_message.get("serviceUrl")
url = service_url + "/v3/conversations/{}/activities/{}".format(conversion_id, activity_id)
res = requests.post(url=url, headers={"Authorization": "Bearer abcdfjkdsa")

 

1 Reply

@barun69 ,

Can you please share how did you generate the Bearer Token? It has to be a valid access token.