· 4 years ago · Mar 17, 2021, 10:56 AM
1# <!!!REPLACEME with code for libraries>
2import datetime
3import requests
4import json
5import urllib.parse
6# 2. Complete the if statement to ask the user for the Webex access token.
7choice = input("Do you wish to use the hard-coded Webex token? (y/n) ")
8
9#<!!!REPLACEME with if statements to ask user for the Webex Teams Access Token!!!>
10if choice == 'n':
11 print("you must press y")
12else:
13 accessToken = "Bearer YWFmNTMxNmQtOGJlNi00ZGY0LTgyZWMtZjc3ZDU5YzUxOWFmMDVhMmUxMGItNmY5_P0A1_e0796e43-9341-40de-87ea-bfc92d9028a9"
14
15# 3. Provide the URL to the Webex Teams room API.
16r = requests.get( url ="https://webexapis.com/v1/rooms",
17 headers = {"Authorization": accessToken}
18 )
19
20#####################################################################################
21# DO NOT EDIT ANY BLOCKS WITH r.status_code
22if not r.status_code == 200:
23 raise Exception("Incorrect reply from Webex Teams API. Status code: {}. Text: {}".format(r.status_code, r.text))
24######################################################################################
25
26# 4.
27.
28print("List of rooms:")
29rooms = r.json()["items"]
30for room in rooms:
31 #<!!!REPLACEME with print code to finish the loop>
32 print("room",room['type'],room['title'])
33
34#######################################################################################
35# SEARCH FOR WEBEX TEAMS ROOM TO MONITOR
36# - Searches for user-supplied room name.
37# - If found, print "found" message, else prints error.
38# - Stores values for later use by bot.
39# DO NOT EDIT CODE IN THIS BLOCK
40#######################################################################################
41
42while True:
43 roomNameToSearch = input("Which room should be monitored for /location messages? ")
44 roomIdToGetMessages = None
45
46 for room in rooms:
47 if(room["title"].find(roomNameToSearch) != -1):
48 print ("Found rooms with the word " + roomNameToSearch)
49 print(room["title"])
50 roomIdToGetMessages = room["id"]
51 roomTitleToGetMessages = room["title"]
52 print("Found room : " + roomTitleToGetMessages)
53 break
54
55 if(roomIdToGetMessages == None):
56 print("Sorry, I didn't find any room with " + roomNameToSearch + " in it.")
57 print("Please try again...")
58 else:
59 break
60
61######################################################################################
62# WEBEX TEAMS BOT CODE
63# Starts Webex bot to listen for and respond to /location messages.
64######################################################################################
65
66while True:
67 time.sleep(1)
68 GetParameters = {
69 "roomId": roomIdToGetMessages,
70 "max": 1
71 }
72# 5. Provide the URL to the Webex Teams messages API.
73 r = requests.get("https://webexapis.com/v1/messages",
74 params = GetParameters,
75 headers = {"Authorization": accessToken}
76 )
77
78 if not r.status_code == 200:
79 raise Exception( "Incorrect reply from Webex Teams API. Status code: {}. Text: {}".format(r.status_code, r.text))
80
81 json_data = r.json()
82 if len(json_data["items"]) == 0:
83 raise Exception("There are no messages in the room.")
84
85 messages = json_data["items"]
86 message = messages[0]["text"]
87 print("Received message: " + message)
88
89 if message.find("/") == 0:
90 location = message[1:]
91# 6. Provide your MapQuest API consumer key.
92 mapsAPIGetParameters = {
93 "location": location,
94 "key": "05V0qvGgfnAAtlPUGHe2KdhggWK5NgYr"
95 }
96# 7. Provide the URL to the MapQuest address API.
97 r = requests.get("https://www.mapquestapi.com/directions/v2/route?",
98 params = mapsAPIGetParameters
99 )
100 json_data = r.json()
101
102 if not json_data["info"]["statuscode"] == 0:
103 raise Exception("Incorrect reply from MapQuest API. Status code: {}".format(r.statuscode))
104
105 locationResults = json_data["results"][0]["providedLocation"]["location"]
106 print("Location: " + locationResults)
107
108# 8. Provide the MapQuest key values for latitude and longitude.
109 locationLat = json_data['items'][0]['position']['lat']#["<!!!REPLACEME!!!> with path to latitude key!!!>"]
110 locationLng = json_data['items'][0]['position']['lon'] #["<!!!REPLACEME!!!> with path to longitude key!!!>"]
111 print("Location GPS coordinates: " + str(locationLat) + ", " + str(locationLng))
112
113 issAPIGetParameters = {
114 "lat": locationLat,
115 "lon": locationLng
116 }
117# 9. Provide the URL to the ISS pass times API.
118 r = requests.get("http://api.open-notify.org/iss-pass.json?lat=LAT&lon=LON",
119 params = issAPIGetParameters
120 )
121
122 json_data = r.json()
123
124 if not "response" in json_data:
125 raise Exception("Incorrect reply from open-notify.org API. Status code: {}. Text: {}".format(r.status_code, r.text))
126
127# 10. Provide the ISS key values risetime and duration.
128 risetimeInEpochSeconds = json_data["<!!!REPLACEME!!!> with path to risetime key!!!>"]
129 durationInSeconds = json_data["<!!!REPLACEME!!!> with path to duration key!!!>"]
130
131# 11. Convert the risetime epoch value to a human readable date and time.
132 risetimeInFormattedString = <!!!REPLACEME with conversion code!!!>
133
134# 12. Complete the code to format the response message.
135# Example responseMessage result: In Austin, Texas the ISS will fly over on Thu Jun 18 18:42:36 2020 for 242 seconds.
136 responseMessage = "In {} the ISS will fly over on {} for {} seconds.".format(<!!!REPLACEME with required variables!!!>)
137
138 print("Sending to Webex Teams: " +responseMessage)
139