本文整理汇总了Python中telegram.ext.Updater.stop方法的典型用法代码示例。如果您正苦于以下问题:Python Updater.stop方法的具体用法?Python Updater.stop怎么用?Python Updater.stop使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类telegram.ext.Updater
的用法示例。
在下文中一共展示了Updater.stop方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from telegram.ext import Updater [as 别名]
# 或者: from telegram.ext.Updater import stop [as 别名]
def main():
# Create the EventHandler and pass it your bot's token.
token = 'TOKEN'
updater = Updater(token, workers=10)
# Get the dispatcher to register handlers
dp = updater.dispatcher
# This is how we add handlers for Telegram messages
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("help", help))
# Message handlers only receive updates that don't contain commands
dp.add_handler(MessageHandler([Filters.text], message))
# Regex handlers will receive all updates on which their regex matches,
# but we have to add it in a separate group, since in one group,
# only one handler will be executed
dp.add_handler(RegexHandler('.*', any_message), group=1)
# String handlers work pretty much the same. Note that we have to tell
# the handler to pass the args or update_queue parameter
dp.add_handler(StringCommandHandler('reply', cli_reply, pass_args=True))
dp.add_handler(StringRegexHandler('[^/].*', cli_noncommand,
pass_update_queue=True))
# All TelegramErrors are caught for you and delivered to the error
# handler(s). Other types of Errors are not caught.
dp.add_error_handler(error)
# Start the Bot and store the update Queue, so we can insert updates
update_queue = updater.start_polling(timeout=10)
'''
# Alternatively, run with webhook:
update_queue = updater.start_webhook('0.0.0.0',
443,
url_path=token,
cert='cert.pem',
key='key.key',
webhook_url='https://example.com/%s'
% token)
# Or, if SSL is handled by a reverse proxy, the webhook URL is already set
# and the reverse proxy is configured to deliver directly to port 6000:
update_queue = updater.start_webhook('0.0.0.0', 6000)
'''
# Start CLI-Loop
while True:
text = input()
# Gracefully stop the event handler
if text == 'stop':
updater.stop()
break
# else, put the text into the update queue to be handled by our handlers
elif len(text) > 0:
update_queue.put(text)
示例2: TelegramPoll
# 需要导入模块: from telegram.ext import Updater [as 别名]
# 或者: from telegram.ext.Updater import stop [as 别名]
class TelegramPoll(BaseTelegramBotEntity):
"""Asyncio telegram incoming message handler."""
def __init__(self, bot, hass, allowed_chat_ids):
"""Initialize the polling instance."""
from telegram.ext import Updater
BaseTelegramBotEntity.__init__(self, hass, allowed_chat_ids)
self.updater = Updater(bot=bot, workers=4)
self.dispatcher = self.updater.dispatcher
self.dispatcher.add_handler(message_handler(self.process_update))
self.dispatcher.add_error_handler(process_error)
def start_polling(self):
"""Start the polling task."""
self.updater.start_polling()
def stop_polling(self):
"""Stop the polling task."""
self.updater.stop()
def process_update(self, bot, update):
"""Process incoming message."""
self.process_message(update.to_dict())
示例3: main
# 需要导入模块: from telegram.ext import Updater [as 别名]
# 或者: from telegram.ext.Updater import stop [as 别名]
def main():
global forecast_info
global dp
loadmodules()
forecast_info = load_db()
updater = Updater(TOKEN)
dp = updater.dispatcher
loadcommands()
dp.add_handler(CommandHandler("help", help))
dp.add_handler(CommandHandler("rehash", rehash))
dp.add_handler(CommandHandler("forecast", forecast))
dp.add_handler(CommandHandler("weather", weather))
dp.add_handler(CommandHandler("setlocation", set_local))
dp.add_handler(MessageHandler([Filters.text], stats))
dp.add_error_handler(error)
updater.start_polling(timeout=5)
while True:
text = raw_input()
if text == 'stop':
save_db(forecast_info)
updater.stop()
break
示例4: test_inUpdater
# 需要导入模块: from telegram.ext import Updater [as 别名]
# 或者: from telegram.ext.Updater import stop [as 别名]
def test_inUpdater(self):
u = Updater(bot="MockBot", job_queue_tick_interval=0.005)
u.job_queue.put(self.job1, 0.5)
sleep(0.75)
self.assertEqual(1, self.result)
u.stop()
sleep(2)
self.assertEqual(1, self.result)
示例5: test_inUpdater
# 需要导入模块: from telegram.ext import Updater [as 别名]
# 或者: from telegram.ext.Updater import stop [as 别名]
def test_inUpdater(self):
u = Updater(bot="MockBot")
u.job_queue.put(Job(self.job1, 0.5))
sleep(0.75)
self.assertEqual(1, self.result)
u.stop()
sleep(2)
self.assertEqual(1, self.result)
示例6: main
# 需要导入模块: from telegram.ext import Updater [as 别名]
# 或者: from telegram.ext.Updater import stop [as 别名]
def main():
# Load telegram API stuff
settings = open('config.yml', 'r')
yaml_data = yaml.load(settings)
token = yaml_data['telegram-apikey']
updater = Updater(token, workers=10)
# Load saved data
global groups
if os.path.isfile(DATA_FILENAME):
try:
with open(DATA_FILENAME, 'r') as saved:
data = json.loads(saved.read())
for chat_id in data:
groups[ast.literal_eval(chat_id)] = GroupInfo(chat_id, data[chat_id])
except ValueError:
logging.info("No JSON saved data found")
# Register handlers
dispatcher = updater.dispatcher
dispatcher.add_handler(MessageHandler([Filters.text], message))
dispatcher.add_handler(CommandHandler("start", start))
dispatcher.add_handler(CommandHandler("privacy", privacy))
dispatcher.add_handler(CommandHandler("generate", generate))
dispatcher.add_handler(CommandHandler("clearhistory", clear_history))
updater.start_polling(timeout=10)
logging.info("Bot started!")
# CLI
while True:
try:
text = raw_input()
except NameError:
text = input()
if text == 'stop':
logging.info("Saving all data...")
save_data = {}
for chat_id in groups:
save_data[chat_id] = groups[chat_id].get_data()
with open(DATA_FILENAME, 'w') as save_file:
json.dump(save_data, save_file)
logging.info("Saved, stopping.")
updater.stop()
break
elif text == 'numchans':
logging.info("The bot has data for %d channels" % len(groups))
else:
logging.info("Unknown command")
示例7: main
# 需要导入模块: from telegram.ext import Updater [as 别名]
# 或者: from telegram.ext.Updater import stop [as 别名]
def main():
# Create the Updater and pass it your bot's token.
updater = Updater(TOKEN, workers=2)
# Get the dispatcher to register handlers
dp = updater.dispatcher
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("help", help))
dp.add_handler(CommandHandler('welcome', set_welcome))
dp.add_handler(CommandHandler('goodbye', set_goodbye))
dp.add_handler(CommandHandler('disable_goodbye', disable_goodbye))
dp.add_handler(CommandHandler("lock", lock))
dp.add_handler(CommandHandler("unlock", unlock))
dp.add_handler(CommandHandler("quiet", quiet))
dp.add_handler(CommandHandler("unquiet", unquiet))
dp.add_handler(CommandHandler("playlist", playlist))
dp.add_handler(CallbackQueryHandler(button))
# dp.add_handler(StringRegexHandler('^$', empty_message))
# dp.add_handler(StringCommandHandler('', empty_message))
# dp.add_handler(StringCommandHandler('level', set_log_level))
# dp.add_handler(StringCommandHandler('count', chatcount))
dp.add_error_handler(error)
# Start the Bot and store the update Queue, so we can insert updates
update_queue = updater.start_polling(poll_interval=1, timeout=5)
# Alternatively, run with webhook:
# updater.bot.setWebhook(webhook_url='https://%s/%s' % (BASE_URL, TOKEN))
# Or, if SSL is handled by a reverse proxy, the webhook URL is already set
# and the reverse proxy is configured to deliver directly to port 6000:
# update_queue = updater.start_webhook(HOST, PORT, url_path=TOKEN)
# update_queue = updater.start_webhook(listen=HOST,
# port=PORT,
# url_path=TOKEN,
# key=CERT_KEY,
# cert=CERT,
# webhook_url='https://%s/%s' % (BASE_URL, TOKEN))
# Start CLI-Loop
while True:
text = input()
# Gracefully stop the event handler
if text == 'stop':
updater.stop()
break
# else, put the text into the update queue
elif len(text) > 0:
update_queue.put(text) # Put command into queue
示例8: __init__
# 需要导入模块: from telegram.ext import Updater [as 别名]
# 或者: from telegram.ext.Updater import stop [as 别名]
class ComicbookTelegramBot:
def __init__(self, token):
self.updater = Updater(token)
self.updater.dispatcher.add_handler(CommandHandler('start', handler_start))
self.updater.dispatcher.add_handler(CommandHandler('comic', handler_comic))
def start(self):
self.updater.start_polling()
# self.updater.idle()
def stop(self):
self.updater.stop()
示例9: main
# 需要导入模块: from telegram.ext import Updater [as 别名]
# 或者: from telegram.ext.Updater import stop [as 别名]
def main():
updater = Updater(token=HTTP_TOKEN)
register_handlers(updater)
log.info('beginning polling...')
try:
updater.start_polling(clean=True)
while True:
time.sleep(1)
except KeyboardInterrupt:
log.info('quitting...')
updater.stop()
示例10: __init__
# 需要导入模块: from telegram.ext import Updater [as 别名]
# 或者: from telegram.ext.Updater import stop [as 别名]
class HDBot:
def __init__(self, token, db_url):
# connect to Telegram with the desired token
self.updater = Updater(token=token)
self.bot = self.updater.bot
# configure the bot behavior
dispatcher = self.updater.dispatcher
dispatcher.add_handler(CommandHandler("start", self.send_welcome))
dispatcher.add_handler(MessageHandler([Filters.text], self.filter_tags))
# create a digester backed by the desired database
try:
self.digester = digester.Digester(db_url)
except Exception as e:
self.stop()
raise e
self.db_url = db_url
# dispatcher methods
self.get_config = self.digester.get_config
self.get_chat = self.bot.getChat
@staticmethod
def send_welcome(bot, update):
message = update.message
LOG.info("%s: %s" % (message.from_user.username, message.text))
bot.sendMessage(chat_id=message.chat_id,
text="Hello, I'm a bot who makes digests of messages with hashtags!",
reply_to_message_id=message.message_id)
def filter_tags(self, _, update):
"""Send the message to the digest for processing
A tagged message will be added to it belonged chat digest
"""
message = update.message
if self.digester.feed(message) and LOG.isEnabledFor(logging.DEBUG):
LOG.info("Message from %s: %s", message.from_user.username, message.text)
def start(self):
self.updater.start_polling(clean=True)
LOG.info("Hashtag Digester Bot started")
if LOG.isEnabledFor(logging.DEBUG):
LOG.debug("Bot username: %s", self.bot.getMe().name)
LOG.debug("Digests database: %s", self.db_url)
def stop(self):
self.updater.stop()
# suitable to be used by `contextlib.closing`
def close(self):
self.stop()
示例11: test_in_updater
# 需要导入模块: from telegram.ext import Updater [as 别名]
# 或者: from telegram.ext.Updater import stop [as 别名]
def test_in_updater(self, bot):
u = Updater(bot=bot)
u.job_queue.start()
try:
u.job_queue.run_repeating(self.job_run_once, 0.02)
sleep(0.03)
assert self.result == 1
u.stop()
sleep(1)
assert self.result == 1
finally:
u.stop()
示例12: main
# 需要导入模块: from telegram.ext import Updater [as 别名]
# 或者: from telegram.ext.Updater import stop [as 别名]
def main():
#pass the token
token = config.get("Connection","Token")
group = config.get("Connection","Group")
updater = Updater(token, workers=10)
# Get the dispatcher to register handlers
dp = updater.dispatcher
# add important handlers
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("help", help))
dp.add_handler(MessageHandler([Filters.command], unknown_command))
dp.add_handler(MessageHandler('.*', any_message))
#non-essential handlers
dp.add_handler(CommandHandler('hiashur', hiashur))
dp.add_handler(CommandHandler('norn', norn))
dp.add_handler(CommandHandler('weed', weed))
dp.add_handler(CommandHandler('song', song))
dp.add_handler(CommandHandler('kinkshame', kinkshame))
#CLI handlers
dp.add_handler(InlineQueryHandler('reply', cli_reply))
dp.add_handler(InlineQueryHandler(unknown_cli_command))
dp.add_handler(RegexHandler('[^/].*', cli_noncommand))
#Error handler
dp.add_error_handler(error)
#start bot and start polling
update_queue = updater.start_polling(poll_interval=0.1, timeout=10)
# Start CLI-Loop
while True:
try:
text = raw_input()
except NameError:
text = input()
# Gracefully stop the event handler
if text == 'stop':
updater.stop()
break
# else, put the text into the update queue to be handled by our handlers
elif len(text) > 0:
update_queue.put(text)
示例13: Robot
# 需要导入模块: from telegram.ext import Updater [as 别名]
# 或者: from telegram.ext.Updater import stop [as 别名]
class Robot(object):
def __init__(self):
self.updater = Updater(token=TG_TOKEN)
self.dispatcher = self.updater.dispatcher
# Register WatchController
watch_handler = CommandHandler(
"watch", controllers.WatchController.watch)
unwatch_handler = CommandHandler(
"unwatch", controllers.WatchController.unwatch)
watchlist_handler = CommandHandler(
"watchlist", controllers.WatchController.watchlist)
updates_handler = CommandHandler(
"update", controllers.WatchController.updates)
up_handler = CommandHandler(
"up", controllers.WatchController.updates)
self.dispatcher.add_handler(watch_handler)
self.dispatcher.add_handler(unwatch_handler)
self.dispatcher.add_handler(watchlist_handler)
self.dispatcher.add_handler(updates_handler)
self.dispatcher.add_handler(up_handler)
# Register SearchController
search_handler = MessageHandler(
[Filters.text], controllers.SearchController.search)
self.dispatcher.add_handler(search_handler)
# Register CityController
city_handler = CommandHandler("city", controllers.CityController.city)
citylist_handler = CommandHandler("citylist", controllers.CityController.citylist)
self.dispatcher.add_handler(city_handler)
self.dispatcher.add_handler(citylist_handler)
# Register StartController
start_handler = CommandHandler("start", controllers.StartController.start)
self.dispatcher.add_handler(start_handler)
restart_handler = CommandHandler("restart", controllers.StartController.restart)
self.dispatcher.add_handler(restart_handler)
help_handler = CommandHandler("help", controllers.StartController.help)
self.dispatcher.add_handler(help_handler)
self.updater.start_polling()
def stop(self):
self.updater.stop()
示例14: Telegram
# 需要导入模块: from telegram.ext import Updater [as 别名]
# 或者: from telegram.ext.Updater import stop [as 别名]
class Telegram(object):
def __init__(self, token=None):
self.token = token or default_token
self.updater = None
self.bot = None
def update_bot(self):
self.updater = Updater(token=self.token)
self.bot = telegram.Bot(token=self.token)
self.updater.start_polling()
self.bot.getMe()
self.bot.getUpdates()
def telegram_article(self, status):
self.update_bot()
# chat_id = bot.getUpdates()[-1].message.chat_id # add this string to update all telegram users
chat_id = default_user
self.bot.sendMessage(chat_id=chat_id, text=status)
self.updater.stop()
示例15: run_bot_service
# 需要导入模块: from telegram.ext import Updater [as 别名]
# 或者: from telegram.ext.Updater import stop [as 别名]
def run_bot_service():
token = os.environ['BOT_TOKEN']
updater = Updater(token, workers=10)
updater.dispatcher.add_handler(get_update_handler())
updater.dispatcher.add_handler(get_forward_handler())
updater.dispatcher.add_handler(get_help_handler())
if CERTIFICATE_PATH:
webhook_path = generate_random_string(length=20)
webhook_uri = '/' + webhook_path
set_webhook(updater, webhook_uri)
update_queue = updater.start_webhook('0.0.0.0', 5000, webhook_path)
else:
update_queue = updater.start_polling(poll_interval=0.1, timeout=10)
running = True
while running:
try:
time.sleep(20000)
except KeyboardInterrupt:
running = False
updater.stop()