How to run codes for telegram

what codes we have to right for Sending Telegram message when temperature crosses threshold in VM

@satishkumarcse1995 in order to send a telegram message, whenever the threshold for the temperature increases, can be achieved by sending the details in a JSON object.

Whenever the current temperature value increases from the threshold value then we basically create a message which has to displayed in the telegram chat. The code for the same is:

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)

Now in order to pass the same message over telegram we have to create data object which will be passed to the telegram API and thus our message will be displayed in the Telegram Chat. In the above code, you can see that after defining the message we are calling a function named send_telegram_message which will take a message parameter. The layout of the function is as follows:

In order to send a message over Telegram we have to use this route for the API: in which the bot id is the id which is being assigned to the bot made by you. As you may be aware of the fact that one bot can be linked to many chats, so the above mentioned url is not correct as it will not identify the chat where the message has to be deployed.

Now we know which API route we have to hit in order to send a message. But as in the url: we haven’t attached the message which we have to send and also haven’t specified the chat id. In order to do this part, we have defined an object named data in which we have specified the chat_id & the message.

We have used the requests module of python to process the HTTP request to Telegram. As we have to send the API request to the Telegram’s server we have used the POST method of data transfer. Along woth it we have specified the URL (API Route) and then have defined the other parameters by the help of data object.

So at last the API request will look like this:
Assume bot id is: A7894, chat_id is: 1245, Threshold value is 20 and current value is 23! Sensor value has exceeded 20. The current value is 23

{The parameters are basically appended at the end of the query after the ? and the various parameters are seperated by the & symbol}

You can find the complete code in the training’s content

I hope I have answered your query :slight_smile: If its solved then do click on the Solution button on my post so that the query can be closed

@satishkumarcse1995 if your query is solved then do close the query by marking my initial post as Solution

sorry bt i dont understand.
i have written codes as teached in that lecture so after that what i have to do

So is the code not running or what’s the problem?

that my question actually how i can run code

Brother, as you have yourself said that you have gone through the course 's lecture of this section then I am not able to get how can’t you run the code?

As through this project you are aiming at sending telegram messages whenever the current value crosses the threshold. So, I request you to go through the Section 11: Social Media of Things. In total you have to follow these steps

  1. Create a Telegram channel
  2. Create a Telegram Bot
  3. Write the code in the Ubuntu

Note: If you are saying that you don’t know how to run the code. You have to run the following code on Putty or VirtualBox. If you are on this section then you must know how to run the code.

If you have provided a name say then after writing the complete code in that file you just have to write the command sudo python3 and then the processing will start