beta -> master
[telefricabot.git/.git] / friendica_module.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 -> Friendicarg'
6
7 from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update,ReplyMarkup,Bot,user
8 from telegram.ext import Updater, CommandHandler, CallbackQueryHandler, CallbackContext, MessageHandler, Filters, CallbackContext
9 import api,crud,re,io,telegram
10 token="1531348810:AAG_ZWBHHPI4qxjeWvyWOwwy3xPXsN_2KJ0"
11
12 bot=Bot(token)
13 es={'pub':'Publicado con exito 😉',
14 'not_l_f':'Usted no esta logueado 😭',
15 'cha_l_f':'Su contraseña es incorrecta 😧',
16 'usa_l_f':'Usuario añadido 😎 ',
17 'usd_l_f':'Usuario eliminado 😭',
18 'usl_l_f':'Su usuario ya esta registrado 😎, para eliminarlo mande el comando /logout_f',
19 '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',
20 'log_s_f':'usa esta sintaxis:\n/login_f Usuario contraseña',
21 'cls_l_f':'Tu publicación es muy pequeña o no tiene contenido'}
22 en={'pub':'Sucess!',
23 'not_l_f':'You not are logged in 😭',
24 'cha_l_f':'You password is incorrect 😧',
25 'usa_l_f':'User added 😎',
26 'usd_l_f':'User deleted 😭',
27 'usl_l_f':'Your user is registered 😎, for delete this please send me this comand /logout_f',
28 'usu_l_f':'The user or password is incorrect please send me again the comand with the user and password',
29 'welcome':'Hello and welcome to the Reisub Bot',
30 'thiss':'This bot contains some advancend functions of Reisub',
31 'suge':'This user: {} send a sugestion: {}',
32 'urown':'Please give me a link',
33 'urow1':'The link was created',
34 'log_s_f':'Use this sintax:\n/login_f user password',
35 'cls_l_f':'Your publication is clear or very short'}
36
37 def return_string(string,lang):
38         if "es" in lang:
39                 return es[string]
40         elif "en" in lang:
41                 return en[string]
42         else:
43                 return es[string]
44
45 def log_friend(update: Update, context: CallbackContext) -> None:
46         try:
47                 lan=io.open(update.effective_user.username,"r")
48                 lang=lan.read()
49                 lan.close()
50         except:
51                 lang="es"
52         bot.send_chat_action(chat_id=update.message.chat_id, action=telegram.ChatAction.TYPING)
53         print(context.args)
54         if len(context.args)<=1:
55                 update.message.reply_text(return_string('log_s_f',lang))
56         else:
57                 try:
58                         friend=api.FriendApi("https://friendicarg.nsupdate.info/",context.args[0],context.args[1])
59                         crud.connect("friend_users.db")
60                         if len(crud.read("users","telegram",update.message.chat_id))==0:
61                                 crud.create("users","'"+str(update.message.chat_id)+"','"+context.args[0]+"','"+context.args[1]+"'")
62                                 update.message.reply_text(return_string('usa_l_f',lang))
63                         else:
64                                 update.message.reply_text(return_string('usl_l_f',lang))
65                         crud.save();crud.close()
66                 except ValueError:
67                         update.message.reply_text(return_string('usu_l_f',lang))
68         context.bot.delete_message(chat_id = update.message.chat_id, message_id = update.message.message_id)
69
70 def logout_friend(update: Update, context: CallbackContext) -> None:
71         try:
72                 lan=io.open(update.effective_user.username,"r")
73                 lang=lan.read()
74                 lan.close()
75         except:
76                 lang="es"
77         bot.send_chat_action(chat_id=update.message.chat_id, action=telegram.ChatAction.TYPING)
78         crud.connect("friend_users.db")
79         if len(crud.read("users","telegram",update.message.chat_id))==1:
80                 crud.delete("users","telegram",str(update.message.chat_id))
81                 crud.save()
82                 crud.close()
83                 update.message.reply_text(return_string('usd_l_f',lang))
84         else:
85                 update.message.reply_text(return_string('not_l_f',lang))
86 def publish(update: Update, context: CallbackContext) -> None:
87         try:
88                 lan=io.open(update.effective_user.username,"r")
89                 lang=lan.read()
90                 lan.close()
91         except:
92                 lang="es"
93         bot.send_chat_action(chat_id=update.message.chat_id, action=telegram.ChatAction.TYPING)
94         print(lang)
95         crud.connect("friend_users.db")
96         r=crud.read("users","telegram",update.message.chat_id)
97         if len(r)==1:
98                 try:
99                         if '-' in str(update.message.chat_id):
100                                 print('LOGGING DEBUG: RUNNING IN A GROUP, if this work please remove the line 93 of the code')
101                                 friend=api.FriendApi("https://friendicarg.nsupdate.info/",r[0][1],r[0][2])
102                                 contexto=update.message.text
103                                 rex=re.findall("#!.*!#",contexto)
104                                 if rex:
105                                         rex=rex[0].replace("#!","").replace("!#","")
106                                 else:
107                                         rex=""
108                                 a=re.findall('<.*>',contexto)
109                                 if a:
110                                         for i in a:
111                                                 contexto=contexto.replace(i,'[attachment type="link" url="'+i.replace("<","").replace(">","")+'"][/attachment]')
112                                 del(a)
113                                 a=re.findall('&.*&',contexto)
114                                 if a:
115                                         for i in a:
116                                                 contexto=contexto.replace(i,'[img='+i.replace("&","")+'][/img]')
117                                 if "!horacu!" in contexto or "!horacu!" in rex:
118                                         horacuvar=horacu()
119                                         rex.replace("!horacu!",horacuvar)
120                                         contexto.replace("!hora!",horavar).replace("!horacu!",horacuvar)
121                                 if "!hora!" in contexto or "!hora!" in rex:
122                                         horavar=time.strftime('%H:%M:%S')
123                                         contexto.replace("!hora!",horavar).replace("!horacu!",horacuvar)
124                                         rex.replace("!hora!",horavar)
125                                 friend.share(rex,contexto.replace(rex,'').replace('/publish','')+"\n#telegram")
126                                 update.message.reply_text(rex+'\n'+contexto.replace('/publish@reisub_bot','').replace('/publish ','').replace('#!'+rex+'!#',''))
127                                 context.bot.delete_message(chat_id = update.message.chat_id, message_id = update.message.message_id)
128                         else:
129                                 friend=api.FriendApi("https://friendicarg.nsupdate.info/",r[0][1],r[0][2])
130                                 contexto=update.message.text
131                                 rex=re.findall("#!.*!#",contexto)
132                                 if rex:
133                                         rex=rex[0].replace("#!","").replace("!#","")
134                                 else:
135                                         rex=""
136                                 a=re.findall('<.*>',contexto)
137                                 if a:
138                                         for i in a:
139                                                 contexto=contexto.replace(i,'[attachment type="link" url="'+i.replace("<","").replace(">","")+'"][/attachment]')
140                                         del(a)
141                                 a=re.findall('&.*&',contexto)
142                                 if a:
143                                         for i in a:
144                                                 contexto=contexto.replace(i,'[img='+i.replace("&","")+'][/img]')
145                                 if "!horacu!" in contexto or "!horacu!" in rex:
146                                         horacuvar=horacu()
147                                         rex.replace("!horacu!",horacuvar)
148                                         contexto.replace("!hora!",horavar).replace("!horacu!",horacuvar)
149                                 if "!hora!" in contexto or "!hora!" in rex:
150                                         horavar=time.strftime('%H:%M:%S')
151                                         contexto.replace("!hora!",horavar).replace("!horacu!",horacuvar)
152                                         rex.replace("!hora!",horavar)
153                                 friend.share(rex,(contexto.replace(rex,'').replace('/publish','')+"\n#telegram").replace('#!','').replace('!#',''))
154                                 update.message.reply_text(return_string('pub',lang))
155                 except:
156                         update.message.reply_text(return_string('cha_l_f',lang))
157         else:
158                 update.message.reply_text(return_string('not_l_f',lang))
159 def notifications(update:Update,context:CallbackContext):
160         try:
161                 lan=io.open(update.effective_user.username,"r")
162                 lang=lan.read()
163                 lan.close()
164         except:
165                 lang="es"
166         bot.send_chat_action(chat_id=update.message.chat_id, action=telegram.ChatAction.TYPING)
167         crud.connect("friend_users.db")
168         r=crud.read("users","telegram",update.message.chat_id)
169         if len(r)==1:
170                 friend=api.FriendApi("https://friendicarg.nsupdate.info/",r[0][1],r[0][2])
171                 update.message.reply_text(friend.notifications(5))
172         else:
173                 update.message.reply_text(return_string('not_l_f',lang))
174
175 def get_5(update:Update,context:CallbackContext):
176         try:
177                 lan=io.open(update.effective_user.username,"r")
178                 lang=lan.read()
179                 lan.close()
180         except:
181                 lang="es"
182         bot.send_chat_action(chat_id=update.message.chat_id, action=telegram.ChatAction.TYPING)
183         crud.connect("friend_users.db")
184         r=crud.read("users","telegram",update.message.chat_id)
185         if len(r)==1:
186                 friend=api.FriendApi("https://friendicarg.nsupdate.info/",r[0][1],r[0][2])
187                 for i in range(0,4):
188                         update.message.reply_text(friend.network(5)[i].replace('><i class=icon-link icon-large><span class=sr-only',''))
189         else:
190                 update.message.reply_text(return_string('not_l_f',lang))