From 7bc36fbd54430ac5230e6a19f0607851885c1973 Mon Sep 17 00:00:00 2001 From: Administrator Date: Mon, 27 Apr 2020 21:10:03 +0200 Subject: [PATCH] Ajouter 'bot.py' --- bot.py | 136 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 bot.py diff --git a/bot.py b/bot.py new file mode 100644 index 0000000..bfde7dd --- /dev/null +++ b/bot.py @@ -0,0 +1,136 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# This program is dedicated to the public domain under the CC0 license. +# Telegram - Betaseries +import logging +import requests +import json +from telegram.ext import Updater, CommandHandler + +# Activer / Désactiver le debuggage +DEBUG = True + +#Configuration de Betaseries +client_id = '' +login='' +password = '' #Le password doit être chiffré en MD5 + +#Configuration de Telegram +user_id = '' +token_bot = '' + + + +logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', + level=logging.INFO) +logger = logging.getLogger(__name__) + +#####LE SCRIPT DEMARRE ICI ##### + + +def start(update, context): + update.message.reply_text('Bonjour Sensei. !') + update.message.reply_text('Je suis ici pour te servir, malgré ça : je reste en Beta ! Soyez doux.') + return + +def ping(update, context): + update.message.reply_text("Pong!") + return + +def init(update, context): + update.message.reply_text("Tentative de connexion à Betaseries...") + data = login_beta(update, context) + + return + + +def login_beta(update, context): + global betauser + print("Launch betaseries command") + API_auth = 'https://api.betaseries.com/members/auth' + API_auth += '?client_id=' + client_id +'&login=' + login + '&password=' + password + request = requests.post(API_auth) + if str(request) != '' : + error = "Impossible d'interroger l'API\n Code:" + str(request.status_code) + update.message.reply_text(error) + return + print(request.text) + data = request.json() + betauser = data['token'] + + message = "Token utilisé pour Betaseries : " + betauser + + API_Message = 'https://api.telegram.org/' + API_Message += 'bot' + token_bot + '/sendMessage?chat_id=' + user_id + '&text=' + + Notification = API_Message + "🥳 🎉 Connection Réussie ! " + request = requests.post(Notification) + + Notification = API_Message + " Votre Token sur Betaséries (⚠️ à ne pas partager ! ⚠️) : " + betauser + request = requests.post(Notification) + update.message.reply_text("Vous pouvez maintenant utiliser le bot !") + + return + + +def repeat(update, context): + message = '' + user = update.message.from_user + + if DEBUG == True : + print('Pseudo du compte : {} ID: {} '.format(user['username'], user['id'])) + + if len(context.args) > 1 : + for i in range(len(context.args)): + mot = str(context.args[i]) + " " + message += mot + message = message[:-1] + else : + message = context.args[0] + if DEBUG == True : + print("phrase envoyée : ", message) + + + if message == "Cinabre est parfait": + update.message.reply_text("J'avoue !") + return + + update.message.reply_text(message) + return + + + + +def main(): + updater = Updater(token_bot, use_context=True) + print("Bot connecté") + dp = updater.dispatcher + + # Définir quelle commande utilise quelle fonction + dp.add_handler(CommandHandler("start", start)) + dp.add_handler(CommandHandler("help", start)) + #Help n'existant pas, on utilise la même fonction que Start + + dp.add_handler(CommandHandler("ping", ping)) + + dp.add_handler(CommandHandler("repeat", repeat, + pass_args=True, + pass_job_queue=True, + pass_chat_data=True)) + + dp.add_handler(CommandHandler("initialisation", init, + pass_args=True, + pass_job_queue=True, + pass_chat_data=True)) + + dp.add_handler(CommandHandler("login", login_beta, + pass_args=True, + pass_job_queue=True, + pass_chat_data=True)) + + updater.start_polling() + updater.idle() + + +if __name__ == '__main__': + main() \ No newline at end of file