New Feature! Added bot replies's lang support

This commit is contained in:
spla 2022-08-09 11:16:26 +02:00
pare 883935d643
commit d53c18b442
S'han modificat 5 arxius amb 66 adicions i 28 eliminacions

Veure arxiu

@ -15,6 +15,7 @@ The bot will process any of the above keywords thanks to the wrapper for ejabber
- admin_account: the ejabberd admin account, in exemple admin@ejabberd.server - admin_account: the ejabberd admin account, in exemple admin@ejabberd.server
- admin_pass: ejabberd admin account password - admin_pass: ejabberd admin account password
- Akkoma hostname: in ex. akkoma.host - Akkoma hostname: in ex. akkoma.host
- bot's replies language (ca or en)
# Requirements # Requirements
@ -32,6 +33,8 @@ Before running `python xmpp.py`:
Enjoy! Enjoy!
## 9.8.2022 New feature! Added lang support! bot replies's lang is configured during setup. Only ca or en are actually supported.

Veure arxiu

@ -16,7 +16,6 @@ import sys
import six import six
from decorator import decorate from decorator import decorate
import hashlib import hashlib
import pdb
### ###
# Version check functions, including decorator and parser # Version check functions, including decorator and parser

Veure arxiu

@ -6,7 +6,6 @@ import fileinput,re
import os import os
import sys import sys
import os.path import os.path
import pdb
### ###
# Dict helper class. # Dict helper class.
@ -42,6 +41,12 @@ class Akkomabot:
self.akkoma, self.akkoma_hostname = self.log_in(self) self.akkoma, self.akkoma_hostname = self.log_in(self)
bot_file_path = "config/config.txt"
self.bot_lang = self.get_parameter("bot_lang" , bot_file_path)
self.load_strings(self)
else: else:
while(True): while(True):
@ -54,6 +59,12 @@ class Akkomabot:
else: else:
bot_file_path = "config/config.txt"
self.bot_lang = self.get_parameter("bot_lang" , bot_file_path)
self.load_strings(self)
break break
@staticmethod @staticmethod
@ -102,9 +113,10 @@ class Akkomabot:
sys.exit("Bye") sys.exit("Bye")
user_name = input("User name, ex. john? ") user_name = input("Bot user name, ex. john? ")
user_password = getpass.getpass("User password? ") user_password = getpass.getpass("Bot password? ")
app_name = input("App name? ") app_name = input("Bot App name? ")
self.bot_lang = input("Bot replies lang (ca or en)? ")
client_id, client_secret = Akkoma.create_app( client_id, client_secret = Akkoma.create_app(
app_name, app_name,
@ -221,6 +233,28 @@ class Akkomabot:
print(f'{file_path} Missing parameter {parameter}') print(f'{file_path} Missing parameter {parameter}')
sys.exit(0) sys.exit(0)
@staticmethod
def load_strings(self):
lang_file_path = f"app/locales/{self.bot_lang}.txt"
self.register_str = self.get_parameter("register_str", lang_file_path)
self.unregister_str = self.get_parameter("unregister_str", lang_file_path)
self.stats_str = self.get_parameter("stats_str", lang_file_path)
self.registerok_str = self.get_parameter("registerok_str", lang_file_path)
self.user_str = self.get_parameter("user_str", lang_file_path)
self.password_str = self.get_parameter("password_str", lang_file_path)
self.server_str = self.get_parameter("server_str", lang_file_path)
self.xmpp_account_str = self.get_parameter("xmpp_account_str", lang_file_path)
self.notdeleteadmin_str = self.get_parameter("notdeleteadmin_str", lang_file_path)
self.deleted_str = self.get_parameter("deleted_str", lang_file_path)
self.stats_title_str = self.get_parameter("stats_title_str", lang_file_path)
self.registered_users_str = self.get_parameter("registered_users_str", lang_file_path)
self.users_online_str = self.get_parameter("users_online_str", lang_file_path)
self.node_users_str = self.get_parameter("node_users_str", lang_file_path)
self.uptime_str = self.get_parameter("uptime_str", lang_file_path)
self.processes_str = self.get_parameter("processes_str", lang_file_path)
@staticmethod @staticmethod
def modify_file(self, file_name, pattern,value=""): def modify_file(self, file_name, pattern,value=""):
@ -253,6 +287,7 @@ class Akkomabot:
the_file.write('akkoma_hostname: \n') the_file.write('akkoma_hostname: \n')
print(f"adding parameter 'akkoma_hostname' to {self.config_filepath}") print(f"adding parameter 'akkoma_hostname' to {self.config_filepath}")
print(f"adding parameter 'bot_lang' to {self.config_filepath}")
@staticmethod @staticmethod
def read_config_line(self): def read_config_line(self):
@ -261,6 +296,7 @@ class Akkomabot:
line = fp.readline() line = fp.readline()
self.modify_file(self, self.config_filepath, "akkoma_hostname: ", value=self.akkoma_hostname) self.modify_file(self, self.config_filepath, "akkoma_hostname: ", value=self.akkoma_hostname)
self.modify_file(self, self.config_filepath, "bot_lang: ", value=self.bot_lang)
def get_data(self, notif): def get_data(self, notif):
@ -319,7 +355,7 @@ class Akkomabot:
#keyword_length = 8 #keyword_length = 8
keyword = question keyword = question
if keyword == 'registre' or keyword == 'baixa' or keyword == 'info': if keyword == self.register_str or keyword == self.unregister_str or keyword == self.stats_str:
keyword_length = len(keyword) keyword_length = len(keyword)

Veure arxiu

@ -5,7 +5,6 @@ import string
import getpass import getpass
import secrets import secrets
from collections import OrderedDict from collections import OrderedDict
import pdb
### ###
# Dict helper class. # Dict helper class.
@ -86,11 +85,13 @@ class Ejabberd:
is_unregistered = False is_unregistered = False
is_admin = False
if username+'@'+host == self.admin_account: if username+'@'+host == self.admin_account:
message = "ets l'admin, no puc esborrar el teu compte!" is_admin = True
return (is_unregistered, message) return (is_unregistered, is_admin)
data = {'user':username, data = {'user':username,
'host':self.local_vhost, 'host':self.local_vhost,
@ -102,9 +103,7 @@ class Ejabberd:
is_unregistered = response.ok is_unregistered = response.ok
message = "eliminat amb èxit!" return (is_unregistered, is_admin)
return (is_unregistered, message)
def stats(self): def stats(self):

33
xmpp.py
Veure arxiu

@ -1,6 +1,5 @@
from akkomabot import Akkomabot from akkomabot import Akkomabot
from ejabberdapi import Ejabberd from ejabberdapi import Ejabberd
import pdb
# main # main
@ -26,7 +25,7 @@ if __name__ == '__main__':
if mention.reply and '@' not in mention.acct: if mention.reply and '@' not in mention.acct:
if mention.question == 'registre': if mention.question == bot.register_str:
password = ejabberd.generate_pass() password = ejabberd.generate_pass()
@ -34,9 +33,9 @@ if __name__ == '__main__':
if is_registered: if is_registered:
post = f"@{mention.acct} compte xmpp registrat amb èxit!\n\nusuari: {mention.acct}@{bot.akkoma_hostname}\n" post = f"@{mention.acct} {bot.registerok_str}\n\n{bot.user_str} {mention.acct}@{bot.akkoma_hostname}\n"
post += f"contrasenya: {password}\nservidor: {bot.akkoma_hostname}" post += f"{bot.password_str} {password}\n{bot.server_str} {bot.akkoma_hostname}"
bot.akkoma.status_post(post, in_reply_to_id=mention.status_id, visibility='direct') bot.akkoma.status_post(post, in_reply_to_id=mention.status_id, visibility='direct')
@ -44,33 +43,35 @@ if __name__ == '__main__':
bot.akkoma.status_post(f'@{mention.acct}, {text}', in_reply_to_id=mention.status_id, visibility='direct') bot.akkoma.status_post(f'@{mention.acct}, {text}', in_reply_to_id=mention.status_id, visibility='direct')
elif mention.question == 'baixa': elif mention.question == bot.unregister_str:
is_unregistered, message = ejabberd.unregister(mention.acct, bot.akkoma_hostname) is_unregistered, is_admin = ejabberd.unregister(mention.acct, bot.akkoma_hostname)
if is_unregistered: if is_unregistered:
bot.akkoma.status_post(f"@{mention.acct}, compte xmpp {mention.acct}@{bot.akkoma_hostname}: {message}", in_reply_to_id=mention.status_id, visibility='direct') bot.akkoma.status_post(f"@{mention.acct}, {bot.xmpp_account_str} {mention.acct}@{bot.akkoma_hostname}: {bot.deleted_str}", in_reply_to_id=mention.status_id, visibility='direct')
else: elif not is_unregistered:
bot.akkoma.status_post(f'@{mention.acct}, {message}', in_reply_to_id=mention.status_id, visibility='direct') if is_admin:
elif mention.question == 'info': bot.akkoma.status_post(f'@{mention.acct}, {bot.notdeleteadmin_str}', in_reply_to_id=mention.status_id, visibility='direct')
elif mention.question == bot.stats_str:
stats = ejabberd.stats() stats = ejabberd.stats()
post = f'@{mention.acct}, estadístiques del node #xmpp a {bot.akkoma_hostname}:\n\n' post = f'@{mention.acct}, {bot.stats_title_str} {bot.akkoma_hostname}:\n\n'
post += f'usuaris registrats: {stats.registeredusers}\n' post += f'{bot.registered_users_str} {stats.registeredusers}\n'
post += f'usuaris en línia: {stats.onlineusers}\n' post += f'{bot.users_online_str} {stats.onlineusers}\n'
post += f'usuaris del node: {stats.onlineusersnode}\n' post += f'{bot.node_users_str} {stats.onlineusersnode}\n'
post += f'temps en línia (uptime): {stats.uptimeseconds}\n' post += f'{bot.uptime_str} {stats.uptimeseconds}\n'
post += f'processos: {stats.processes}\n' post += f'{bot.processes_str} {stats.processes}\n'
bot.akkoma.status_post(post, in_reply_to_id=mention.status_id, visibility=mention.visibility) bot.akkoma.status_post(post, in_reply_to_id=mention.status_id, visibility=mention.visibility)