Raspberry Pi Zero W Wireless Projects by Vasilis Tzivaras

Raspberry Pi Zero W Wireless Projects by Vasilis Tzivaras

Author:Vasilis Tzivaras
Language: eng
Format: epub, azw3, pdf
Publisher: Packt Publishing
Published: 2017-08-24T06:17:31+00:00


At this point, you have learned how to create a notification with slack API calls. It is pretty easy to combine our previous code with the temperature sensor and the new code with the slack API, and send a notification when the temperature is over 25 degrees Celsius.

Advanced client

The client here is actually the application in our mobile device or our desktop computer. For the sake of simplicity and for this project, the client will not send any messages back to the server. The client will only receive notifications if the temperature goes over 25 degrees Celsius. However, the advanced client can be developed by adding two functions. The actual function that sends a message:

def send_message(channel_id, message):

slack_client.api_call(

"chat.postMessage",

channel=channel_id,

text=message,

username='pythonbot',

icon_emoji=':robot_face:'

)

In the previous code, you choose the channel in which you want to send the message and of course the main function:

if __name__ == '__main__':

channels = list_channels()

if channels:

print("Channels: ")

for channel in channels:

print(channel['name'] + " (" + channel['id'] + ")")

detailed_info = channel_info(channel['id'])

if detailed_info:

print('Latest text from ' + channel['name'] + ":")

print(detailed_info['latest']['text'])

if channel['name'] == 'general':

send_message(channel['id'], "Hello " +

channel['name'] + "! It worked!")

print('-----')

else:

print("Unable to authenticate.")

We will not analyze this code, but you can try it out to send messages back to the server; for example, you can simply get a notification and send back an ok message.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.