beta -> master
[telefricabot.git/.git] / bot.py
1 """«Copyright 2021 Boris Daniel»
2 GPL v3 -> licence.txt"""
3 author='Boris Daniel Martinez Millán'
4 email='borisdanielmm@nauta.cu'
5 name='Reisub-Bot'
6
7 try:
8         from include import friendica_u,welcome,admin_id,token
9 except:
10         raise ValueError('Hubo un error al importar el include.py')
11 from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update,ReplyMarkup,Bot,user
12 from telegram.ext import Updater, CommandHandler, CallbackQueryHandler, CallbackContext, MessageHandler, Filters, CallbackContext
13 #Importandk Friendica
14 from friendica_module import log_friend,logout_friend,publish,notifications
15 #Importando la base
16 import requests,io,random,re,api,crud,datetime,time,telegram
17 from os import remove
18 #Funcion base, no eliminar
19 def typing(chat):
20         bot.send_chat_action(chat_id=chat, action=telegram.ChatAction.TYPING)
21
22 ua = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:60.0) Gecko/20100101'
23 ua += ' Firefox/60.0'
24 HEADERS = {'user-agent': ua}
25
26 #Idioma español
27 es={'help':'''/start - Inicia el bot
28
29 /help - Muestra este menu
30 /short_url - Un acortador de URLs
31
32 /login_f - Logeate en tu cuenta de Friendicarg
33
34 /logout_f - Borra tu cuenta de la base de datos
35
36 /publish - Publica texto en tu cuenta de Friendica desde el cliente de Telegram''',
37 'pub':'Publicado con exito 😉',
38 'not_l_f':'Usted no esta logueado 😭',#
39 'cha_l_f':'Su contraseña es incorrecta 😧',#
40 'usa_l_f':'Usuario añadido 😎 ',#
41 'usd_l_f':'Usuario eliminado 😭',#
42 'usl_l_f':'Su usuario ya esta regtistrado 😎, para eliminarlo mande el comando /logout_f',#
43 'usu_l_f':'El usuario o la contraseña esta mal,por favor enviame de nuevo el comando con el usuario y la contraseña',#
44 'log_s_f':'usa esta sintaxis:\n/login_f Usuario contraseña',
45 'finish':'Exito'}
46
47
48 #Idioma ingles
49 en={'help':'''/start - Start the bot
50
51 /help - Show this menu
52
53 /short_url - A smaller url creator
54
55 /login_f - Login into your Friendicarg account
56
57 /logout_f - Delete your account from the database
58
59 /publish - Publish text into your Friendicarg account with telegram client''',
60 'pub':'Sucess!',
61 'not_l_f':'You not are logged in 😭',
62 'cha_l_f':'You password is incorrect 😧',
63 'usa_l_f':'User added 😎',
64 'usd_l_f':'User deleted 😭',
65 'usl_l_f':'Your user is registered 😎, for delete this please send me this comand /logout_f',
66 'usu_l_f':'The user or password is incorrect please send me again the comand with the user and password',
67 'log_s_f':'Use this sintax:\n/login_f user password',
68 'finish':'Sucess!'}
69
70 #Codigo base de todo abajo 
71 def return_string(string,lang):
72         if "es" in lang:
73                 return es[string]
74         elif "en" in lang:
75                 return en[string]
76         else:
77                 return es[string]
78
79 def horacu():
80         global x
81         x = datetime.datetime.now()
82         hora=int("%s" %x.hour)
83         hora_aqui=22
84         hora_s=hora+hora_aqui
85         if hora_s>=25:
86                 hora_s=hora_s-24
87         else:
88                 pass
89         
90         return str(hora_s)+":%s:%s" % (x.month, x.second)
91
92 try:
93         crud.connect("friend_users.db")
94         crud.runcode("""CREATE TABLE users(telegram INT,user STRING,password STRING)""")
95         crud.save()
96         crud.close() 
97 except:
98         pass
99
100 bot=Bot(token)
101
102 def start(update:Update, context: CallbackContext) -> None:
103         typing(update.message.chat_id)
104         try:
105                 lan=io.open(update.effective_user.username,"r")
106                 lang=lan.read()
107                 lan.close()
108         except:
109                 lang='es'
110         try:
111                 update.message.reply_text(welcome[lang])
112         except:
113                 update.message.reply_text(welcome['es'])
114
115 def sugerir(update:Update, context: CallbackContext) -> None:
116         bot.send_chat_action(chat_id=update.message.chat_id, action=telegram.ChatAction.TYPING)
117         try:
118                 lan=io.open(update.effective_user.username,"r")
119                 lang=io.read()
120                 lan.close()
121         except:
122                 lang="es"
123         bot.send_message(chat_id=admin_id,text="El personaje: @"+update.effective_user.username+" hizo una sugerencia: "+" ".join(context.args))
124         update.reply_text(return_string('finish',))
125
126 def help(update:Update, context: CallbackContext) -> None:
127         bot.send_chat_action(chat_id=update.message.chat_id, action=telegram.ChatAction.TYPING)
128         try:
129                 lan=io.open(update.effective_user.username,"r")
130                 lang=lang.read()
131                 lan.close()
132         except:
133                 lang="es"
134         update.message.reply_text(return_string('help',lang))
135
136
137 def set(update:Update, context: CallbackContext) -> None:
138         if update.message.text.replace("/set ","").split(" ")[0]=="language":
139                 if "en" in update.message.text.replace("/set ","").split(" "):
140                         f=io.open(update.effective_user.username,"w")
141                         f.write("en")
142                         f.close()
143                         update.message.reply_text('Sucess')
144                 elif "es" in update.message.text.replace("/set ","").split(" "):
145                         f=io.open(update.effective_user.username,"w")
146                         f.write("es")
147                         f.close()
148                         update.message.reply_text('Exito')
149                 else:
150                         update.message.reply_text("You can help sending this comand /contribute\nPuedes contribuir mandando el comando /contribute")
151 if __name__=='__main__':
152         updater=Updater(token=token)
153         dispatcher=updater.dispatcher
154         dispatcher.add_handler(CommandHandler('start', start))
155         dispatcher.add_handler(CommandHandler('sugerencia', sugerir))
156         dispatcher.add_handler(CommandHandler('login_f', log_friend))
157         dispatcher.add_handler(CommandHandler('logout_f', logout_friend))
158         dispatcher.add_handler(CommandHandler('publish', publish))
159         dispatcher.add_handler(CommandHandler('help',help))
160         dispatcher.add_handler(CommandHandler('set',set))
161         dispatcher.add_handler(CommandHandler('notifications',notifications))
162
163         updater.start_polling()
164         updater.idle()
165
166
167
168
169