users: 123456789: keyboard: - # , - buttonText: 'Global Temperatures' # , reportName: 'Global Temperatures' # report: '/views/Regional/GlobalTemperatures' # , - buttonText: 'College Admissions' # , reportName: 'College Admissions' # report: '/views/Regional/College' - # , - buttonText: 'Regional FlightDelays' # , reportName: 'Regional FlightDelays' # report: '/views/Regional/FlightDelays'
def makeCustomKeyboard( userKeyboard ): """ . keyboard privacy.yaml. """ custom_keyboard = map( lambda z: [ telegram.KeyboardButton(x['buttonText']) for x in z ], userKeyboard ) return telegram.ReplyKeyboardMarkup(custom_keyboard, resize_keyboard='True')
def updateSettings(): """ """ return load( file( CONFIG_FILE ) )
config: # botToken: '111222333:AAO4L0Ttt1tt01-1TtT1t_t22tT' # tabcmd.exe tabcmdLocation: 'C:\Program Files\Tableau\Tableau Server\10.1\bin\' # reportsLocation: 'C:\Users\Administrator\Documents\bot\reports\' # logsLocation: 'C:\Users\Administrator\Documents\bot\logs\' admin: # chat id id: 198765432
def start(bot, update): """ /start . ( privacy.yaml, users). , . """ # chat_id chat_id = update.message.chat_id # config = updateSettings() # if chat_id in config['users']: logs(update, 'Received /start command from user in privacy.yaml') # reply_markup = makeCustomKeyboard(config['users'][int(chat_id)]['keyboard']) bot.sendMessage(chat_id=chat_id, text=', ', reply_markup=reply_markup) logs(update, 'List of available reports sent') # else: logs(update, 'Received /start command from user NOT IN privacy.yaml') # bot.sendMessage(chat_id=chat_id, text=', . ' + \ ' , ') # u = ast.literal_eval(str(update)) bot.sendMessage(chat_id=config['admin']['id'], text=' ' + str(u['message']['from']['username'].encode('utf-8')) + ' ' + str(u['message']['from']['first_name'].encode('utf-8')) + ' ' + str(u['message']['from']['last_name'].encode('utf-8')) + ' ' + str(chat_id)) logs(update, 'Request for access sent')
bot.sendMessage(chat_id=chat_id, text=' , ...', reply_markup=reply_markup) executeFile = os.path.join( tabcmdLocation, 'tabcmd.exe' ) subprocess.call([ executeFile, 'login', '-s', 'localhost', '-u', 'admin', '-p', 'adminPassword '])
subprocess.call([ executeFile, 'get', reportAddress, '-f', reportsLocation + reportName + '.pdf'])
subprocess.call([ executeFile, 'logout'])
bot.send_document(chat_id=chat_id, document=open(reportsLocation + reportName.encode('utf-8') + '.pdf', 'rb'))
users: 123456789: keyboard: - # , - buttonText: 'Global Temperatures' # , reportName: 'Global Temperatures' # report: '/views/Regional/GlobalTemperatures' # , - buttonText: 'College Admissions' # , reportName: 'College Admissions' # report: '/views/Regional/College' - # , - buttonText: 'Regional FlightDelays' # , reportName: 'Regional FlightDelays' # report: '/views/Regional/FlightDelays' config: # botToken: '111222333:AAO4L0Ttt1tt01-1TtT1t_t22tT' # tabcmd.exe tabcmdLocation: 'C:\Program Files\Tableau\Tableau Server\10.1\bin\' # reportsLocation: 'C:\Users\Administrator\Documents\bot\reports\' # logsLocation: 'C:\Users\Administrator\Documents\bot\logs\' admin: # chat id id: 198765432
# -*- coding: utf-8 -*- CONFIG_FILE = r'C:\Users\Administrator\Documents\bot\privacy.yaml' # API import telegram from telegram.ext import Updater from telegram.ext import MessageHandler, Filters from telegram.ext import CommandHandler import sys import subprocess from yaml import load import ast import os.path def updateSettings(): """ """ return load( file( CONFIG_FILE ) ) # config = updateSettings() # token = config['config']['botToken'] # tabcmd.exe tabcmdLocation = config['config']['tabcmdLocation'] # reportsLocation = config['config']['reportsLocation'] # logsLocation = config['config']['logsLocation'] def makeCustomKeyboard( userKeyboard ): """ . keyboard privacy.yaml. """ custom_keyboard = map( lambda z: [ telegram.KeyboardButton(x['buttonText']) for x in z ], userKeyboard ) return telegram.ReplyKeyboardMarkup(custom_keyboard, resize_keyboard='True') def logs(update, comment): """ . comment logs , chat id . """ u = ast.literal_eval(str(update)) with open( os.path.join(logsLocation, str(update.message.chat_id) + '.txt'), 'a') as f: f.write( str(u['message']['from']['username'].encode('utf-8')) + '\t' + str(u['message']['from']['first_name'].encode('utf-8')) + '\t' + str(u['message']['from']['last_name'].encode('utf-8')) + '\t' + str(u['message']['text'].encode('utf-8')) + '\t' + str(comment) + '\n') def start(bot, update): """ /start . ( privacy.yaml, users). , . """ # chat_id chat_id = update.message.chat_id # config = updateSettings() # if chat_id in config['users']: logs(update, 'Received /start command from user in privacy.yaml') # reply_markup = makeCustomKeyboard(config['users'][int(chat_id)]['keyboard']) bot.sendMessage(chat_id=chat_id, text=', ', reply_markup=reply_markup) logs(update, 'List of available reports sent') # else: logs(update, 'Received /start command from user NOT IN privacy.yaml') # bot.sendMessage(chat_id=chat_id, text=', . ' + \ ' , ') # u = ast.literal_eval(str(update)) bot.sendMessage(chat_id=config['admin']['id'], text=' ' + str(u['message']['from']['username'].encode('utf-8')) + ' ' + str(u['message']['from']['first_name'].encode('utf-8')) + ' ' + str(u['message']['from']['last_name'].encode('utf-8')) + ' ' + str(chat_id)) logs(update, 'Request for access sent') def echo(bot, update): """ , . , """ # chat_id chat_id=update.message.chat_id # response = update.message.text # config = updateSettings() # ( greenLight = True) greenLight = False # for line in config['users'][chat_id]['keyboard']: # for button in line: if button['buttonText'] == response: logs(update, 'Menu found, generating current keyboard and reports') # reply_markup = makeCustomKeyboard( config['users'][chat_id]['keyboard'] ) logs(update, 'Current keyboard generated') # greenLight = True # reportName = button['reportName'] reportAddress = button['report'] + '?:refresh=yes' if greenLight: logs(update, 'Starting to generate report') # ( ) bot.sendMessage(chat_id=chat_id, text=' , ...', reply_markup=reply_markup) logs(update, 'Message about report generation sent') # executeFile = os.path.join( tabcmdLocation, 'tabcmd.exe' ) # subprocess.call([ executeFile, 'login', '-s', 'localhost', '-u', 'admin', '-p', 'adminPassword']) # PDF subprocess.call([ executeFile, 'get', reportAddress, '-f', reportsLocation + reportName + '.pdf']) # subprocess.call([ executeFile, 'logout']) logs(update, 'Report generated, sending document') # bot.send_document(chat_id=chat_id, document=open(reportsLocation + reportName.encode('utf-8') + '.pdf', 'rb')) logs(update, 'Report sent') elif response == token: updater.stop() # , else: logs(update, 'Bad message, no access to this report') # reply_markup = makeCustomKeyboard(config['users'][chat_id]['keyboard']) bot.sendMessage(chat_id=chat_id, text=' . ' + \ ', admin@tableau.mycompany.ru ', reply_markup=reply_markup) logs(update, 'Main menu keyboard sent after bad message') # updater = Updater(token=token) dispatcher = updater.dispatcher updater.start_polling() # /start start_handler = CommandHandler('start', start) dispatcher.add_handler(start_handler) # echo_handler = MessageHandler([Filters.text], echo) dispatcher.add_handler(echo_handler)
Source: https://habr.com/ru/post/319690/
All Articles