I implemented a Vim-like replacement command in Slackbot #Python

Vim is good!

Do you guys use Vim? You're using Vim, right? You're not using Vim, right? I use it.

So, Vimmer, do you know Vim's Japanese community? Vim's Japanese community is primarily active on Lingr. So, it's a chat room here, but there are quite a few bots running, and there are a lot of very useful functions. Under such circumstances, here is the function that I thought was the best. Screen Shot 2017-05-21 at 10.04.22.png Isn't this amazing? When you type the Vim replace command, the bot will immediately send you a modified version of the text. (I'm not going to talk about Vim's replace commands, because all programmers are supposed to be Vimmer, because they're all Vimmer, right?)

I've always thought about it. ** "This, I'm crazy about myself and this Slack" ** But I didn't like Slackbot because I had given up in the past. "Maybe I can do it now?" I came up with the idea.

That's why I'm going to make it.

The source will be placed at ko ↑ ko ↓.

The sites that I referred to are as follows.

-Create a Slack bot with Python's slackbot library -Make Slackbot with Python

How to make

Preparation

This time, I referred to this article. First of all, [this area](http://qiita.com/sukesuke/items/1ac92251def87357fdf6#slackbot%E3%81%AE%E5%88%9D%E6%9C%9F%E8%A8%AD%E5% Please prepare up to AE% 9A). After finishing so far, I would like to make this replacement command.

Replace command

First, create a file called bot_listen.py in the plugin directory.

The contents are like this.

bot_listen.py


from slackbot.bot import listen_to
import re

pastmsg = ''
pastmsg2 = ''

@listen_to(r'.+')
def savemsg(message):
    global pastmsg2
    global pastmsg
    pastmsg2 = pastmsg
    pastmsg = message.body['text']

@listen_to(r'^s/+\S+/+\S+/$')
def replace(message):
    before = re.findall(r'^s/(.*)/+\S+/', pastmsg)
    after = re.findall(r'^s/+\S+/(.*)/', pastmsg)
    if before[0] in pastmsg2:
        message.send('「' + pastmsg2.replace(before[0], after[0]) + 'I wanted to say')
    else:
        message.send('In the previous sentence "' + before[0] + 'Is not included?')

What I'm doing is

--Save the last two messages in pastmsg and pastmsg2 --If there is a statement in the form of s / something 1 / something 2 /, it will be processed. --Substitute a list of something 1 strings for before and a list of something 2 for ʻafter ――If there issomething 1 in the previous message, replace something 1withsomething 2` and bot will tell you the result. --If it doesn't match, bot will make you foolish

It's not a big deal if you can use regular expressions.

Task

--The storage of the last two messages is dirty --There seems to be a way to get it with standard functions --Since it only responds to the sentence immediately before the replace command, if another user speaks first after you speak and notice the typographical error, the replacement will not work. ――It may be good to save the latest two for each user ↑ ↓ ↓ per associative array ――Get it with standard function h (ry) --Anyway, there is only one before and one ʻafter`, so it doesn't have to be a list, but I didn't know how to get the matched strings. -[Gugure](http://dic.nicovideo.jp/a/ Gugurekas)

Recommended Posts

I implemented a Vim-like replacement command in Slackbot #Python
Hit a command in Python (Windows)
A simple HTTP client implemented in Python
I made a payroll program in Python!
I implemented Cousera's logistic regression in Python
A note I looked up to make a command line tool in Python
I implemented Robinson's Bayesian Spam Filter in python
I want to create a window in Python
I tried playing a typing game in Python
I wrote a class in Python3 and Java
[Memo] I tried a pivot table in Python
I implemented the inverse gamma function in python
I tried adding a Python3 module in C
I made a Caesar cryptographic program in Python.
Implemented SimRank in Python
I implemented Python Logging
Implemented Shiritori in Python
Slackbot development in Python
I implemented breadth-first search in python (queue, drawing self-made)
I want to embed a variable in a Python string
I want to easily implement a timeout in python
Specify a subcommand as a command line argument in Python
I want to write in Python! (2) Let's write a test
Make a simple Slackbot with interactive button in python
I tried to implement a pseudo pachislot in Python
I want to randomly sample a file in Python
I want to work with a robot in python.
I made a prime number generation program in Python 2
How to execute a command using subprocess in Python
Take a screenshot in Python
I wrote python in Japanese
Create a function in Python
Create a dictionary in Python
Execute external command in python
Make a bookmarklet in Python
Sudoku solver implemented in Python 3
I made a python text
External command execution in Python
I understand Python in Japanese!
Draw a heart in Python
What I learned in Python
6 Ball puzzle implemented in python
I made a simple typing game with tkinter in Python
A general-purpose program that formats Linux command strings in python
I tried to implement a one-dimensional cellular automaton in Python
I made a quick feed reader using feedparser in Python
I made a command to generate a table comment in Django
I tried "a program that removes duplicate statements in Python"
I tried "How to get a method decorated in Python"
[MQTT / Python] Implemented a class that does MQTT Pub / Sub in Python
[python] Manage functions in a dictionary (command table, function table, function pointer)
I created a class in Python and tried duck typing
I made a puzzle game (like) with Tkinter in Python
I tried to make a stopwatch using tkinter in python
I implemented Donald Knuth's unbiased sequential calculation algorithm in Python
I want to make input () a nice complement in python
I get a UnicodeDecodeError in mecab-python3
Maybe in a python (original title: Maybe in Python)
Write a binary search in Python
[python] Manage functions in a list
Implemented image segmentation in python (Union-Find)