python-telegram-bot/telegram/updater.py

313 lines
12 KiB
Python
Raw Normal View History

#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2016
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser Public License for more details.
#
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains the class Updater, which tries to make creating
Telegram bots intuitive."""
import logging
import os
2015-11-16 13:05:57 +01:00
import ssl
from threading import Thread, Lock
2015-11-16 20:35:27 +01:00
from time import sleep
2015-11-22 01:03:29 +01:00
import subprocess
2015-11-23 03:45:47 +01:00
from signal import signal, SIGINT, SIGTERM, SIGABRT
from telegram import (Bot, TelegramError, dispatcher, Dispatcher,
2016-01-05 13:32:19 +01:00
NullHandler, JobQueue)
2015-11-16 13:05:57 +01:00
from telegram.utils.webhookhandler import (WebhookServer, WebhookHandler)
# Adjust for differences in Python versions
2015-11-16 13:05:57 +01:00
try:
from Queue import Queue
2015-11-16 13:05:57 +01:00
except ImportError:
from queue import Queue
2015-11-06 00:24:01 +01:00
2015-12-07 23:45:21 +01:00
try:
from urllib2 import URLError
except ImportError:
from urllib.error import URLError
H = NullHandler()
logging.getLogger(__name__).addHandler(H)
2015-11-06 00:24:01 +01:00
class Updater:
"""
This class, which employs the Dispatcher class, provides a frontend to
telegram.Bot to the programmer, so they can focus on coding the bot. It's
purpose is to receive the updates from Telegram and to deliver them to said
dispatcher. It also runs in a separate thread, so the user can interact
with the bot, for example on the command line. The dispatcher supports
handlers for different kinds of data: Updates from Telegram, basic text
commands and even arbitrary types.
The updater can be started as a polling service or, for production, use a
webhook to receive updates. This is achieved using the WebhookServer and
WebhookHandler classes.
2015-11-17 15:57:22 +01:00
2015-11-21 16:04:06 +01:00
Attributes:
2015-11-17 15:57:22 +01:00
Args:
token (Optional[str]): The bot's token given by the @BotFather
base_url (Optional[str]):
workers (Optional[int]): Amount of threads in the thread pool for
functions decorated with @run_async
bot (Optional[Bot]):
Raises:
ValueError: If both `token` and `bot` are passed or none of them.
"""
2015-11-17 15:57:22 +01:00
2016-01-05 13:32:19 +01:00
def __init__(self,
token=None,
base_url=None,
workers=4,
bot=None,
job_queue_tick_interval=1.0):
if (token is None) and (bot is None):
raise ValueError('`token` or `bot` must be passed')
if (token is not None) and (bot is not None):
raise ValueError('`token` and `bot` are mutually exclusive')
if bot is not None:
self.bot = bot
else:
self.bot = Bot(token, base_url)
self.update_queue = Queue()
2016-01-05 13:40:07 +01:00
self.job_queue = JobQueue(self.bot, job_queue_tick_interval)
2015-11-22 14:47:38 +01:00
self.dispatcher = Dispatcher(self.bot, self.update_queue,
workers=workers)
self.last_update_id = 0
self.logger = logging.getLogger(__name__)
self.running = False
2015-11-23 03:45:47 +01:00
self.is_idle = False
2015-11-16 13:05:57 +01:00
self.httpd = None
self.__lock = Lock()
def start_polling(self, poll_interval=0.0, timeout=10, network_delay=2):
"""
2015-11-17 15:57:22 +01:00
Starts polling updates from Telegram.
Args:
2015-11-17 15:57:22 +01:00
poll_interval (Optional[float]): Time to wait between polling
updates from Telegram in seconds. Default is 0.0
timeout (Optional[float]): Passed to Bot.getUpdates
network_delay (Optional[float]): Passed to Bot.getUpdates
Returns:
Queue: The update queue that can be filled from the main thread
"""
with self.__lock:
if not self.running:
self.running = True
# Create Thread objects
dispatcher_thread = Thread(target=self.dispatcher.start,
name="dispatcher")
updater_thread = Thread(target=self._start_polling,
2015-12-31 15:03:40 +01:00
name="updater",
args=(poll_interval,
timeout,
network_delay))
2015-11-17 15:57:22 +01:00
# Start threads
dispatcher_thread.start()
updater_thread.start()
2015-11-17 15:57:22 +01:00
# Return the update queue so the main thread can insert updates
return self.update_queue
def start_webhook(self,
listen='127.0.0.1',
port=80,
url_path='',
cert=None,
key=None):
2015-11-16 13:05:57 +01:00
"""
Starts a small http server to listen for updates via webhook. If cert
and key are not provided, the webhook will be started directly on
http://listen:port/url_path, so SSL can be handled by another
application. Else, the webhook will be started on
https://listen:port/url_path
2015-11-16 13:05:57 +01:00
Args:
listen (Optional[str]): IP-Address to listen on
port (Optional[int]): Port the bot should be listening on
url_path (Optional[str]): Path inside url
cert (Optional[str]): Path to the SSL certificate file
key (Optional[str]): Path to the SSL key file
2015-11-16 13:05:57 +01:00
Returns:
Queue: The update queue that can be filled from the main thread
"""
with self.__lock:
if not self.running:
self.running = True
2015-11-16 13:05:57 +01:00
# Create Thread objects
dispatcher_thread = Thread(target=self.dispatcher.start,
name="dispatcher")
updater_thread = Thread(target=self._start_webhook,
2015-12-31 15:03:40 +01:00
name="updater",
args=(listen,
port,
url_path,
cert,
key))
2015-11-16 13:05:57 +01:00
# Start threads
dispatcher_thread.start()
updater_thread.start()
2015-11-16 13:05:57 +01:00
# Return the update queue so the main thread can insert updates
return self.update_queue
2015-11-16 13:05:57 +01:00
def _start_polling(self, poll_interval, timeout, network_delay):
"""
Thread target of thread 'updater'. Runs in background, pulls
updates from Telegram and inserts them in the update queue of the
2015-11-22 14:47:38 +01:00
Dispatcher.
"""
current_interval = poll_interval
self.logger.info('Updater thread started')
2015-11-16 13:05:57 +01:00
# Remove webhook
self.bot.setWebhook(webhook_url=None)
while self.running:
try:
updates = self.bot.getUpdates(self.last_update_id,
timeout=timeout,
network_delay=network_delay)
if not self.running:
if len(updates) > 0:
2015-11-16 20:43:35 +01:00
self.logger.info('Updates ignored and will be pulled '
'again on restart.')
break
for update in updates:
self.update_queue.put(update)
self.last_update_id = update.update_id + 1
current_interval = poll_interval
2015-11-17 15:57:22 +01:00
2015-11-05 16:01:08 +01:00
sleep(current_interval)
except TelegramError as te:
2015-11-22 14:47:38 +01:00
# Put the error into the update queue and let the Dispatcher
# broadcast it
self.update_queue.put(te)
sleep(current_interval)
2015-12-07 23:45:21 +01:00
except URLError as e:
self.logger.error("Error while getting Updates: %s" % e)
# increase waiting times on subsequent errors up to 30secs
if current_interval == 0:
current_interval = 1
elif current_interval < 30:
current_interval += current_interval / 2
elif current_interval > 30:
current_interval = 30
self.logger.info('Updater thread stopped')
def _start_webhook(self, listen, port, url_path, cert, key):
self.logger.info('Updater thread started')
use_ssl = cert is not None and key is not None
url_path = "/%s" % url_path
2015-11-16 13:05:57 +01:00
# Create and start server
2015-11-16 13:05:57 +01:00
self.httpd = WebhookServer((listen, port), WebhookHandler,
self.update_queue, url_path)
if use_ssl:
# Check SSL-Certificate with openssl, if possible
2015-11-22 01:03:29 +01:00
try:
exit_code = subprocess.call(["openssl", "x509", "-text",
"-noout", "-in", cert],
stdout=open(os.devnull, 'wb'),
stderr=subprocess.STDOUT)
except OSError:
exit_code = 0
if exit_code is 0:
try:
self.httpd.socket = ssl.wrap_socket(self.httpd.socket,
certfile=cert,
keyfile=key,
server_side=True)
except ssl.SSLError as error:
raise TelegramError(str(error))
else:
raise TelegramError('SSL Certificate invalid')
self.httpd.serve_forever(poll_interval=1)
self.logger.info('Updater thread stopped')
2015-11-16 13:05:57 +01:00
def stop(self):
"""
2016-01-05 13:32:19 +01:00
Stops the polling/webhook thread, the dispatcher and the job queue
"""
2015-11-21 21:21:09 +01:00
2016-01-05 13:32:19 +01:00
self.job_queue.stop()
with self.__lock:
if self.running:
self.running = False
self.logger.info('Stopping Updater and Dispatcher...')
self.logger.debug('This might take a long time if you set a '
'high value as polling timeout.')
if self.httpd:
self.logger.info(
'Waiting for current webhook connection to be '
'closed... Send a Telegram message to the bot to exit '
'immediately.')
self.httpd.shutdown()
self.httpd = None
self.logger.debug("Requesting Dispatcher to stop...")
self.dispatcher.stop()
while dispatcher.running_async > 0:
sleep(1)
self.logger.debug("Dispatcher stopped.")
2015-11-23 03:45:47 +01:00
def signal_handler(self, signum, frame):
self.is_idle = False
self.stop()
2015-11-23 17:40:39 +01:00
def idle(self, stop_signals=(SIGINT, SIGTERM, SIGABRT)):
"""
2016-01-04 01:32:47 +01:00
Blocks until one of the signals are received and stops the updater
2015-11-23 17:40:39 +01:00
Args:
stop_signals: Iterable containing signals from the signal module
that should be subscribed to. Updater.stop() will be called on
2015-12-16 16:18:48 +01:00
receiving one of those signals. Defaults to (SIGINT, SIGTERM,
SIGABRT)
2015-11-23 17:40:39 +01:00
"""
for sig in stop_signals:
signal(sig, self.signal_handler)
2015-11-23 03:45:47 +01:00
self.is_idle = True
while self.is_idle:
sleep(1)