For backup
This commit is contained in:
parent
7ba3841a67
commit
dbb93bfd4d
1 changed files with 51 additions and 19 deletions
70
tbr.py
70
tbr.py
|
|
@ -23,11 +23,13 @@
|
||||||
#
|
#
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import pprint
|
import logging
|
||||||
from pprint import pformat
|
from pprint import pformat
|
||||||
|
from multiprocessing import Process
|
||||||
|
|
||||||
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
|
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
|
||||||
import logging
|
from telegram.error import (TelegramError, Unauthorized, BadRequest,
|
||||||
|
TimedOut, ChatMigrated, NetworkError)
|
||||||
|
|
||||||
# Enable logging
|
# Enable logging
|
||||||
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
||||||
|
|
@ -36,6 +38,7 @@ logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Define a few command handlers. These usually take the two arguments bot and
|
# Define a few command handlers. These usually take the two arguments bot and
|
||||||
# update. Error handlers also receive the raised TelegramError object in error.
|
# update. Error handlers also receive the raised TelegramError object in error.
|
||||||
def start(bot, update):
|
def start(bot, update):
|
||||||
|
|
@ -54,8 +57,27 @@ def echo(bot, update):
|
||||||
update.message.reply_text(update.message.text)
|
update.message.reply_text(update.message.text)
|
||||||
|
|
||||||
|
|
||||||
def error(bot, update, error):
|
def error_callback(bot, update, error):
|
||||||
logger.warn('Update "%s" caused error "%s"' % (update, error))
|
try:
|
||||||
|
raise error
|
||||||
|
except Unauthorized:
|
||||||
|
# remove update.message.chat_id from conversation list
|
||||||
|
logger.warning(error.msg)
|
||||||
|
except BadRequest:
|
||||||
|
# handle malformed requests - read more below!
|
||||||
|
logger.warning(error.msg)
|
||||||
|
except TimedOut:
|
||||||
|
# handle slow connection problems
|
||||||
|
logger.warning('TimedOut occurred')
|
||||||
|
except NetworkError:
|
||||||
|
# handle other connection problems
|
||||||
|
logger.warning(error.msg)
|
||||||
|
except ChatMigrated as e:
|
||||||
|
# the chat_id of a group has changed, use e.new_chat_id instead
|
||||||
|
logger.warning(error.msg)
|
||||||
|
except TelegramError:
|
||||||
|
# handle all other telegram related errors
|
||||||
|
logger.warning(error.msg)
|
||||||
|
|
||||||
|
|
||||||
routingdict = dict()
|
routingdict = dict()
|
||||||
|
|
@ -72,6 +94,22 @@ def print_routes(bot, update):
|
||||||
update.message.reply_text(pformat(routingdict))
|
update.message.reply_text(pformat(routingdict))
|
||||||
|
|
||||||
|
|
||||||
|
def input_handler(fd):
|
||||||
|
stream = open(fd)
|
||||||
|
for line in stream: # CTRL+D, eli EOF, lopettaa
|
||||||
|
print('Line: {}'.format(line), end='')
|
||||||
|
parsed = json.loads(line)
|
||||||
|
source_id = parsed.get('source_id', '')
|
||||||
|
for chat_id in routingdict.get(source_id, {}):
|
||||||
|
if source_id == 'pandacraft':
|
||||||
|
if parsed['verb'] == 'joined':
|
||||||
|
updater.bot.send_message(chat_id=chat_id, text='Pandacraft: {} liittyi peliin'.format(parsed['player']))
|
||||||
|
else:
|
||||||
|
updater.bot.send_message(chat_id=chat_id, text='Pandacraft: {} poistui'.format(parsed['player']))
|
||||||
|
else:
|
||||||
|
updater.bot.send_message(chat_id=chat_id, text=line)
|
||||||
|
|
||||||
|
|
||||||
def main(token_file):
|
def main(token_file):
|
||||||
TG_TOKEN = open(token_file).read()
|
TG_TOKEN = open(token_file).read()
|
||||||
|
|
||||||
|
|
@ -94,29 +132,23 @@ def main(token_file):
|
||||||
dp.add_handler(MessageHandler(Filters.text, echo))
|
dp.add_handler(MessageHandler(Filters.text, echo))
|
||||||
|
|
||||||
# log all errors
|
# log all errors
|
||||||
dp.add_error_handler(error)
|
dp.add_error_handler(error_callback)
|
||||||
|
|
||||||
# Start the Bot
|
# Start the Bot
|
||||||
updater.start_polling()
|
updater.start_polling()
|
||||||
|
|
||||||
# PA-PA
|
# Start input handler
|
||||||
for line in sys.stdin: # CTRL+D, eli EOF, lopettaa
|
fd = sys.stdin.fileno() # = 0
|
||||||
print('Line: {}'.format(line))
|
ih = Process(target=input_handler, args=(fd,))
|
||||||
parsed = json.loads(line)
|
ih.start()
|
||||||
source_id = parsed.get('source_id', '')
|
|
||||||
for chat_id in routingdict.get(source_id, {}):
|
print('Input handler started.')
|
||||||
if source_id == 'pandacraft':
|
ih.join()
|
||||||
if parsed['verb'] == 'joined':
|
|
||||||
updater.bot.send_message(chat_id=chat_id, text='Pandacraft: {} liittyi peliin'.format(parsed['player']))
|
|
||||||
else:
|
|
||||||
updater.bot.send_message(chat_id=chat_id, text='Pandacraft: {} poistui'.format(parsed['player']))
|
|
||||||
else:
|
|
||||||
updater.bot.send_message(chat_id=chat_id, text=line)
|
|
||||||
|
|
||||||
# Run the bot until you press Ctrl-C or the process receives SIGINT,
|
# Run the bot until you press Ctrl-C or the process receives SIGINT,
|
||||||
# SIGTERM or SIGABRT. This should be used most of the time, since
|
# SIGTERM or SIGABRT. This should be used most of the time, since
|
||||||
# start_polling() is non-blocking and will stop the bot gracefully.
|
# start_polling() is non-blocking and will stop the bot gracefully.
|
||||||
print('Entering idle...')
|
print('Entering idle.')
|
||||||
updater.idle()
|
updater.idle()
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue