#prog bot v1.3 MAJ 22/01/21

#!/usr/bin/python2.7
# coding: utf-8
# encoding=utf8
import sys
reload(sys)
sys.setdefaultencoding('utf8')

import datetime
import telepot
import time
import requests
import os
import glob
from telepot.loop import MessageLoop

token = 'VOTRE CLÉ TOKEN-gardez les apostrophes'
bot = telepot.Bot(token)

rootID = 0000000000

bot.sendMessage(rootID, "Salut BOSS !\n\nJe viens d'être activé !\npi4-2go opérationnel.")

sep = "\n------------------------------------------------------------\n"

def handle(msg):
	rep = '';
	
	chat_id = msg['chat']['id']
	prenom = msg['chat']['first_name']
	user = msg['chat']['username']
	command = msg['text']
	
	#LES COMMANDES
	if command[0] == '/':
		
		print("-> "+str(user)+" ("+str(chat_id)+")"+" get command:\n"+str(command)+sep)
		
		bot.sendMessage(chat_id, "Traitement de la commande...")
		if command == '/help':
			rep =  "Besoin d'aide ?\n\n-/help: Liste des commandes\n-/myid: Ton ID chat Telegram\n-/allMyInfo: Toutes tes informations chat\n-/time: La date et l'heure\n-/cpuTemp: CPU température\n-/stopbot: Eteindre le BOT (only root)"
		#Divers
		elif command == '/myid':
			rep = "Ton ID chat: " +str(chat_id)
		elif command =='/allMyInfo':
			long = len(msg['chat'])
			rep = 'Tes informations Telegram:\n'
			for info in msg['chat']:
				rep = rep+"\n"+info+": "+str(msg['chat'][info])
		elif command == "/time":
			now = time.localtime(time.time())
			rep = "Aujourd'hui nous sommes le " + (time.strftime("%d/%m/%Y", now)) + " et il est " + (time.strftime("%H:%M:%S", now)) 
		elif  command == '/cpuTemp':
			rep = "Impossible de lire la température du CPU pour le moment..."
			with open('/sys/class/thermal/thermal_zone0/temp', 'r') as ftemp:
				current_temp = int(ftemp.read()) / 1000
				rep = "La température du CPU est de "+str(current_temp)+"°C"
		elif command == '/stopbot':
			if chat_id == rootID:
				bot.sendMessage(rootID, "Bye BOSS, the BOT is now disabled")
				os._exit(1)
			else:
				rep = "Il m'est impossible de traiter cette commande, car tu n'as pas les autorisations requises pour ça... Demande au BOSS si il veut bien te les donner..."
				bot.sendMessage(rootID, "Fucking user want to stop the BOT !!!")
				bot.sendMessage(rootID, ("User ID Chat: ", chat_id))
		else:
			rep= "OUPS... La commande "+command+" n'existe pas...\n-/help pour plus d'infos"
	#LES MESSAGES
	else:
		print("-> "+str(user)+" ("+str(chat_id)+") chat:\n"+str(command)+sep)
		command = command.lower()
		rep = 'Dsl, je ne comprends pas...'
		
		if command == 'salut':
			rep = 'Hé salut '+str(prenom)+' !'
	
	#on affiche la réponse si elle est renseignée
	if rep != '':
		bot.sendMessage(chat_id, rep)
		print ("-> "+"BOT reply to "+str(user)+" ("+str(chat_id)+"):\n"+str(rep)+sep)
			

MessageLoop(bot, handle).run_as_thread()
print ("I am listening ...\n")

while 1:
    time.sleep(10)
