Try to make BOT by linking spreadsheet and Slack with python 2/2 (python + gspread + slackbot)

Introduction

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.

Thing you want to do

Write the code

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

Things to watch out for

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.

at the end

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

Try to make BOT by linking spreadsheet and Slack with python 2/2 (python + gspread + slackbot)
Try to make BOT by linking spreadsheet and Slack with python 1/2 (python + gspread + slackbot)
Fractal to make and play with Python
Try to make foldl and foldr with Python: lambda. Also time measurement
Image characters and post to slack (python slackbot)
Try to make a "cryptanalysis" cipher with Python
Try to make a dihedral group with Python
WEB scraping with python and try to make a word cloud from reviews
Try to make a command standby tool with python
Try to operate DB with Python and visualize with d3
Read and write files with Slackbot ~ Bot development with Python ~
[Python] Sort spreadsheet worksheets by sheet name with gspread
Post to slack with Python 3
Try to make it using GUI and PyQt in Python
Send experiment results (text and images) to slack with Python
Try to bring up a subwindow with PyQt5 and Python
I tried to make GUI tic-tac-toe with Python and Tkinter
Try to operate Facebook with Python
How to make a slack bot
[Machine learning] Try running Spark MLlib with Python and make recommendations
Linking Python and Arduino to display IME On / Off with LED
How to make a surveillance camera (Security Camera) with Opencv and Python
I tried to make a periodical process with Selenium and Python
Try to display google map and geospatial information authority map with python
[Introduction] I want to make a Mastodon Bot with Python! 【Beginners】
Throw something to Kinesis with python and make sure it's in
Try to make capture software with as high accuracy as possible with python (1)
Try to implement and understand the segment tree step by step (python)
[Python] Try to recognize characters from images with OpenCV and pyocr
[TCP / IP] After studying, try to make an HTTP client-like with Python
Try to reproduce color film with Python
Try logging in to qiita with Python
Put Cabocha 0.68 on Windows and try to analyze the dependency with Python
Python beginners decided to make a LINE bot with Flask (Flask rough commentary)
Try converting latitude / longitude and world coordinates to each other with python
Try to make a capture software with as high accuracy as possible with python (2)
Try to build python and anaconda environment on Mac (by pyenv, conda)
Linking python and JavaScript with jupyter notebook
[Python] Try to make a sort program by yourself. (Selection sort, insertion sort, bubble sort)
Let's make a Twitter Bot with Python!
I tried to make a periodical process with CentOS7, Selenium, Python and Chrome
Add conversation function to slack bot (made by python) using Recruit's Talk API
Python + chatwork + google extension = How to make an easy and funny chat BOT
If you want to make a discord bot with python, let's use a framework
Try to make your own AWS-SDK with bash
MessagePack-Try to link Java and Python with RPC
Try running Google Chrome with Python and Selenium
Try to solve the man-machine chart with Python
Try to draw a life curve with python
I want to make a game with Python
Try to communicate with EV3 and PC! (MQTT)
Try to automatically generate Python documents with Sphinx
Sample to send slack notification with python lambda
Steps to create a Twitter bot with python
[Python] Troubleshooting before accessing Google Spreadsheet with gspread
Try to make client FTP fastest with Pythonista
[Python] Mention to multiple people with Slack API
Flow to complete Slack authentication with Flask (Python)
Try to detect fish with python + OpenCV2.4 (unfinished)
[# 1] Make Minecraft with Python. ~ Preliminary research and design ~
I tried to make the weather forecast on the official line by referring to the weather forecast bot of "Dialogue system made with python".