-1

I need my bot to monitor my raspberry cpu temperature. It checks it every minute and then send an alert if > a threshold. When a message is sent I need it to not send it again for 10 minutes. I've done it but then I get a timeout error when sending the same message 10 minutes after. Can anybody help me? I did not find any help on telepot giyhub page.

This is my code

bot = telepot.Bot(TOKEN)
bot.message_loop(handle)

while 1:
  if ((get_cpu_temperature() > 30.0) and alarm()):
        data = "Temperature: " + str(get_cpu_temperature()) + " 'C"
        bot.sendMessage(users[0],data)
  time.sleep(60)

The alarm function just checks if 10 mins are passed.

This is the error:

Traceback (most recent call last):
  File "temp_disk_check_live.py", line 74, in <module>
    bot.sendMessage(users[0],data)
  File "/usr/local/lib/python2.7/dist-packages/telepot/__init__.py", line 456, in sendMessage
    return self._api_request('sendMessage', _rectify(p))
  File "/usr/local/lib/python2.7/dist-packages/telepot/__init__.py", line 434, in _api_request
    return api.request((self._token, method, params, files), **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/telepot/api.py", line 130, in request
    r = fn(*args, **kwargs)  # `fn` must be thread-safe
  File "/home/pi/.local/lib/python2.7/site-packages/urllib3/request.py", line 148, in request_encode_body
    return self.urlopen(method, url, **extra_kw)
  File "/home/pi/.local/lib/python2.7/site-packages/urllib3/poolmanager.py", line 321, in urlopen
    response = conn.urlopen(method, u.request_uri, **kw)
  File "/home/pi/.local/lib/python2.7/site-packages/urllib3/connectionpool.py", line 639, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/home/pi/.local/lib/python2.7/site-packages/urllib3/util/retry.py", line 357, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "/home/pi/.local/lib/python2.7/site-packages/urllib3/connectionpool.py", line 601, in urlopen
    chunked=chunked)
  File "/home/pi/.local/lib/python2.7/site-packages/urllib3/connectionpool.py", line 389, in _make_request
    self._raise_timeout(err=e, url=url, timeout_value=read_timeout)
  File "/home/pi/.local/lib/python2.7/site-packages/urllib3/connectionpool.py", line 320, in _raise_timeout
    raise ReadTimeoutError(self, url, "Read timed out. (read timeout=%s)" % timeout_value)
urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='api.telegram.org', port=443): Read timed out. (read timeout=30)
Exception in thread Thread-1 (most likely raised during interpreter shutdown):
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
  File "/usr/local/lib/python2.7/dist-packages/telepot/__init__.py", line 391, in run
  File "/usr/local/lib/python2.7/dist-packages/telepot/__init__.py", line 310, in k
  File "/usr/lib/python2.7/threading.py", line 168, in acquire
<type 'exceptions.TypeError'>: 'NoneType' object is not callable

the handle function is the standard one from telepot examples.

Thanks a lot

ScazzoMatto
  • 99
  • 1
  • 4
  • 12

2 Answers2

0

You could create a new thread and start a timer like..

def hello():
    print("hello, world")

t = Timer(30.0, hello)
t.start()  # after 30 seconds, "hello, world" will be printed

So in your code;

def send_message(user,message):
    bot.sendMessage(user,message)

t = Timer(600, send_message("Temperature...", user[0])
if cpu_temp > 30: t.start()
elbaridne
  • 13
  • 2
0

How about initialize Bot whenever you really need to send a message?

while 1:
  if ((get_cpu_temperature() > 30.0) and alarm()):
        data = "Temperature: " + str(get_cpu_temperature()) + " 'C"
        telepot.Bot(TOKEN).sendMessage(users[0],data)
  time.sleep(60*10) # 10 min
handicop
  • 892
  • 12
  • 25