theo33500

Untitled

Nov 5th, 2018
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.32 KB | None | 0 0
  1. import os
  2. import sys
  3. import random
  4. import time
  5. import telepot
  6. from telepot.loop import MessageLoop
  7.  
  8. chemin = "/ton/chemin/ici"
  9. channelid = 000 #remplace 000 par l'id du channel
  10. token = "ton token ici"
  11.  
  12. def getListOfFiles(dirName):
  13.     # create a list of file and sub directories
  14.     # names in the given directory
  15.     listOfFile = os.listdir(dirName)
  16.     allFiles = list()
  17.     # Iterate over all the entries
  18.     for entry in listOfFile:
  19.         # Create full path
  20.         fullPath = os.path.join(dirName, entry)
  21.         # If entry is a directory then get the list of files in this directory
  22.         if os.path.isdir(fullPath):
  23.             allFiles = allFiles + getListOfFiles(fullPath)
  24.         else:
  25.             allFiles.append(fullPath)
  26.                
  27.     return allFiles
  28.  
  29. def getrandom():
  30.     randomfile = random.choice(getListOfFiles(chemin))
  31.     if randomfile.endswith('.gif'):
  32.         getrandom()
  33.     else:
  34.         return randomfile
  35.  
  36. def handle(msg):
  37.     # content_type, chat_type, chat_id = telepot.glance(msg)
  38.     # print(content_type, chat_type, chat_id)
  39.     randomfile = getrandom()
  40.     bot.sendPhoto(channelid, open(randomfile, 'rb'))
  41.     time.sleep(5)
  42.     exit()
  43.  
  44. def main():
  45.     global bot
  46.     bot = telepot.Bot(token)
  47.     handle("")
  48.     while 1:
  49.         time.sleep(3)
  50.    
  51.  
  52. main()
Advertisement
Add Comment
Please, Sign In to add comment