Advertisement
ligerrendy

Untitled

Dec 24th, 2016
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.20 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf8 -*-
  3.  
  4. #Library nya
  5. import sys
  6. import time
  7. import datetime
  8. import telepot
  9. import uniout
  10. import MySQLdb
  11. from ConfigParser import SafeConfigParser
  12. from twx.botapi import TelegramBot, ReplyKeyboardMarkup
  13.  
  14. db_host = 'localhost'
  15. db_uid = 'root'
  16. db_pass = 'passoword'
  17. db_name = 'belajar'
  18.  
  19. # Config database nya
  20. db = MySQLdb.connect(db_host, db_uid, db_pass, db_name, charset='utf8', init_command='SET NAMES UTF8')
  21.  
  22. def handle(msg):
  23.     #Deklarasi untuk profile Telegram
  24.     chat_id = msg['chat']['id']
  25.     command = msg['text']
  26.     user_id = msg['from']['id']
  27.     username = msg['from']['first_name']
  28.     fullname = msg['from']['last_name']+msg['from']['first_name']
  29.     timestr = time.strftime("%Y-%m-%d_%H")
  30.     content_type, chat_type, chat_id = telepot.glance(msg)
  31.     db.commit()
  32.  
  33.     if (user_id == cek_user(msg)):
  34.         command = msg['text']
  35.         print ('Got command: %s' % command)
  36.         #Dekklarasi command apa saja yang mau dimasukan
  37.         if command == 'halo':
  38.             keyboard = [
  39.                 ['Halo', 'erlang'],
  40.                 ['Cek id', 'ddd'],
  41.                 ['ccc'],['Tutup']
  42.             ]
  43.             reply_markup = ReplyKeyboardMarkup.create(keyboard)
  44.             bot.sendMessage(chat_id, "Assalamu'alaikum "+username+", ..?",reply_markup=reply_markup)
  45.         elif command == 'erlang':
  46.             bot.sendPhoto(chat_id=chat_id, photo=open('E:/@@@/@smartfren/capture trial/Compare_KPI_1' + '.jpg','rb'))
  47.             bot.sendPhoto(chat_id=chat_id, photo=open('E:/@@@/@smartfren/capture trial/Compare_KPI_2' + '.jpg','rb'))
  48.         elif command == 'Cek id':
  49.             print ('Got command: %s' % timestr + command)
  50.             bot.sendMessage(chat_id, 'Kamu adalah {} dan user kamu adalah {}'.format(username, user_id))
  51.         elif 'Tutup' in command:
  52.             hide_keyboard = {'hide_keyboard': True}
  53.             bot.sendMessage(chat_id, 'Fungsi KeyBoard ditutup!', reply_markup=hide_keyboard)
  54.         elif 'ccc' in command:
  55.             reps_content = query(command)
  56.             bot.sendMessage(chat_id, reps_content)
  57.         elif 'ddd' in command:
  58.             reps_content = query(command)
  59.             bot.sendMessage(chat_id, reps_content)
  60.     else:
  61.         print ('Got command: '+'Any person try message from ({}) and user id ({})'.format(username, user_id))
  62.         bot.sendMessage(chat_id, 'Hello {}! Sorry, your user_id {} not registered'.format(username, user_id))
  63.  
  64. def query(requ):
  65.  
  66.     cursor = db.cursor()
  67.     sql = "SELECT * FROM monitor WHERE tanggal LIKE '%" + str(requ.encode('utf-8')) + "%'"
  68.    
  69.     try:
  70.         cursor.execute(sql)
  71.         # memecahkan hasil dari table
  72.         results = cursor.fetchall()
  73.         output_beneran = []
  74.         # memberikan hasil jika hasil sesuai sama tabel maka keluar sesuai hasil
  75.         if len(results)>0:
  76.             output =  results
  77.             for row in results:
  78.                 Tanggal = row[0]
  79.                 DOWNTIME_PLN = row[1]
  80.                 RUN_GENSET = row[2]
  81.                 SHREDDER_MILL_DOWNTIME = row[3]
  82.                 SHREDDER_CRUMB_DOWNTIME = row[4]
  83.                 SHREDDER_CRUMB_AMPERE = row[5]
  84.                 has_trans = row[6]
  85.                 # Now print fetched result
  86.                 #output = (SHREDDER_CRUMB_AMPERE)
  87.                 output = ('Tanggal {tanggal},Downtime {DOWNTIME_PLN},run genset {genset},shredder mill downtime {shd},shredder crumb downtime {scd},shredder crumb ampere {sca},has trans {trans} '.format(tanggal=Tanggal,DOWNTIME_PLN = DOWNTIME_PLN,genset=RUN_GENSET,shd=SHREDDER_MILL_DOWNTIME,scd=SHREDDER_CRUMB_DOWNTIME,sca=SHREDDER_CRUMB_AMPERE,trans=has_trans))
  88.                 #print (SHREDDER_CRUMB_AMPERE)
  89.                 output_beneran.append(output)
  90.             return output_beneran
  91.         else:
  92.             output = 'Data not found'
  93.     except:
  94.         output = 'error'
  95.     return output
  96.    
  97. def cek_user(msg):
  98.     user_id = msg['from']['id']
  99.     cursor = db.cursor()
  100.     sql = "SELECT * FROM user WHERE `id` = '%d'" % user_id
  101.     try:
  102.         cursor.execute(sql)
  103.         # memecahkan hasil dari table
  104.         results = cursor.fetchall()
  105.         if len(results)>0:
  106.             output = ''
  107.             for row in results:
  108.                 id = row[0]
  109.                 keterangan = row[1]
  110.                 # Now print fetched result
  111.                 output = id
  112.         else:
  113.             print ('Got command: '+'Any person try send message from ({}) and user id ({})'.format(username, user_id))
  114.             bot.sendMessage(chat_id, 'Hello {}! Sorry, your user_id {} not registered'.format(username, user_id))
  115.            
  116.     except:
  117.         output = 'error'
  118.     return output
  119.  
  120.        
  121. # deklarasi database
  122.  
  123.  
  124. bot = telepot.Bot('321559260:AAEtxA_pRZifaXw5BP66diXv4PcKUm6evUc')
  125. bot.message_loop(handle)
  126. print 'Membaca ...'
  127.  
  128. while 1:
  129.     time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement