-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
178 lines (134 loc) · 5.5 KB
/
bot.py
File metadata and controls
178 lines (134 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
from telegram.ext import Updater, CommandHandler, MessageHandler, ConversationHandler , Filters
import time as t
import csv
import pandas as pd
import os
from _configure import TOKEN
from git_update import git_update
########################### global variables#################################
# fUNTION NECESITIES
WRITING , W1 = range(2) #
# FILE = 'memories.md'
DIR_PHOTO ='media/'
DIR_TEXT ='texts/'
UDF = 'user_data.csv' # user data file, name
# Messages
HELP = """Hello, here you can store your more relevant (at least for you) memories, any kind of feelings.
The instructions are:
/start basic bot initialization
/help shows this beautiful message
/hi to start writing your memories
/done to close your memories
/git to upload your result
"""
START = """Oh dear, I look forward to hearing from you :) ESTO ES CREEPY PERO BUENO
Please tell me whatever you want, I am an open book"""
END_W = """Reading you has been a pleasure!"""
######################### basic calls #########################
def setup (bot , update ):
"""Create or upload de user informatión a redstribute to his count
user_data.csv; structure: id, first name , username
"""
user_id = update.message.from_user.id
first_name = update.message.from_user.first_name
user_name = update.message.from_user.username
data = pd.read_csv(UDF)
# check if is a new user, in this case create it count
if not True in list(data['id'] == user_id):
# add to list
os.system( f'touch {user_name}')
with open(UDF, 'a', newline='') as csvfile: # cambio w por a
fieldnames = ['id', 'first name' , 'username' ]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
#writer.writeheader()
writer.writerow({'id': user_id, 'first name': first_name , 'username': user_name})
#writer.close())
#create its file
update.message.reply_text(f""" I see it is your first time here {first_name}, I am going to give you an introduction:
{HELP}""")
else:
update.message.reply_text( f""" Hello , {first_name} is a pleasure to see you again""" )
def help_ms(bot , update):
""" string -> void
Show help message
"""
update.message.reply_text(HELP)
def start( bot , update ):
"""Show a message that mean that you have started
"""
#file management # AÑADIR CONTROL DE LA FECHA
global DATA
global modified
FILE = str(update.message.from_user.username)+'.md'
print(f'Procedamso a ver si se puede abrir el archivo {FILE}')
with open(FILE , 'r') as original:
DATA = original.read()
print('se ha abierto el archivo')
original.close()
modified = open( FILE , 'w')
modified.write(f'\n# {t.strftime("%A")} {t.strftime("%d")} {t.strftime("%m")} {t.strftime("%y")} a las {t.strftime("%H:%M:%S")} \n ' ) # Imprimo la fecha t.strftime('%y-%m-%d_%a_%H_%M_%S')
update.message.reply_text(START)
return WRITING
def write1(bot , update):
""" repeat the message you have write
"""
file_text = update.message.text
update.message.reply_text( f' OMG ')
modified.write(file_text + '\n')
return WRITING
def write(bot , update):
""" repeat the message you have write
"""
file_text = update.message.text
update.message.reply_text( f' Ok ')
modified.write(file_text + '\n')
return W1
def photo (bot , update):
""" manage photos"""
photo = update.message.photo[-1]
# Preguntar por el nombre de la foto y modificar (interactive_tools.py)
name = t.strftime('%y-%m-%d_%a_%H_%M_%S')
path = DIR+name
#gestión de carpetas
file_id = photo.file_id
file_down = bot.get_file(file_id)
file_down.download(path)
modified.write(f'\n')
update.message.reply_text( f'Su fotillo {name} ha sido descargada en la carpeta {DIR} ')
return W1
def my_finish (bot , update):
"""end conversation
"""
#file managemet
modified.write(DATA)
modified.close()
update.message.reply_text( END_W)
return ConversationHandler.END
def git ( bot , update ):
""" update to git """
update.message.reply_text('Subido a git con éxito (suponiendo que no lo tuvieras como comentario) '
)
git_update( f"Diario del día {t.strftime('%y-%m-%d_%a_%H_%M_%S')}", add=f'{update.message.from_user.username}.md', path='~/repositorios/BlancaCC.github.io/' )
######################## main #####################################
def main():
updater = Updater(TOKEN)
# my calls
updater.dispatcher.add_handler(CommandHandler("start", setup ))
updater.dispatcher.add_handler(CommandHandler('help', help_ms))
updater.dispatcher.add_handler(CommandHandler('git', git ))
conv_handler = ConversationHandler(
entry_points=[ CommandHandler('hi', start) ],
# both states has the same options, but for design reason the need to coexist :)
states={
WRITING: [ MessageHandler( Filters.text , write) , MessageHandler( Filters.photo , photo) ], # add inside Fiters.phto, video, audio...
W1:[ MessageHandler( Filters.text , write1) , MessageHandler( Filters.photo , photo)]
},
fallbacks=[ CommandHandler('done', my_finish) , CommandHandler('help', help_ms),CommandHandler("start", help_ms) ]
)
# AÑADIR MODO UPDATE PARA SUBIR ACTUALIZACIÓN A GIT (estaría guay que te enviara un enlace, para así poder verlo desde la página wed)
updater.dispatcher.add_handler(conv_handler)
# basic coniguration
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()