Compare commits

...

8 Commits

Author SHA1 Message Date
149ab033b0 Add player count 2019-01-29 11:47:31 +05:30
f3b74b65ed Fix /list 2019-01-29 11:11:19 +05:30
17eb4e8bdd Fix replies to non-chat messages by bot 2019-01-29 10:19:56 +05:30
7bdebb7fd3 Move join/leave/chat messages to mod instead of parsing stderr 2019-01-29 09:57:16 +05:30
bb9a9f7a9b Remove list from join/leave messages 2019-01-29 03:03:45 +05:30
0434bf5854 Add /time 2019-01-29 02:57:31 +05:30
0238610cd1 Add /list 2019-01-29 02:05:39 +05:30
c4afd9139e Add death messages 2019-01-29 01:39:16 +05:30
3 changed files with 72 additions and 31 deletions

View File

@@ -5,7 +5,7 @@ import logging
import requests import requests
import re import re
from emoji import demojize from emoji import demojize
from telegram.ext import Updater, MessageHandler, Filters from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - \ logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - \
%(message)s', level=logging.INFO) %(message)s', level=logging.INFO)
@@ -16,7 +16,8 @@ def get_text_or_type(msg, bot, truncate=False):
# Set message contents # Set message contents
if msg.text: if msg.text:
if msg.from_user.username == bot.username: if msg.from_user.username == bot.username and re.match('<.*?>(.*)',
msg.text):
x = re.compile('(<.*?>)(.*)') x = re.compile('(<.*?>)(.*)')
y = x.search(msg.text) y = x.search(msg.text)
z = y.group(2) z = y.group(2)
@@ -59,7 +60,8 @@ def get_text_or_type(msg, bot, truncate=False):
# Set sender # Set sender
return_message = "" return_message = ""
for i in message.strip().split("\n"): for i in message.strip().split("\n"):
if msg.from_user.username != bot.username: if msg.from_user.username != bot.username or not re.match('<.*?>(.*)',
msg.text):
if msg.from_user.username: if msg.from_user.username:
return_message += f"@{msg.from_user.username}: " return_message += f"@{msg.from_user.username}: "
else: else:
@@ -87,7 +89,18 @@ def message_handling(bot, update):
data=params) data=params)
def direct_cmd(bot, update, text):
if update.message.chat.id in config.groups:
params = {"message": text}
requests.post("http://localhost:" + str(config.port) + "/post",
data=params)
updater = Updater(token=config.api_key) updater = Updater(token=config.api_key)
dispatcher = updater.dispatcher dispatcher = updater.dispatcher
dispatcher.add_handler(CommandHandler('list', lambda x, y: direct_cmd(x, y,
"list")))
dispatcher.add_handler(CommandHandler('time', lambda x, y: direct_cmd(x, y,
"time")))
dispatcher.add_handler(MessageHandler(Filters.all, message_handling)) dispatcher.add_handler(MessageHandler(Filters.all, message_handling))
updater.start_polling() updater.start_polling()

View File

@@ -1,30 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from telegram import Bot
import sys import sys
import re import re
import config
from emoji import emojize
bot = Bot(token=config.api_key)
for line in sys.stdin: for line in sys.stdin:
text = ""
parse_mode = None
if not re.match(".* Mod performs HTTP request with URL ", line): if not re.match(".* Mod performs HTTP request with URL ", line):
print(line, end="") print(line, end="")
if re.match(r".*ACTION\[Server\]: CHAT", line):
text = re.sub(".*?CHAT: ", "", line)
elif re.match(r".*?(\w+ (leaves|joins) game.*)", line):
text = "<code>"
text += re.sub(r".*?(\w+ (leaves|joins) game\.).*", r"\1", line)
text += "\nList of players: \n"
text += "\n".join(re.sub(r".*?: ", "", line).split(" "))
text += "</code>"
parse_mode = "HTML"
if text != "":
print(text)
for i in config.groups:
if parse_mode:
bot.send_message(chat_id=i, text=emojize(text),
parse_mode=parse_mode)
else:
bot.send_message(chat_id=i, text=emojize(text))

View File

@@ -1,21 +1,72 @@
local http_api = minetest.request_http_api()
function tablelength(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
function send_messages(res) function send_messages(res)
for message in string.gmatch(res["data"], "[^\n]+") do for message in string.gmatch(res["data"], "[^\n]+") do
minetest.chat_send_all(message) if message == "list" then
local x = minetest.get_connected_players()
local text = "List of Players ("
text = text .. tostring(tablelength(x)) .. "/"
text = text .. tostring(minetest.settings:get("max_users")) .. "):"
for j, i in ipairs(x) do
text = text .. "\n" .. i.get_player_name(i)
end
http_api.fetch_async({url = "http://localhost:" .. port .. "/sendCode",
post_data = {message = text}})
elseif message == "time" then
local x = minetest.get_timeofday()
local text = string.format("%02d:%02d", math.floor(x*24), math.floor((x*24*60) % 60))
http_api.fetch_async({url = "http://localhost:" .. port .. "/sendCode",
post_data = {message = text}})
else
minetest.chat_send_all(message)
end
end end
end end
function main(http_api) function main()
port = minetest.settings:get("chat_server_port") port = minetest.settings:get("chat_server_port")
if port == nil then if port == nil then
port = "9876" port = "9876"
end end
http_api.fetch({url = "http://localhost:" .. port .. "/get"}, send_messages) http_api.fetch({url = "http://localhost:" .. port .. "/get"}, send_messages)
minetest.after(0.5, main, http_api) minetest.after(0.5, main)
end
function dead(player)
http_api.fetch_async({url = "http://localhost:" .. port .. "/sendCode",
post_data = {message = player.get_player_name(player) ..
" died."}})
end
function join(player)
http_api.fetch_async({url = "http://localhost:" .. port .. "/sendCode",
post_data = {message = player.get_player_name(player) ..
" joined the server."}})
end
function left(player)
http_api.fetch_async({url = "http://localhost:" .. port .. "/sendCode",
post_data = {message = player.get_player_name(player) ..
" left the server."}})
end
function chat(name, message)
http_api.fetch_async({url = "http://localhost:" .. port .. "/send",
post_data = {message = "<" .. name .. "> " .. message}})
end end
local http_api = minetest.request_http_api()
if http_api == nil then if http_api == nil then
minetest.log("error", "Chat Mod is not in secure.http_mods or secure.trusted_mods. Exiting.") minetest.log("error", "Chat Mod is not in secure.http_mods or secure.trusted_mods. Exiting.")
else else
main(http_api) main()
end end
minetest.register_on_dieplayer(dead)
minetest.register_on_joinplayer(join)
minetest.register_on_leaveplayer(left)
minetest.register_on_chat_message(chat)