-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathpost_dm_to_conversation.py
113 lines (89 loc) · 4.25 KB
/
post_dm_to_conversation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import base64
import hashlib
import os
import re
import json
import requests
from requests_oauthlib import OAuth2Session
#This example is set up to add a DM to a specified conversation by referencing its ID.
POST_DM_URL = "https://api.twitter.com/2/dm_conversations/:dm_conversation_id/messages"
#-----------------------------------------------------------------------------------------------------------------------
# These variables need to be updated to the setting that match how your Twitter App is set-up at
# https://developer.twitter.com/en/portal/dashboard. These will not change from run-by-run.
client_id = ''
#This must match *exactly* the redirect URL specified in the Developer Portal.
redirect_uri = "https://www.example.com"
#-----------------------------------------------------------------------------------------------------------------------
# These variables indicate the conversation that the message should be added to. A more ready-to-be used example would
# have these passed in from some calling code.
# Provide the ID of the conversation this message should be added to.
dm_conversation_id = "1512210732774948865"
#Set the text of the message to be sent.
text_message = "Hi, I am adding a message to an existing conversation by referencing its ID."
#-----------------------------------------------------------------------------------------------------------------------
def handle_oauth():
# Set the scopes needed to be granted by the authenticating user.
scopes = ["dm.read", "dm.write", "tweet.read", "users.read", "offline.access"]
# Create a code verifier.
code_verifier = base64.urlsafe_b64encode(os.urandom(30)).decode("utf-8")
code_verifier = re.sub("[^a-zA-Z0-9]+", "", code_verifier)
# Create a code challenge.
code_challenge = hashlib.sha256(code_verifier.encode("utf-8")).digest()
code_challenge = base64.urlsafe_b64encode(code_challenge).decode("utf-8")
code_challenge = code_challenge.replace("=", "")
# Start an OAuth 2.0 session.
oauth = OAuth2Session(client_id, redirect_uri=redirect_uri, scope=scopes)
# Create an authorize URL.
auth_url = "https://twitter.com/i/oauth2/authorize"
authorization_url, state = oauth.authorization_url(
auth_url, code_challenge=code_challenge, code_challenge_method="S256"
)
# Visit the URL to authorize your App to make requests on behalf of a user.
print(
"Visit the following URL to authorize your App on behalf of your Twitter handle in a browser:"
)
print(authorization_url)
# Paste in your authorize URL to complete the request.
authorization_response = input(
"Paste in the full URL after you've authorized your App:\n"
)
# Fetch your access token.
token_url = "https://api.twitter.com/2/oauth2/token"
# The following line of code will only work if you are using a type of App that is a public client
auth = False
token = oauth.fetch_token(
token_url=token_url,
authorization_response=authorization_response,
auth=auth,
client_id=client_id,
include_client_id=True,
code_verifier=code_verifier,
)
# The access token.
access = token["access_token"]
return access
def add_dm_to_conversation(dm_text, dm_conversation_id):
request_body = {}
access = handle_oauth()
headers = {
"Authorization": "Bearer {}".format(access),
"Content-Type": "application/json",
"User-Agent": "TwitterDevSampleCode",
"X-TFE-Experiment-environment": "staging1",
"Dtab-Local": "/s/gizmoduck/test-users-temporary => /s/gizmoduck/gizmoduck"
}
request_url = POST_DM_URL.replace(':dm_conversation_id', str(dm_conversation_id))
request_body['text'] = dm_text
json_body = json.dumps(request_body)
#Send DM
response = requests.request("POST", request_url, headers=headers, json=json.loads(json_body))
if response.status_code != 201:
print("Request returned an error: {} {}".format(response.status_code, response.text))
else:
print(f"Response code: {response.status_code}")
return response
def main():
response = add_dm_to_conversation(text_message, dm_conversation_id)
print(json.dumps(json.loads(response.text), indent=4, sort_keys=True))
if __name__ == "__main__":
main()