-
Notifications
You must be signed in to change notification settings - Fork 22
/
gcp_function.py
35 lines (25 loc) · 1 KB
/
gcp_function.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
import requests
from requests_oauthlib import OAuth1
import os
consumer_key = os.environ.get("CONSUMER_KEY")
consumer_secret = os.environ.get("CONSUMER_SECRET")
access_token = os.environ.get("ACCESS_TOKEN")
access_token_secret = os.environ.get("ACCESS_TOKEN_SECRET")
def random_fact():
fact = requests.get("https://catfact.ninja/fact?max_length=280").json()
return fact["fact"]
def format_fact(fact):
return {"text": "{}".format(fact)}
def connect_to_oauth(consumer_key, consumer_secret, acccess_token, access_token_secret):
url = "https://api.twitter.com/2/tweets"
auth = OAuth1(consumer_key, consumer_secret, acccess_token, access_token_secret)
return url, auth
def hello_pubsub(event, context):
fact = random_fact()
payload = format_fact(fact)
url, auth = connect_to_oauth(
consumer_key, consumer_secret, access_token, access_token_secret
)
request = requests.post(
auth=auth, url=url, json=payload, headers={"Content-Type": "application/json"}
)