First unstable commit

This commit is contained in:
Boris Daniel Martinez Millán 2021-03-09 00:38:37 -05:00
parent 71906d2c7d
commit 25542f702b
3 changed files with 41 additions and 25 deletions

39
bot.py
View File

@ -12,6 +12,8 @@ from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update,ReplyMar
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler, CallbackContext, MessageHandler, Filters, CallbackContext from telegram.ext import Updater, CommandHandler, CallbackQueryHandler, CallbackContext, MessageHandler, Filters, CallbackContext
#Importandk Friendica #Importandk Friendica
from friendica_module import log_friend,logout_friend,publish,notifications from friendica_module import log_friend,logout_friend,publish,notifications
#Importando Uptime
from uptime_module import uptime
#Importando la base #Importando la base
import requests,io,random,re,api,crud,datetime,time,telegram import requests,io,random,re,api,crud,datetime,time,telegram
from os import remove from os import remove
@ -22,7 +24,11 @@ def typing(chat):
ua = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:60.0) Gecko/20100101' ua = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:60.0) Gecko/20100101'
ua += ' Firefox/60.0' ua += ' Firefox/60.0'
HEADERS = {'user-agent': ua} HEADERS = {'user-agent': ua}
help1=''
def log_function(dispatcher,name,function1):
global help1
dispatcher.add_handler(CommandHandler(name,function1))
help1=help1+str('/'+name)+' -> '+function1.__doc__+'\n\n'
#Idioma español #Idioma español
es={'help':'''/start - Inicia el bot es={'help':'''/start - Inicia el bot
@ -100,6 +106,7 @@ except:
bot=Bot(token) bot=Bot(token)
def start(update:Update, context: CallbackContext) -> None: def start(update:Update, context: CallbackContext) -> None:
"""Inicia el Bot"""
typing(update.message.chat_id) typing(update.message.chat_id)
try: try:
lan=io.open(update.effective_user.username,"r") lan=io.open(update.effective_user.username,"r")
@ -113,6 +120,7 @@ def start(update:Update, context: CallbackContext) -> None:
update.message.reply_text(welcome['es']) update.message.reply_text(welcome['es'])
def sugerir(update:Update, context: CallbackContext) -> None: def sugerir(update:Update, context: CallbackContext) -> None:
"""Sugiere que aladan algo o mejoren algo"""
bot.send_chat_action(chat_id=update.message.chat_id, action=telegram.ChatAction.TYPING) bot.send_chat_action(chat_id=update.message.chat_id, action=telegram.ChatAction.TYPING)
try: try:
lan=io.open(update.effective_user.username,"r") lan=io.open(update.effective_user.username,"r")
@ -124,17 +132,12 @@ def sugerir(update:Update, context: CallbackContext) -> None:
update.reply_text(return_string('finish',)) update.reply_text(return_string('finish',))
def help(update:Update, context: CallbackContext) -> None: def help(update:Update, context: CallbackContext) -> None:
bot.send_chat_action(chat_id=update.message.chat_id, action=telegram.ChatAction.TYPING) """Devuelve esta ayuda"""
try: update.message.reply_text(help1)
lan=io.open(update.effective_user.username,"r")
lang=lang.read()
lan.close()
except:
lang="es"
update.message.reply_text(return_string('help',lang))
def set(update:Update, context: CallbackContext) -> None: def set(update:Update, context: CallbackContext) -> None:
"""Establece alguna configuraciones"""
if update.message.text.replace("/set ","").split(" ")[0]=="language": if update.message.text.replace("/set ","").split(" ")[0]=="language":
if "en" in update.message.text.replace("/set ","").split(" "): if "en" in update.message.text.replace("/set ","").split(" "):
f=io.open(update.effective_user.username,"w") f=io.open(update.effective_user.username,"w")
@ -151,15 +154,15 @@ def set(update:Update, context: CallbackContext) -> None:
if __name__=='__main__': if __name__=='__main__':
updater=Updater(token=token) updater=Updater(token=token)
dispatcher=updater.dispatcher dispatcher=updater.dispatcher
dispatcher.add_handler(CommandHandler('start', start)) log_function(dispatcher,'start', start)
dispatcher.add_handler(CommandHandler('sugerencia', sugerir)) log_function(dispatcher,'sugerencia', sugerir)
dispatcher.add_handler(CommandHandler('login_f', log_friend)) log_function(dispatcher,'login_f', log_friend)
dispatcher.add_handler(CommandHandler('logout_f', logout_friend)) log_function(dispatcher,'logout_f', logout_friend)
dispatcher.add_handler(CommandHandler('publish', publish)) log_function(dispatcher,'publish', publish)
dispatcher.add_handler(CommandHandler('help',help)) log_function(dispatcher,'help',help)
dispatcher.add_handler(CommandHandler('set',set)) log_function(dispatcher,'set',set)
dispatcher.add_handler(CommandHandler('notifications',notifications)) log_function(dispatcher,'notifications',notifications)
log_function(dispatcher,'uptime',uptime)
updater.start_polling() updater.start_polling()
updater.idle() updater.idle()

View File

@ -43,13 +43,14 @@ def return_string(string,lang):
return es[string] return es[string]
def log_friend(update: Update, context: CallbackContext) -> None: def log_friend(update: Update, context: CallbackContext) -> None:
"""Permite el logueo en tu instancia de friendica"""
try: try:
lan=io.open(update.effective_user.username,"r") lan=io.open(update.effective_user.username,"r")
lang=lan.read() lang=lan.read()
lan.close() lan.close()
except: except:
lang="es" lang="es"
bot.send_chat_action(chat_id=update.message.chat_id, action=telegram.ChatAction.TYPING) context.bot.send_chat_action(chat_id=update.message.chat_id, action=telegram.ChatAction.TYPING)
print(context.args) print(context.args)
if len(context.args)<=1: if len(context.args)<=1:
update.message.reply_text(return_string('log_s_f',lang)) update.message.reply_text(return_string('log_s_f',lang))
@ -65,16 +66,17 @@ def log_friend(update: Update, context: CallbackContext) -> None:
crud.save();crud.close() crud.save();crud.close()
except ValueError: except ValueError:
update.message.reply_text(return_string('usu_l_f',lang)) update.message.reply_text(return_string('usu_l_f',lang))
context.bot.delete_message(chat_id = update.message.chat_id, message_id = update.message.message_id) context.context.bot.delete_message(chat_id = update.message.chat_id, message_id = update.message.message_id)
def logout_friend(update: Update, context: CallbackContext) -> None: def logout_friend(update: Update, context: CallbackContext) -> None:
"""Borra la cuenta de tu instancia de friendica"""
try: try:
lan=io.open(update.effective_user.username,"r") lan=io.open(update.effective_user.username,"r")
lang=lan.read() lang=lan.read()
lan.close() lan.close()
except: except:
lang="es" lang="es"
bot.send_chat_action(chat_id=update.message.chat_id, action=telegram.ChatAction.TYPING) context.bot.send_chat_action(chat_id=update.message.chat_id, action=telegram.ChatAction.TYPING)
crud.connect("friend_users.db") crud.connect("friend_users.db")
if len(crud.read("users","telegram",update.message.chat_id))==1: if len(crud.read("users","telegram",update.message.chat_id))==1:
crud.delete("users","telegram",str(update.message.chat_id)) crud.delete("users","telegram",str(update.message.chat_id))
@ -84,13 +86,14 @@ def logout_friend(update: Update, context: CallbackContext) -> None:
else: else:
update.message.reply_text(return_string('not_l_f',lang)) update.message.reply_text(return_string('not_l_f',lang))
def publish(update: Update, context: CallbackContext) -> None: def publish(update: Update, context: CallbackContext) -> None:
"""Publica en tu cuenta, requiere estar logueado"""
try: try:
lan=io.open(update.effective_user.username,"r") lan=io.open(update.effective_user.username,"r")
lang=lan.read() lang=lan.read()
lan.close() lan.close()
except: except:
lang="es" lang="es"
bot.send_chat_action(chat_id=update.message.chat_id, action=telegram.ChatAction.TYPING) context.bot.send_chat_action(chat_id=update.message.chat_id, action=telegram.ChatAction.TYPING)
print(lang) print(lang)
crud.connect("friend_users.db") crud.connect("friend_users.db")
r=crud.read("users","telegram",update.message.chat_id) r=crud.read("users","telegram",update.message.chat_id)
@ -124,7 +127,7 @@ def publish(update: Update, context: CallbackContext) -> None:
rex.replace("!hora!",horavar) rex.replace("!hora!",horavar)
friend.share(rex,contexto.replace(rex,'').replace('/publish','')+"\n#telegram") friend.share(rex,contexto.replace(rex,'').replace('/publish','')+"\n#telegram")
update.message.reply_text(rex+'\n'+contexto.replace('/publish@reisub_bot','').replace('/publish ','').replace('#!'+rex+'!#','')) update.message.reply_text(rex+'\n'+contexto.replace('/publish@reisub_bot','').replace('/publish ','').replace('#!'+rex+'!#',''))
context.bot.delete_message(chat_id = update.message.chat_id, message_id = update.message.message_id) context.context.bot.delete_message(chat_id = update.message.chat_id, message_id = update.message.message_id)
else: else:
friend=api.FriendApi("https://friendicarg.nsupdate.info/",r[0][1],r[0][2]) friend=api.FriendApi("https://friendicarg.nsupdate.info/",r[0][1],r[0][2])
contexto=update.message.text contexto=update.message.text
@ -157,13 +160,14 @@ def publish(update: Update, context: CallbackContext) -> None:
else: else:
update.message.reply_text(return_string('not_l_f',lang)) update.message.reply_text(return_string('not_l_f',lang))
def notifications(update:Update,context:CallbackContext): def notifications(update:Update,context:CallbackContext):
"""Devuelve las notificaciones de tu cuenta"""
try: try:
lan=io.open(update.effective_user.username,"r") lan=io.open(update.effective_user.username,"r")
lang=lan.read() lang=lan.read()
lan.close() lan.close()
except: except:
lang="es" lang="es"
bot.send_chat_action(chat_id=update.message.chat_id, action=telegram.ChatAction.TYPING) context.bot.send_chat_action(chat_id=update.message.chat_id, action=telegram.ChatAction.TYPING)
crud.connect("friend_users.db") crud.connect("friend_users.db")
r=crud.read("users","telegram",update.message.chat_id) r=crud.read("users","telegram",update.message.chat_id)
if len(r)==1: if len(r)==1:
@ -173,13 +177,14 @@ def notifications(update:Update,context:CallbackContext):
update.message.reply_text(return_string('not_l_f',lang)) update.message.reply_text(return_string('not_l_f',lang))
def get_5(update:Update,context:CallbackContext): def get_5(update:Update,context:CallbackContext):
"""Devuelve los ultimos 5 posts del apartado network"""
try: try:
lan=io.open(update.effective_user.username,"r") lan=io.open(update.effective_user.username,"r")
lang=lan.read() lang=lan.read()
lan.close() lan.close()
except: except:
lang="es" lang="es"
bot.send_chat_action(chat_id=update.message.chat_id, action=telegram.ChatAction.TYPING) context.bot.send_chat_action(chat_id=update.message.chat_id, action=telegram.ChatAction.TYPING)
crud.connect("friend_users.db") crud.connect("friend_users.db")
r=crud.read("users","telegram",update.message.chat_id) r=crud.read("users","telegram",update.message.chat_id)
if len(r)==1: if len(r)==1:

8
uptime_module.py Normal file
View File

@ -0,0 +1,8 @@
import subprocess
from telegram import Update
from telegram.ext import Updater, CallbackContext
def uptime(update: Update,context: CallbackContext):
"""Uptime es un modulo solo compatible con linux que permite ver el tiempo desde que se encendio la pc"""
update.message.reply_text(subprocess_output('uptime',shell=True))