N
N
Nikita Vezhlivtsev2021-07-04 19:30:48
Telegram
Nikita Vezhlivtsev, 2021-07-04 19:30:48

How to send a Telegram message to a bot (in python), after running a successful ping command?

I am learning python and making a simple telegram bot. Essence of the question: when the ping command is successfully executed, which is launched on my PC using python (sends 4 packets), a notification should come to the telegram bot. How can this be written? Something like the code below, only now it writes to me when I press the start button, I would like it to be just a message.
import telnetlib
import os
import telebot
host = "hostname"
ping = os.system("ping -c 1 " + host)
bot = telebot.TeleBot(TOKEN)
@bot.message_handler(content_types=['text'])
def get_message (message):
if ping == 0:
bot.send_message(message.chat.id, text = "PING")
bot.polling(none_stop=True)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2021-07-04
@shabelski89

Telebot libraries are not required for this task.
You need:
1) a token
2) python requests
3) your code and logic
4) send a message via http post
An example of such a bot is here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question