-
-
Save diegoparrilla/8082885 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import uuid | |
import jwt | |
import requests | |
import re | |
# config | |
SHARED = 'YOUR SHARED KEY' | |
SUB = 'YOURSUBDOMAIN' | |
TOPIC_ID = 'YOURTOPICID' # the id for "question or feature suggestion, found in the frontend e.g. 200001701 | |
ENDPOINT = 'https://' + SUB + '.zendesk.com/access/jwt?jwt=' | |
def encode_jwt(content): | |
blob = { | |
"iat" : int(time.time()), | |
"jti" : str(uuid.uuid1()), | |
"name" : content['name'], | |
"email" : content['email'] | |
} | |
return jwt.encode(blob, SHARED) | |
def get_token(name, email): | |
''' | |
Get session data for authenticated user | |
''' | |
output = encode_jwt({'name' : name, 'email' : email}) | |
cookies = dict(cookies_are='working') | |
conn = requests.get(ENDPOINT + output + '&return_to=https://' + SUB + '.zendesk.com/hc/communities/public/questions/new', cookies=cookies) | |
token = re.compile(r'<meta content="(.*)" name="csrf-token"').search(conn.text).group(1) | |
if token: | |
return {'token':token,'cookie':conn.cookies} | |
return | |
def create_question(user_data, content): | |
''' | |
Create a new Help Centre question | |
''' | |
endpoint = 'https://' + SUB + '.zendesk.com/hc/communities/public/questions' | |
data = {'utf8': '✓', | |
'authenticity_token': user_data['token'], | |
'question[title]' : content['title'], | |
'question[details]' : content['details'], | |
'question[topic_ids]' : TOPIC_ID | |
} | |
conn = requests.post(endpoint, data=data, headers=header, cookies=user_data['cookie']) | |
out = re.compile(r'question-title"><a href="(.*)">').search(conn.text).group(1) | |
return out | |
def add_comment(user_data, content, official=0): | |
''' | |
Add a new comment to a HC question | |
''' | |
endpoint = 'https://' + SUB + '.zendesk.com' + content['question'] + '/answers' | |
data = {'utf8': '✓', | |
'authenticity_token': user_data['token'], | |
'answer[body]' : content['message'], | |
'question[official]' : official | |
} | |
conn = requests.post(endpoint, data=data, headers=header, cookies=user_data['cookie']) | |
return conn | |
if __name__ == "__main__": | |
content = {'title' : 'Testing automated post', | |
'details' : 'This is a test, seems to be working'} | |
a = get_token('User 1', '[email protected]') | |
b = create_question(a, content) | |
c = get_token('User 2', '[email protected]') | |
d = add_comment(c, {'message' : 'Yeah, it does seem to work', 'question' : b}) | |
e = get_token('Staff 1', '[email protected]') | |
f = add_comment(c, {'message' : 'This is the official answer, yes.', 'question' : b}, 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment