Advertisement
Guest User

function

a guest
Oct 18th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.48 KB | None | 0 0
  1. #-*- Coding: utf-8 -*-
  2. import telebot
  3. from pathlib import Path
  4. import os
  5. import random
  6.  
  7. bottoken = '794241937:AAFMSkYblndvGWBpOXjfoO5pzNp59UqMblE'
  8. bot = telebot.TeleBot(bottoken)
  9.  
  10. okay = ['okay', 'ok', 'k', 'yes', 'yep']
  11. admins = [667754854]
  12.  
  13. @bot.message_handler(commands=['order'])
  14. def ordercommand(message):
  15.     if message.chat.type == 'private':
  16.         try:
  17.             open(str(message.chat.id) + '.txt', "r").close()
  18.         except FileNotFoundError:
  19.             try:
  20.                 usrnm1 = open(str(message.chat.id) + '.txt', "w")
  21.                 usrnm1.write('@' + str(message.chat.username) + '\n')
  22.                 usrnm1.close()
  23.             except:
  24.                 print('ERROR : Error while creating new File!')
  25.             else:
  26.                 bot.reply_to(message, "Your're Data will be stored in our Servers for ordering other products. Is that ok?")
  27.         else:
  28.             try:
  29.                 os.remove(str(message.chat.id) + '.txt')
  30.             except:
  31.                 print('ERROR : Error in ordercommand()')
  32.             else:
  33.                 try:
  34.                     usrnm = open(str(message.chat.id) + '.txt', "w")
  35.                     usrnm.write('@' + str(message.chat.username) + '\n')
  36.                     usrnm.close()
  37.                 except Exception as e:
  38.                     print('ERROR : Error while creating new File!')
  39.                     print('ERROR : ' + e)
  40.                 else:
  41.                     bot.reply_to(message, 'You are now going to the order process. Is that ok?')
  42.                     bot.register_next_step_handler(message, order)
  43.  
  44. def order(message):
  45.     global okay
  46.     if message.text == 'okay': #To do: Make a custom button
  47.         bot.send_message(message.chat.id, 'Please enter the Product ID.')
  48.         bot.register_next_step_handler(message, save_id)
  49.     else:
  50.         bot.reply_to(message, "Sorry, I didn't understand you.")
  51.  
  52. def save_id(message):
  53.     file = open(str(message.chat.id) + '.txt', "a")
  54.     file.write(str(message.chat.id) + '\n')
  55.     try:
  56.         print(int(message.text))
  57.     except:
  58.         bot.reply_to(message, 'ID Should be a number!')
  59.     else:
  60.         file.write(str(message.text) + '\n')
  61.         file.close()
  62.         bot.send_message(message.chat.id, "Now send the picture of the product. Make sure it's a png or jpg file.")
  63.         bot.register_next_step_handler(message, picture_handler)
  64.  
  65. def picture_handler(message):
  66.     fileID = message.photo[-1].file_id # get the file id (seems has 3 sizes?) [-1] means the last one
  67.     file_info = bot.get_file(fileID) # get file info
  68.     downloaded_file = bot.download_file(file_info.file_path) # download file data?
  69.  
  70.     print(file_info)
  71.  
  72.     fname = os.path.splitext(os.path.basename(file_info.file_path))[0]
  73.  
  74.     with open(str(message.chat.id) + "_1.png", 'wb') as new_file: # create file on a path?
  75.         new_file.write(downloaded_file)
  76.     bot.send_message(message.chat.id, 'Send me another one!')
  77.     bot.register_next_step_handler(message, picture_handler_2)
  78.  
  79. def picture_handler_2(message):
  80.     fileID = message.photo[-1].file_id # get the file id (seems has 3 sizes?) [-1] means the last one
  81.     file_info = bot.get_file(fileID) # get file info
  82.     downloaded_file = bot.download_file(file_info.file_path) # download file data?
  83.  
  84.     print(file_info)
  85.  
  86.     fname = os.path.splitext(os.path.basename(file_info.file_path))[0]
  87.  
  88.     with open(str(message.chat.id) + "_2.png", 'wb') as new_file: # create file on a path?
  89.         new_file.write(downloaded_file)
  90.     bot.send_message(message.chat.id, "Now send me you're phone number.")
  91.     bot.register_next_step_handler(message, phonenumber)
  92.  
  93. def phonenumber(message):
  94.     pn = open(str(message.chat.id) + '.txt', "a")
  95.     pn.write(str(message.text) + '\n')
  96.     pn.close()
  97.     bot.send_message(message.chat.id, 'Now send me the shipping adress.')
  98.     bot.register_next_step_handler(message, adress)
  99.  
  100. def adress(message):
  101.     global admins
  102.     global srandom
  103.     bot.send_message(message.chat.id, "Thank you! you're information is beeing sent to an admin.")
  104.     sa = open(str(message.chat.id) + '.txt', "a")
  105.     sa.write(str(message.text) + '\n')
  106.     rnd_admin = random.choice(admins)
  107.     sndfile = open(str(message.chat.id) + '.txt', "r")
  108.     send = sndfile.read()
  109.     sndfile.close()
  110.     bot.send_message(rnd_admin, send)
  111.     bot.send_photo(rnd_admin, str(message.chat.id) + '_1.png')
  112.     bot.send_photo(rnd_admin, str(message.chat.id) + '_2.png')
  113.  
  114. #Reordering
  115. #---------------------------------------------------------------------
  116.  
  117. @bot.message_handler(commands=['reorder'])
  118. def reorder(message):
  119.     try:
  120.         open(str(message.chat.id) + '.txt', "r").close()
  121.     except FileNotFoundError:
  122.         bot.send_message(message.chat.id, "You have never ordered before!")
  123.     else:
  124.         bot.send_message(message.chat.id, "Now send me the one picture. Make sure it's a jpg or png file.")
  125.         bot.register_next_step_handler(message, picture_handler_re)
  126.  
  127. def picture_handler_re(message):
  128.     fileID = message.photo[-1].file_id # get the file id (seems has 3 sizes?) [-1] means the last one
  129.     file_info = bot.get_file(fileID) # get file info
  130.     downloaded_file = bot.download_file(file_info.file_path) # download file data?
  131.  
  132.     print(file_info)
  133.  
  134.     fname = os.path.splitext(os.path.basename(file_info.file_path))[0]
  135.  
  136.     with open(str(message.chat.id) + "_1.png", 'wb') as new_file: # create file on a path?
  137.         new_file.write(downloaded_file)
  138.     bot.send_message(message.chat.id, 'Send me another one!')
  139.     bot.register_next_step_handler(message, picture_handler_2_re)
  140.  
  141. def picture_handler_2_re(message):
  142.     global admins
  143.     global srandom
  144.     fileID = message.photo[-1].file_id # get the file id (seems has 3 sizes?) [-1] means the last one
  145.     file_info = bot.get_file(fileID) # get file info
  146.     downloaded_file = bot.download_file(file_info.file_path) # download file data?
  147.  
  148.     print(file_info)
  149.  
  150.     fname = os.path.splitext(os.path.basename(file_info.file_path))[0]
  151.  
  152.     with open(str(message.chat.id) + "_2.png", 'wb') as new_file: # create file on a path?
  153.         new_file.write(downloaded_file)
  154.     bot.send_message(message.chat.id, "Thank you! you're information is beeing sent to an admin.")
  155.     try:
  156.         rnd_admin = random.choice(admins)
  157.         sndfile = open(str(message.chat.id) + '.txt', "r")
  158.         send = sndfile.read()
  159.         bot.send_message(rnd_admin, send)
  160.         sndfile.close()
  161.     except Exception as e:
  162.         print('ERROR : ' + e)
  163.  
  164. bot.polling()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement