我还是 python 脚本的初学者。我想问一下标题名称,我在我的办公室监控 KPI 的想法创新,并且几乎完成。但有问题不能自己完成。
如何使用 python 显示从表 mysql 到电报机器人的多条记录,并且只有某些电话号码可以访问我的机器人电报?
这是我的脚本:
'如何显示从表 mysql 到电报机器人的多条记录'
def query(requ):
cursor = db.cursor()
sql = "SELECT * FROM monitor WHERE Tanggal LIKE '%" + str(requ.encode('utf-8')) + "%'"
try:
cursor.execute(sql)
# memecahkan hasil dari table
results = cursor.fetchall()
# memberikan hasil jika hasil sesuai sama tabel maka keluar sesuai hasil
if len(results)>0:
output = ''
for row in results:
Tanggal = row[0]
DOWNTIME_PLN = row[1]
RUN_GENSET = row[2]
SHREDDER_MILL_DOWNTIME = row[3]
SHREDDER_CRUMB_DOWNTIME = row[4]
SHREDDER_CRUMB_AMPERE = row[5]
has_trans = row[6]
# Now print fetched result
output = "Tanggal = %s, \nDOWNTIME_PLN = %d, \nRUN_GENSET = %s, \nSHREDDER_MILL_DOWNTIME = %s, \nSHREDDER_CRUMB_DOWNTIME=%s, \nSHREDDER_CRUMB_AMPERE=%s, \nhas_trans = %s" % \
(Tanggal,DOWNTIME_PLN,RUN_GENSET,SHREDDER_MILL_DOWNTIME,SHREDDER_CRUMB_DOWNTIME,SHREDDER_CRUMB_AMPERE,has_trans)
print (Tanggal,DOWNTIME_PLN,RUN_GENSET,SHREDDER_MILL_DOWNTIME,SHREDDER_CRUMB_DOWNTIME,SHREDDER_CRUMB_AMPERE,has_trans)
else:
output = 'No word in my dic'
except:
output = 'error'
return output
'如何只有某些电话号码才能访问我的机器人电报?
def handle(msg):
#Deklarasi untuk profile Telegram
chat_id = msg['chat']['id']
command = msg['text']
user_id = msg['from']['id']
username = msg['from']['first_name']
fullname = msg['from']['last_name']+msg['from']['first_name']
timestr = time.strftime("%Y-%m-%d_%H")
content_type, chat_type, chat_id = telepot.glance(msg)
if (content_type == 'text'):
command = msg['text']
print ('Got command: %s' % command)
#Dekklarasi command apa saja yang mau dimasukin
if command == 'halo':
keyboard = [
['Halo', 'erlang'],
['cpu', 'ddd'],
['ccc'],['Tutup']
]
reply_markup = ReplyKeyboardMarkup.create(keyboard)
bot.sendMessage(chat_id, "Assalamu'alaikum "+username+", ..?",reply_markup=reply_markup)
elif command == 'erlang':
bot.sendPhoto(chat_id=chat_id, photo=open('E:/@@@/@smartfren/capture trial/Compare_KPI_1' + '.jpg','rb'))
bot.sendPhoto(chat_id=chat_id, photo=open('E:/@@@/@smartfren/capture trial/Compare_KPI_2' + '.jpg','rb'))
elif command == 'cpu':
print ('Got command: %s' % timestr + command)
bot.sendMessage(chat_id=chat_id, :test:)
elif 'Tutup' in command:
hide_keyboard = {'hide_keyboard': True}
bot.sendMessage(chat_id, 'Fungsi KeyBoard ditutup!', reply_markup=hide_keyboard)
elif 'ccc' in command:
reps_content = query(command)
bot.sendMessage(chat_id, reps_content)
elif 'ddd' in command:
reps_content = query2(command)
bot.sendMessage(chat_id, reps_content)
谢谢 :)