Unable to get output on telegram


unable to output on telegram

Are you using the telegram_chat_id(beginning with@) of your own created bot in the conf.py file?
Also, please attach the send_telegram_message() implementation.

1 Like

yes i had used my own chat id

Did you give the bot admin privileges? Seems like an issue of unauthorized access.

what is it and how to do it?

Open your Telegram channel, open channel info and click on Administrators.
Search your bot by its name and add it to your channel. Give it the rights(all except add new admin).

same problem with me
Ans I have given the bot the admin privilege.
Yet still I couldn’t get message on telegram

Hi @kittupriyatham,

Please share your code and screenshot of the output to to resolve the issue.

Do let me know in case you need any other information.

1 Like

yes just a while
I need to open the virtualbox

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)

Hi @kittupriyatham,

Your telegram_chat_id details are incorrect. Please check this thread Telegram chat id

Your telegram chat id will be -

telegram_chat_id = "@tempAlert"

Do let me know in case you need any other information.

Hi @rahul.singh1
Yesterday I got correct output on the terminal but didn’t get telegram message.

  But today I am not getting even the correct output on terminal which I got yesterday.

Neither the telegram message

@kittupriyatham,

Update the telegram_chat_id and send me the screenshot of the code and the output.

@rahul.singh1
Thank you very much

sir can you please guide me …i am not able to get alert on telegram though i feel my code is right !
here are the pictures of my code
bolt1|498x500

@rolikagupta2001
Can you share the output of the code?

have you been using correct api? check for your api in bolt iot website first. if its correct check if you achieved the day limit if you are not pro member. otherwise send me your code ill resolve.

@mahadikvedant2017
Just go to telegram and type : get id on search bar . There will be a bot channel.
Open it and type: /start
This will give you a chat id in form of digits. Use this as : telegram_chat_id = “xxxxxxxx”.

It will work . Thankyou

you can also obtain chat ID using this url :
https://api.telegram.org/bot/getUpdates
here you will receive chat ID after you send a message to you bot or when you add the bot to a group or channel