Telegram not send sms

I have entered the code correectly. But it is showing error and skipping. Attaching the image. Kindly solve the issue.

Hii @ykoffice6920
Inside of your def get_sensor_value_from_pin(pin) you have assigned the variable response to your Bolt reading but you have passed it as requests in the next line.
Also you have not imported json module. Also you would have to import requests
to make a HTTP request to the Telegram servers using the URL.

Hope this helps! :smiley:

I done that. But it still shows like that. What to do

@ykoffice6920
in the def send_telegram_message(message) function you have typed telegram as te;egram.
Hope it clears the error :smiley:

@ykoffice6920
could you please share me the whole coding file as well as output for further assistance.

“”“Configurations for telegram_alert.py”""
bolt_api_key = “XXXX” # This is your Bolt Cloud API Key
device_id = “XXXX” # This is the device ID and will be similar to BOLTXXXX where XXXX is some numbers
telegram_chat_id = “@XXXX” # This is the channel ID of the created Telegram channel. Paste after @
telegram_bot_id = “botXXXX” # This is the bot ID of the created Telegram Bot. Paste after bot
threshold = 250 # Threshold beyond which the alert should be sent

first save this file as conf.py

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 conf                     # config file

mybolt = Bolt(conf.bolt_api_key, 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/" + conf.telegram_bot_id + "/sendMessage"
    data = {
        "chat_id": conf.telegram_chat_id,
        "text": message
    }
    try:
        response = requests.request(
            "POST",
            url,
            params=data
        )
        print("This is the Telegram URL")
        print(url)
        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 >= conf.threshold:
        print("Sensor value has exceeded threshold")
        message = "Alert! Sensor value has exceeded " + str(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)

Then execute this code ull get the result

Actually You have made few tiny mistakes which is causing heavy effects.
On the First Mark see,
You have passed requests as the parameter forjson.loads which is wrong. The thing which must be passed is response. and code will look something like this.

data = json.loads(response)

and in the second mark, you have miss spelled telegram, the actual right will look like,

url = “https://api.telegram.org/” + conf.telegram_bot_id + “/sendMessage”

and Hope you have also used these line in your code.

> try:
>         response = requests.request(
>             "POST",
>             url,
>             params=data
>         )
>         print("This is the Telegram URL")
>         print(url)
>         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")
>     Temperature=(100*sensor_value)/1024
>     print("The current Temperature is:", Temperature, "*C")
>     
>     # Step 2
>     if Temperature == -999:
>         print("Request was unsuccessfull. Skipping.")
>         time.sleep(10)
>         continue
>     
>     # Step 3
>     if Temperature <= conf.threshold:
>         print("Temperature has exceeded threshold")
>         message = "Alert! Temperature value has exceeded " + str(conf.threshold) + \
>                   ". The current value is " + str(Temperature)
>         telegram_status = send_telegram_message(message)
>         print("This is the Telegram status:", telegram_status)
> 
>     # Step 4
>     time.sleep(10)

Hmmmm, I see you guys maybe are into bulk SMS verification
I recommend using the platform sms-man.com as they have many numbers available for services like WhatsApp, Telegram etc

They also have an API if you’re interested