updating initial slackBot

This commit is contained in:
2019-10-14 01:31:18 +01:00
parent e86e8652d3
commit ae9df823a4
6 changed files with 72 additions and 46 deletions

View File

@@ -3,9 +3,11 @@ import os
import glob
import sys
from datetime import datetime
import requests
import base64
sys.path.append(os.getcwd())
import pullTrafficInfo
import http.client
inst = (
pullTrafficInfo.getTrafficInfo.getTrafficURL('M62')
@@ -70,40 +72,71 @@ if accidentMessage is None:
accidentMessage = data[accidentMessageStart:accidentMessageEnd]
accidentMessage = ''.join(accidentMessage)
print(f'messageTitle: {messageTitle}' + '\n')
print(f'congestionTitle: {congestionTitle}' + '\n')
print(f'congestionMessage: {congestionMessage}' + '\n')
print(f'accidentTitle: {accidentTitle}' + '\n')
print(f'accidentMessage: {accidentMessage}' + '\n')
print(f'end: {end}')
# print(f'messageTitle: {messageTitle}' + '\n')
# print(f'congestionTitle: {congestionTitle}' + '\n')
# print(f'congestionMessage: {congestionMessage}' + '\n')
# print(f'accidentTitle: {accidentTitle}' + '\n')
# print(f'accidentMessage: {accidentMessage}' + '\n')
# print(f'end: {end}')
conn = http.client.HTTPConnection("hooks,slack,com")
payload = """{\"attachments\": [\n {\n \"title\": \"",\n \"pretext\": \"Did someone say M62!? \\ud83d\\ude27\",\n \"text\": \"\",\n \"mrkdwn_in\": [\"text\", \"pretext\"],\n \"footer\": \"ec2-54-246-210-90.eu-west-1.compute.amazonaws.com\",\n \"footer_icon\": \"https://i.imgur.com/ADsI87O.png\",\n \"ts\": \"1570917914\",\n \"fields\": [\n\t\t \t{\n\t\t \"title\": \"\",\n\t\t \"value\": \"\",\n\t\t \"short\": false\n\t\t \t},\n\t\t \t{\n\t\t \"title\": \"\",\n\t\t \"value\": \"\",\n\t\t \"short\": false\n\t\t \t},\n\t \t\t{\n\t\t \"title\": \"\",\n\t\t \"value\": \" \",\n\t\t \"short\": false\n\t\t \t}\n\t \t]\n }\n ]\n}"""
payload = """{\"attachments\": [\n {\n \"title\": \"\",\n \"pretext\": \"Did someone say M62!? \\ud83d\\ude27\",\n \"text\": \"\",\n \"mrkdwn_in\": [\"text\", \"pretext\"],\n \"footer\": \"ec2-54-246-210-90.eu-west-1.compute.amazonaws.com\",\n \"footer_icon\": \"https://i.imgur.com/ADsI87O.png\",\n \"ts\": \"1570917914\",\n \"fields\": [\n\t\t \t{\n\t\t \"title\": \"\",\n\t\t \"value\": \"\",\n\t\t \"short\": false\n\t\t \t},\n\t\t \t{\n\t\t \"title\": \"\",\n\t\t \"value\": \"\",\n\t\t \"short\": false\n\t\t \t},\n\t \t\t{\n\t\t \"title\": \"\",\n\t\t \"value\": \" \",\n\t\t \"short\": false\n\t\t \t}\n\t \t]\n }\n ]\n}"""
jsonPayload = json.loads(payload)
timeNow = datetime.now().timestamp()
jsonPayload = json.loads(payload)
print(jsonPayload)
jsonPayload['attachments'][0]['title'] = f'{messageTitle}'
print(jsonPayload['attachments'][0]['title'])
# print(jsonPayload['attachments'][0]['title'])
jsonPayload['attachments'][0]['ts'] = timeNow
print(jsonPayload['attachments'][0]['ts'])
# print(jsonPayload['attachments'][0]['ts'])
jsonPayload['attachments'][0]['fields'][0]['title'] = f'{congestionTitle}'
print(jsonPayload['attachments'][0]['fields'][0]['title'])
# print(jsonPayload['attachments'][0]['fields'][0]['title'])
jsonPayload['attachments'][0]['fields'][0]['value'] = f'{congestionMessage}'
print(jsonPayload['attachments'][0]['fields'][0]['value'])
# print(jsonPayload['attachments'][0]['fields'][0]['value'])
jsonPayload['attachments'][0]['fields'][1]['title'] = f'{accidentTitle}'
print(jsonPayload['attachments'][0]['fields'][1]['title'])
# print(jsonPayload['attachments'][0]['fields'][1]['title'])
jsonPayload['attachments'][0]['fields'][1]['value'] = f'{accidentMessage}'
print(jsonPayload['attachments'][0]['fields'][1]['value'])
# print(jsonPayload['attachments'][0]['fields'][1]['value'])
jsonPayload['attachments'][0]['fields'][2]['value'] = f'{end}'
print(jsonPayload['attachments'][0]['fields'][2]['value'])
# print(jsonPayload['attachments'][0]['fields'][2]['value'])
newJsonPayload = json.dumps(jsonPayload)
# print(newJsonPayload)
with open(f'{os.getcwd()}/slackURL.json', 'r') as slackURL:
url = json.load(slackURL)
url = base64.b64decode(url.encode()).decode()
querystring = {"": ""}
headers = {
'Content-Type': 'application/json',
'User-Agent': 'PostmanRuntime/7.17.1',
'Accept': '*/*',
'Cache-Control': 'no-cache',
'Postman-Token': 'a4b63f2d-43b1-4ce7-898a-2fd03952ad7d,46ba7deb-472c-4328'
'-a261-9c2000da2f11',
'Host': 'hooks.slack.com',
'Accept-Encoding': 'gzip, deflate',
'Content-Length': '1277',
'Connection': 'keep-alive',
'cache-control': 'no-cache',
}
response = requests.request(
'POST',
url,
data=newJsonPayload.encode(),
headers=headers,
params=querystring,
)
print(response.text)