Can i use anything like telegram of mailgun or anything else in stead of twillio???
you can use telegram
1 Like
can you please explain the code to me
import requests # for making HTTP requests
import json # library for handling JSON data
import time # module for sleep operation
from boltiot import Bolt # importing Bolt from boltiot module
import tele_conf # config file
mybolt = Bolt(tele_conf.bolt_api_key, tele_conf.device_id)
def get_sensor_value_from_pin(pin):
"""Returns the sensor value. Returns -999 if request fails"""
try:
response = mybolt.analogRead(pin)
data = json.loads(response)
if data["success"] != 1:
print("Request not successfull")
print("This is the response->", data)
return -999
sensor_value = int(data["value"])
return sensor_value
except Exception as e:
print("Something went wrong when returning the sensor value")
print(e)
return -999
def send_telegram_message(message):
"""Sends message via Telegram"""
url = "https://api.telegram.org/" + tele_conf.telegram_bot_id + "/sendMessage"
data = {
"chat_id": tele_conf.telegram_chat_id,
"text": message
}
try:
response = requests.request(
"POST",
url,
params=data
)
print("This is the Telegram response")
print(response.text)
telegram_data = json.loads(response.text)
return telegram_data["ok"]
except Exception as e:
print("An error occurred in sending the alert message via Telegram")
print(e)
return False
while True:
# Step 1
sensor_value = get_sensor_value_from_pin("A0")
print("The current sensor value is:", sensor_value)
# Step 2
if sensor_value == -999:
print("Request was unsuccessfull. Skipping.")
time.sleep(10)
continue
# Step 3
if sensor_value >= tele_conf.threshold:
print("Sensor value has exceeded threshold")
message = "Alert! Sensor value has exceeded " + str(tele_conf.threshold) + \
". The current value is " + str(sensor_value)
telegram_status = send_telegram_message(message)
print("This is the Telegram status:", telegram_status)
# Step 4
time.sleep(10)
this code for temperature so you can modify for light on threshold
1 Like
Can we use whatsapp instead??
WhatsApp has no bot feature which will alert via messages
Then using discord is possible?? It has not feature too
Yup, but you need to link your server bot API. Here is a link for creating bots https://medium.com/@ShinobiSystems/how-to-setup-push-notifications-with-discord-bot-api-1c02959c76ff