Last active
May 25, 2018 18:51
-
-
Save sash13/18e62158fbddced05f46aeeae4c0e2d8 to your computer and use it in GitHub Desktop.
s@bot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: windows-1251 -*- | |
from matrix_bot_api.matrix_bot_api import MatrixBotAPI | |
from matrix_bot_api.mregex_handler import MRegexHandler | |
from matrix_bot_api.mcommand_handler import MCommandHandler | |
import random | |
import re | |
from functools import partial | |
import logging | |
import sys | |
logging.basicConfig( | |
filename='reduplicator.log', | |
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', | |
level=logging.INFO | |
) | |
logger = logging.getLogger('matrix_client.client') | |
# Global variables | |
NAME = "Chirno" | |
USERNAME = "bot" # Bot's username | |
PASSWORD = "" # Bot's password | |
SERVER = "https://localhost" # Matrix server URL | |
users = {} | |
def hi_callback(room, event): | |
# Somebody said hi, let's say Hi back | |
room.send_text("Привет, " + event['sender']) | |
def echo_callback(room, event): | |
args = event['content']['body'].split() | |
args.pop(0) | |
# Echo what they said back | |
room.send_text(' '.join(args)) | |
def reduplic_callback(room, event): | |
args = event['content']['body'].split() | |
args.pop(0) | |
out = ' '.join(args).encode('windows-1251') | |
print 'reduplicate', room, args, event, ' out',out | |
regex = re.compile('[а-яА-Я]*') | |
replacer = partial(reduplicate_word) | |
message = regex.sub(replacer, out) | |
global users | |
if event['sender'] not in users: | |
print 'add users' | |
new_users = room.get_joined_members() | |
for us in new_users: | |
users[us.user_id] = us.displayname | |
print users | |
message = re.sub('^@{} '.format(event['sender']), '', message, flags=re.I) | |
print message | |
room.send_html('<a href=\"https://matrix.to/#/'+event['sender']+'\">'+users[event['sender']]+'</a>: '+message.decode('windows-1251', 'ignore')) | |
def reduplicate_word(match_object, repeat=True): | |
vowels = 'аеёиоуыэюя' | |
vowel_map = { | |
'а': 'я', | |
'о': 'ё', | |
'у': 'ю', | |
'э': 'е', | |
'ы': 'и' | |
} | |
word = match_object.group(0) | |
def replacer(inner_match_object): | |
if not inner_match_object.end(0) == len(word): | |
letter = inner_match_object.group(0)[-1].lower() | |
replaced = 'ху{}'.format(vowel_map.get(letter, letter)) | |
if inner_match_object.group(0)[0].isupper(): | |
replaced = replaced.capitalize() | |
if repeat: | |
replaced = '{}-{}'.format(word, replaced) | |
return replaced | |
regex = re.compile('[^{}]*([{}])'.format(vowels, vowels), flags=re.I+re.UNICODE) | |
return regex.sub(replacer, word, 1) or word | |
def main(): | |
# Create an instance of the MatrixBotAPI | |
bot = MatrixBotAPI(USERNAME, PASSWORD, SERVER) | |
# Add a regex handler waiting for the word Hi | |
hi_handler = MRegexHandler(NAME, reduplic_callback) | |
bot.add_handler(hi_handler) | |
# Start polling | |
bot.start_polling() | |
# Infinitely read stdin to stall main thread while the bot runs in other threads | |
while True: | |
input() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment