Skip to content

Instantly share code, notes, and snippets.

@tzmfreedom
Created September 13, 2014 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tzmfreedom/8abf0dd93dea601d29ef to your computer and use it in GitHub Desktop.
Save tzmfreedom/8abf0dd93dea601d29ef to your computer and use it in GitHub Desktop.
import serial
import urllib.request
import json
CLIENT_ID = "input your client_id"
CLIENT_SECRET = "input your client_secret"
#chatterにポストする
def postFeedItem(val):
try:
chatter_param = {
"body" : {
"messageSegments": [{
"type":"Text",
"text": "value = " + str(val)
}]
}
}
chatter_headers = {
"Content-Type": "application/json",
"Authorization": "Bearer " + token["access_token"]
}
chatter_req = urllib.request.Request(
url=token["instance_url"] + "/services/data/v30.0/chatter/feeds/news/me/feed-items",
data=json.dumps(chatter_param).encode(),
headers=chatter_headers
)
chatter_res = urllib.request.urlopen(chatter_req)
print(chatter_res.read().decode())
except urllib.error.HTTPError as e:
print(e.read())
#access_token等を取得
request_param = {
"grant_type" : "password",
"client_id" : CLIENT_ID,
"client_secret" : CLIENT_SECRET,
"username" : "input your username",
"password" : "input your password"
}
headers = {
"Content-Type": "application/x-www-form-urlencoded"
}
req = urllib.request.Request(
url="https://login.salesforce.com/services/oauth2/token",
data=urllib.parse.urlencode(request_param).encode(),
headers=headers
)
res = urllib.request.urlopen(req)
token = json.loads(res.read().decode())
#シリアルで転送されてきた値を読み取って処理を行う
#以下はシリアルポートはCOM3の例
ser = serial.Serial(2)
while True:
line = ser.readline()
val = int(line.decode())
if val < 10:
postFeedItem(val)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment