From 382822243a5a26e7109f7128089ac9606e2a4d86 Mon Sep 17 00:00:00 2001 From: Norm Rasmussen Date: Thu, 31 Oct 2024 14:27:20 -0400 Subject: [PATCH] First prototype of a CC script to auto create, reply, and delete a question which will be moved to workato for Docusign. --- Scripts/CC/docusign_auto_questions.py | 104 ++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 Scripts/CC/docusign_auto_questions.py diff --git a/Scripts/CC/docusign_auto_questions.py b/Scripts/CC/docusign_auto_questions.py new file mode 100644 index 00000000..221f72b3 --- /dev/null +++ b/Scripts/CC/docusign_auto_questions.py @@ -0,0 +1,104 @@ +""" +API Endpoints to use: +Open/Close a question: https://api2-eu-west-1.insided.com/docs/community/#tag/Questions/operation/toggleQuestionClosed +Respond to a question: https://api2-eu-west-1.insided.com/docs/community/#tag/Questions/operation/replyQuestion +Delete a question: https://api2-eu-west-1.insided.com/docs/community/#tag/Questions/operation/toggleQuestionTrashedAction + +Posting and Response need to be from different users. Response needs to happen 500 times to trigger the AWS threshold. + +For Kaitlyn's Sandbox: +{"token_type":"bearer","access_token":"R6ltj5LZmUii5jPZ6ruORE6qDP69GlL9","expires_in":7200}% +""" + +import requests + +APIKEY = "R6ltj5LZmUii5jPZ6ruORE6qDP69GlL9" +HEADERS = {"Content-Type": "application/json", "Authorization": f"Bearer {APIKEY}"} +# EU +# BASEURL = "https://api2-eu-west-1.insided.com/v2" +# US +BASEURL = "https://api2-us-west-2.insided.com/v2" + +# My admin user id = userid=21 +# Responder user id = 24 (nrasmussen+insidetest@gainsight.com) + +def get_access_token(): + client_id = "6142ab56-8c2e-473c-bcb0-7b48f2aa8477" + client_secret = "73b04f70280331e1a230c47c20b4fa9a43629d386fa69c6ba411d731aaa2cf55" + token_url = "https://api2-us-west-2.insided.com/oauth2/token" + token_headers = { + "Content-Type": "application/x-www-form-urlencoded" + } + token_load = f"grant_type=client_credentials&client_id={client_id}&client_secret={client_secret}&scope=write" + token_resp = requests.post(token_url, data=token_load, headers=token_headers) + # print(token_resp.text) + # print(token_resp.status_code) + tokenjson = token_resp.json() + access_token = tokenjson['access_token'] + ask_question(access_token) + +def ask_question(access_token): + # /questions/ask" + ask_authid = 21 + askurl = f"{BASEURL}/questions/ask?authorId={ask_authid}" + askload = { + "title": "Test Question", + "content": "I'm trapped please help!", + "categoryId": 38, # Hidden category in Kaitlyn's Sandbox + } + askresp = requests.post(askurl, json=askload, headers=HEADERS) + if askresp.status_code == 201: + question_id = get_question_id() + reply_question(question_id) + else: + print("something went wrong") + print(askresp.text, askresp.status_code) + +def get_question_id(): + # GET List questions for a category + get_quest_url = f"{BASEURL}/categories/38/questions" + get_quest = requests.get(get_quest_url, headers=HEADERS) + queston = get_quest.json() + return queston['result'][0]['id'] + +def reply_question(question_id): + # "/questions/{id}/reply" + rep_authid = 24 + quest_id = question_id + reply_url = f"{BASEURL}/questions/{quest_id}/reply?authorId={rep_authid}" + error_list = [] + for i in range(5): + replyload = { + "content": f"This is response {i}", + } + try: + replyresp = requests.post(reply_url, json=replyload, headers=HEADERS) + if replyresp.raise_for_status(): + print(f"Raised Status occurred on number {i}") + except TabError as exc: + code = exc.response.status_code + error_list.append((code, i)) + + if len(error_list) > 0: + print("Some errors exist. Here's the list:") + print(error_list) + else: + print("No errors.") + delete_question(question_id) + + +def delete_question(question_id): + # /questions/{id}/toggleTrashed" + moderatorid = 21 + delete_url = ( + f"{BASEURL}/questions/{question_id}/toggleTrashed?moderatorId={moderatorid}" + ) + delete_payload = {"trashed": True} + deleteresp = requests.post(delete_url, json=delete_payload, headers=HEADERS) + if deleteresp.status_code == 204: + print("Completed!") + else: + print(deleteresp.status_code) + +if __name__ == "__main__": + get_access_token()