Telepot - Telegram bot每10分钟发送一次消息

时间:2018-05-22 13:14:21

标签: python telegram telepot

我需要我的机器人来监控我的覆盆子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

句柄功能是来自电波示例的标准功能。

非常感谢

2 个答案:

答案 0 :(得分: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()

答案 1 :(得分: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
相关问题