本文整理汇总了Python中telegram.Bot.setWebhook方法的典型用法代码示例。如果您正苦于以下问题:Python Bot.setWebhook方法的具体用法?Python Bot.setWebhook怎么用?Python Bot.setWebhook使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类telegram.Bot
的用法示例。
在下文中一共展示了Bot.setWebhook方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: token
# 需要导入模块: from telegram import Bot [as 别名]
# 或者: from telegram.Bot import setWebhook [as 别名]
#.........这里部分代码省略.........
Telegram servers before actually starting the webhook. Default
is False.
Returns:
Queue: The update queue that can be filled from the main thread
"""
with self.__lock:
if not self.running:
self.running = True
if clean:
self._clean_updates()
# Create & start threads
self._init_thread(self.dispatcher.start, "dispatcher"),
self._init_thread(self._start_webhook, "updater", listen,
port, url_path, cert, key)
# Return the update queue so the main thread can insert updates
return self.update_queue
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
Dispatcher.
"""
cur_interval = poll_interval
self.logger.info('Updater thread started')
# 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)
except TelegramError as te:
self.logger.error(
"Error while getting Updates: {0}".format(te))
# Put the error into the update queue and let the Dispatcher
# broadcast it
self.update_queue.put(te)
cur_interval = self._increase_poll_interval(cur_interval)
else:
if not self.running:
if len(updates) > 0:
self.logger.info('Updates ignored and will be pulled '
'again on restart.')
break
if updates:
for update in updates:
self.update_queue.put(update)
self.last_update_id = updates[-1].update_id + 1
cur_interval = poll_interval
sleep(cur_interval)
@staticmethod
示例2: token
# 需要导入模块: from telegram import Bot [as 别名]
# 或者: from telegram.Bot import setWebhook [as 别名]
#.........这里部分代码省略.........
key (Optional[str]): Path to the SSL key file
Returns:
Queue: The update queue that can be filled from the main thread
"""
# Create Thread objects
dispatcher_thread = Thread(target=self.dispatcher.start,
name="dispatcher")
event_handler_thread = Thread(target=self._start_webhook,
name="updater",
args=(listen, port, url_path, cert, key))
self.running = True
# Start threads
dispatcher_thread.start()
event_handler_thread.start()
# Return the update queue so the main thread can insert updates
return self.update_queue
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
Dispatcher.
"""
current_interval = poll_interval
self.logger.info('Updater thread started')
# 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:
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
sleep(current_interval)
except TelegramError as te:
# Put the error into the update queue and let the Dispatcher
# broadcast it
self.update_queue.put(te)
sleep(current_interval)
# increase waiting times on subsequent errors up to 30secs
if current_interval < 30:
current_interval += current_interval / 2
if current_interval > 30:
current_interval = 30
self.logger.info('Updater thread stopped')
示例3: Gallery
# 需要导入模块: from telegram import Bot [as 别名]
# 或者: from telegram.Bot import setWebhook [as 别名]
gallery = Gallery().search(tgid = update.message.chat.id)
if gallery:
gallery.delete()
text = 'Gallery deleted'
else:
text = 'Gallery is not registered'
# TODO: Confirm
elif args[0] == '/config':
args.pop(0)
gallery = Gallery.search(tgid = update.message.chat.id)
if gallery:
if len(args) == 0:
text = g.config(update.message.chat.id)
elif len(args) == 1:
text = 'get one'
text = g.config(update.message.chat.id, args[0])
else:
text = g.config(update.message.chat.id, args[0], args[1])
else:
text = 'Gallery is not registered'
#else:
# text = update.to_json()
if text:
bot.sendMessage(update.message.chat.id, text, disable_web_page_preview=True)
return ""
if __name__ == '__main__':
bot = Bot(app.config.get('TOKEN'))
bot.setWebhook('https://%s/%s' % (app.config.get('WEBHOOK_HOST'), app.config.get('WEBHOOK_ROUTE')))
app.run(host='0.0.0.0')
示例4: Updater
# 需要导入模块: from telegram import Bot [as 别名]
# 或者: from telegram.Bot import setWebhook [as 别名]
#.........这里部分代码省略.........
self.httpd.serve_forever(poll_interval=1)
def _check_ssl_cert(self, cert, key):
# Check SSL-Certificate with openssl, if possible
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:
self.logger.exception('Failed to init SSL socket')
raise TelegramError(str(error))
else:
raise TelegramError('SSL Certificate invalid')
@staticmethod
def _gen_webhook_url(listen, port, url_path):
return 'https://{listen}:{port}{path}'.format(listen=listen, port=port, path=url_path)
def _bootstrap(self, max_retries, clean, webhook_url, cert=None):
retries = 0
while 1:
try:
if clean:
# Disable webhook for cleaning
self.bot.setWebhook(webhook_url='')
self._clean_updates()
sleep(1)
self.bot.setWebhook(webhook_url=webhook_url, certificate=cert)
except (Unauthorized, InvalidToken):
raise
except TelegramError:
msg = 'error in bootstrap phase; try={0} max_retries={1}'.format(retries,
max_retries)
if max_retries < 0 or retries < max_retries:
self.logger.warning(msg)
retries += 1
else:
self.logger.exception(msg)
raise
else:
break
sleep(1)
def _clean_updates(self):
self.logger.debug('Cleaning updates from Telegram server')
updates = self.bot.getUpdates()
while updates:
updates = self.bot.getUpdates(updates[-1].update_id + 1)
def stop(self):
"""
Stops the polling/webhook thread, the dispatcher and the job queue
"""
self.job_queue.stop()
with self.__lock:
示例5: Bot
# 需要导入模块: from telegram import Bot [as 别名]
# 或者: from telegram.Bot import setWebhook [as 别名]
from bot import app
from telegram import Bot
from plugin import Plugin
if __name__ == "__main__":
commands = [instance.name for plugin,instance in Plugin.plugins.items() if instance.command]
print commands
bot = Bot(app.config.get('TOKEN'))
bot.setWebhook(app.config.get('WEBHOOK'))
app.run(debug=True, port=80, host="0.0.0.0")