In Previous article, I implemented it to the point that if you send a word pre-registered in a spreadsheet on Slack to BOT, the meaning will be returned.
This time is the continuation.
Word registration
Output list of registered words
Delete word
Complete all of this on Slack
Only add code to my_mention.py.
my_mention.py
@respond_to('Registration:(.*)')
def mention_func2(message, entry_word):
entry_list = entry_word.split()
entry_len = len(entry_list)
values_list = worksheet.col_values(1)
if entry_len == 2:
gyou = len(values_list) #Get the number of rows in the selected worksheet
worksheet.update_cell(gyou+1, 1, entry_list[0])
worksheet.update_cell(gyou+1, 2, entry_list[1])
message.reply('「'+ entry_word +'」'+'Has been registered.')
else:
message.reply('Register the word and its meaning at the same time. Example → PC PC') #Mention
@respond_to('List')
def mention_func3(message):
values_list = worksheet.col_values(1)
message.reply('The following words have been registered so far. →→→→' + '、'.join(values_list))
@respond_to('Delete:(.*)')
def mention_func4(message, delete_word):
values_list = worksheet.col_values(1)
tof = delete_word in values_list
if tof == True:
cells = worksheet.find(delete_word) #Get coordinates that match the input result
worksheet.delete_row(cells.row) #Delete the selected row
message.reply('「' + delete_word + '」'+'Has been deleted.')
else:
message.reply('I tried to delete it, but I couldn't find it. Please send "List" to check.') #Mention
I wanted to enter the word and meaning at the same time and register, so when I send "Registration: ○○ △△", write ○○ as the word and △△ as the meaning in the spreadsheet. Use split () to pass a split list of ○○ and △△ to entry_list.
Check entry_len to see if there are two elements stored in entry_list to avoid entering only one in the spreadsheet if you make a registration mistake.
Once you have two elements in place, work with the spreadsheet and store it in an empty row.
After all, I think that I want to write a little smarter around here, but beginners like me are all ages even if I can implement it ... Please let me know if you have any improvement plans.
message.reply doesn't seem to support the output of the list itself, so use join () to convert it to a string. ',' Means to separate elements with ",".
It works like a search. There was nothing particularly clogged up.
Through this BOT implementation, I learned a lot about python and its surrounding libraries. I've been wondering how it works with Slack, so I'm going to implement something different when I feel like it again.
Recommended Posts