Advertisement
Guest User

need help mas rendy

a guest
Dec 24th, 2016
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.78 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.        
  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.                 print (SHREDDER_CRUMB_AMPERE)
  88.         else:
  89.             output = 'Data not found'
  90.     except:
  91.         output = 'error'
  92.     return output
  93.    
  94. def cek_user(msg):
  95.     user_id = msg['from']['id']
  96.     cursor = db.cursor()
  97.     sql = "SELECT * FROM user WHERE `id` = '%d'" % user_id
  98.     try:
  99.         cursor.execute(sql)
  100.         # memecahkan hasil dari table
  101.         results = cursor.fetchall()
  102.         if len(results)>0:
  103.             output = ''
  104.             for row in results:
  105.                 id = row[0]
  106.                 keterangan = row[1]
  107.                 # Now print fetched result
  108.                 output = id
  109.         else:
  110.             print ('Got command: '+'Any person try send message from ({}) and user id ({})'.format(username, user_id))
  111.             bot.sendMessage(chat_id, 'Hello {}! Sorry, your user_id {} not registered'.format(username, user_id))
  112.            
  113.     except:
  114.         output = 'error'
  115.     return output
  116.  
  117.        
  118. # deklarasi database
  119.  
  120.  
  121. bot = telepot.Bot('321559260:AAEtxA_pRZifaXw5BP66diXv4PcKUm6evUc')
  122. bot.message_loop(handle)
  123. print 'Membaca ...'
  124.  
  125. while 1:
  126.     time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement