-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWord_Swap.py
70 lines (65 loc) · 2.24 KB
/
Word_Swap.py
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
"""
Word swap module
"""
import re
import logging
import html
import difflib
from telegram import Bot, Update
from telegram.ext import Filters, MessageHandler, Updater
import core
PLUGINVERSION = 2
from difflib import Differ
def appendBoldChanges(s1, s2):
"""
Adds <b></b> tags to words that are changed
https://stackoverflow.com/a/10775310
"""
l1 = s1.split(' ')
l2 = s2.split(' ')
dif = list(Differ().compare(l1, l2))
return " ".join(['<b>'+i[2:]+'</b>' if i[:1] == '+' else i[2:] for i in dif
if not i[:1] in '-?'])
# Always name this variable as `plugin`
# If you dont, module loader will fail to load the plugin!
plugin = core.Plugin()
@plugin.command(command="/s/",
description="Swaps word in message",
inline_supported=True,
hidden=False)
def wordsw(bot: Bot, update: Update, user, args):
"""
Example usage:
User A
Hi
User B
[In reply to User A]
/s/Hi/Bye
OctoBot
Hello, User A
Did you mean:
Bye
"""
msg = update.message
txt = msg.text
if msg.reply_to_message is not None:
if txt.startswith("/s"):
if not msg.reply_to_message.from_user.name == bot.getMe().name:
origword = txt[2]
swap = txt[3]
groups = [b.replace("\/", "/") for b in re.split(r"(?<!\\)/", txt)]
find = groups[2]
replacement = groups[3]
if len(groups) > 4:
flags = groups[4]
else:
flags = ""
if len(origword) > 0:
find_re = re.compile("{}{}".format("(?{})".format(flags.replace("g", "")) if flags.replace("g", "") != "" else "", find))
mod_msg = html.escape(find_re.sub(replacement, msg.reply_to_message.text, count=int("g" not in flags)))
mod_msg = appendBoldChanges(msg.reply_to_message.text, mod_msg)
text = "Hello, {}\nDid you mean:\n{}".format(
msg.reply_to_message.from_user.first_name,
mod_msg
)
return core.message(text=text, parse_mode="HTML")