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. 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
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.
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 is
something 1 in the previous message, replace
something 1with
something 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.
--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