Compare commits
8 Commits
a9ed7aa159
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 149ab033b0 | |||
| f3b74b65ed | |||
| 17eb4e8bdd | |||
| 7bdebb7fd3 | |||
| bb9a9f7a9b | |||
| 0434bf5854 | |||
| 0238610cd1 | |||
| c4afd9139e |
19
bot/bot.py
19
bot/bot.py
@@ -5,7 +5,7 @@ import logging
|
||||
import requests
|
||||
import re
|
||||
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 - \
|
||||
%(message)s', level=logging.INFO)
|
||||
@@ -16,7 +16,8 @@ def get_text_or_type(msg, bot, truncate=False):
|
||||
|
||||
# Set message contents
|
||||
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('(<.*?>)(.*)')
|
||||
y = x.search(msg.text)
|
||||
z = y.group(2)
|
||||
@@ -59,7 +60,8 @@ def get_text_or_type(msg, bot, truncate=False):
|
||||
# Set sender
|
||||
return_message = ""
|
||||
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:
|
||||
return_message += f"@{msg.from_user.username}: "
|
||||
else:
|
||||
@@ -87,7 +89,18 @@ def message_handling(bot, update):
|
||||
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)
|
||||
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))
|
||||
updater.start_polling()
|
||||
|
||||
@@ -1,30 +1,7 @@
|
||||
#!/usr/bin/env python3
|
||||
from telegram import Bot
|
||||
import sys
|
||||
import re
|
||||
import config
|
||||
from emoji import emojize
|
||||
|
||||
bot = Bot(token=config.api_key)
|
||||
for line in sys.stdin:
|
||||
text = ""
|
||||
parse_mode = None
|
||||
if not re.match(".* Mod performs HTTP request with URL ", line):
|
||||
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))
|
||||
|
||||
@@ -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)
|
||||
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
|
||||
|
||||
function main(http_api)
|
||||
function main()
|
||||
port = minetest.settings:get("chat_server_port")
|
||||
if port == nil then
|
||||
port = "9876"
|
||||
end
|
||||
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
|
||||
|
||||
local http_api = minetest.request_http_api()
|
||||
if http_api == nil then
|
||||
minetest.log("error", "Chat Mod is not in secure.http_mods or secure.trusted_mods. Exiting.")
|
||||
else
|
||||
main(http_api)
|
||||
main()
|
||||
end
|
||||
minetest.register_on_dieplayer(dead)
|
||||
minetest.register_on_joinplayer(join)
|
||||
minetest.register_on_leaveplayer(left)
|
||||
minetest.register_on_chat_message(chat)
|
||||
|
||||
Reference in New Issue
Block a user