0

我需要我的机器人来监控我的覆盆子 CPU 温度。它每分钟检查一次,然后在 > 阈值时发送警报。发送消息时,我需要它在 10 分钟内不再发送。我已经完成了,但是在 10 分钟后发送相同的消息时出现超时错误。有谁能够帮我?我在 Telepot giyhub 页面上没有找到任何帮助。

这是我的代码

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)

警报功能仅检查是否经过了 10 分钟。

这是错误:

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

handle 函数是 Telepot 示例中的标准函数。

非常感谢

4

2 回答 2

0

当你真的需要发送消息时,如何初始化 Bot?

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
于 2019-05-10T08:52:30.993 回答
0

您可以创建一个新线程并启动一个计时器,例如..

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

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

所以在你的代码中;

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

t = Timer(600, send_message("Temperature...", user[0])
if cpu_temp > 30: t.start()
于 2018-05-22T13:26:15.933 回答