mirror of
https://gitlab.com/octospacc/WinDog.git
synced 2025-06-05 22:09:20 +02:00
Crossplatform updates
This commit is contained in:
@@ -30,7 +30,7 @@
|
|||||||
},
|
},
|
||||||
"level": {
|
"level": {
|
||||||
"empty": [
|
"empty": [
|
||||||
"Check what's your level of something.\n\n*Usage*: {Cmd} <Thing>."
|
"Check what's your level of something.\n\n*Usage*: {Cmd} <Thing>."
|
||||||
],
|
],
|
||||||
"done": [
|
"done": [
|
||||||
"_Your level of *{Thing}* is... *{Percent}%*._"
|
"_Your level of *{Thing}* is... *{Percent}%*._"
|
||||||
|
@@ -20,7 +20,7 @@
|
|||||||
},
|
},
|
||||||
"level": {
|
"level": {
|
||||||
"empty": [
|
"empty": [
|
||||||
"Controlla il tuo livello di qualcosa.\n\n*Uso*: {Cmd} <Cosa>."
|
"Controlla il tuo livello di qualcosa.\n\n*Uso*: {Cmd} <Cosa>."
|
||||||
],
|
],
|
||||||
"done": [
|
"done": [
|
||||||
"_Il tuo livello di *{Thing}* è... *{Percent}%*._"
|
"_Il tuo livello di *{Thing}* è... *{Percent}%*._"
|
||||||
|
@@ -3,16 +3,12 @@
|
|||||||
# Licensed under AGPLv3 by OctoSpacc #
|
# Licensed under AGPLv3 by OctoSpacc #
|
||||||
# ================================== #
|
# ================================== #
|
||||||
|
|
||||||
def percenter(update:Update, context:CallbackContext) -> None:
|
def percenter(Context, Data=None) -> None:
|
||||||
Cmd = HandleCmd(update)
|
if Data.Body:
|
||||||
if not Cmd: return
|
Text = choice(Locale.__(f'{Data.Name}.done'))
|
||||||
if len(Cmd.Tokens) >= 2:
|
|
||||||
Text = choice(Locale.__(f'{Cmd.Name}.done'))
|
|
||||||
else:
|
else:
|
||||||
Text = choice(Locale.__(f'{Cmd.Name}.empty'))
|
Text = choice(Locale.__(f'{Data.Name}.empty'))
|
||||||
update.message.reply_markdown_v2(
|
SendMsg(Context, {"Text": Text.format(Cmd=Data.Tokens[0], Percent=RandPercent(), Thing=Data.Body)})
|
||||||
CharEscape(Text, '.!<>').format(Cmd=Cmd.Tokens[0], Percent=RandPercent(), Thing=Cmd.Body),
|
|
||||||
reply_to_message_id=update.message.message_id)
|
|
||||||
|
|
||||||
def multifun(update:Update, context:CallbackContext) -> None:
|
def multifun(update:Update, context:CallbackContext) -> None:
|
||||||
Cmd = HandleCmd(update)
|
Cmd = HandleCmd(update)
|
||||||
@@ -38,7 +34,7 @@ def multifun(update:Update, context:CallbackContext) -> None:
|
|||||||
update.message.reply_markdown_v2(Text, reply_to_message_id=ReplyToMsg)
|
update.message.reply_markdown_v2(Text, reply_to_message_id=ReplyToMsg)
|
||||||
|
|
||||||
def cStart(Context, Data=None) -> None:
|
def cStart(Context, Data=None) -> None:
|
||||||
SendMsg(Context, {"Text": "Hi stupid :3"})
|
SendMsg(Context, {"Text": choice(Locale.__('start')).format('stupid')})
|
||||||
|
|
||||||
#def cStart(update:Update, context:CallbackContext) -> None:
|
#def cStart(update:Update, context:CallbackContext) -> None:
|
||||||
# Cmd = HandleCmd(update)
|
# Cmd = HandleCmd(update)
|
||||||
@@ -67,34 +63,15 @@ def cConfig(update:Update, context:CallbackContext) -> None:
|
|||||||
# ... language: en, it, ...
|
# ... language: en, it, ...
|
||||||
# ... userdata: import, export, delete
|
# ... userdata: import, export, delete
|
||||||
|
|
||||||
|
def cPing(Context, Data=None) -> None:
|
||||||
|
SendMsg(Context, {"Text": "*Pong!*"})
|
||||||
|
|
||||||
def cEcho(Context, Data=None) -> None:
|
def cEcho(Context, Data=None) -> None:
|
||||||
if Data.Body:
|
if Data.Body:
|
||||||
SendMsg(Context, {"Text": Data.Body})
|
SendMsg(Context, {"Text": Data.Body})
|
||||||
else:
|
else:
|
||||||
SendMsg(Context, {"Text": choice(Locale.__('echo.empty'))})
|
SendMsg(Context, {"Text": choice(Locale.__('echo.empty'))})
|
||||||
|
|
||||||
def cEcho2(update:Update, context:CallbackContext) -> None:
|
|
||||||
Cmd = HandleCmd(update)
|
|
||||||
if not Cmd: return
|
|
||||||
Msg = update.message.text
|
|
||||||
if len(Msg.split(' ')) >= 2:
|
|
||||||
Text = Msg[len(Msg.split(' ')[0])+1:]
|
|
||||||
update.message.reply_text(
|
|
||||||
Text,
|
|
||||||
reply_to_message_id=update.message.message_id)
|
|
||||||
else:
|
|
||||||
Text = CharEscape(choice(Locale.__('echo.empty')), '.!')
|
|
||||||
update.message.reply_markdown_v2(
|
|
||||||
Text,
|
|
||||||
reply_to_message_id=update.message.message_id)
|
|
||||||
|
|
||||||
def cPing(update:Update, context:CallbackContext) -> None:
|
|
||||||
Cmd = HandleCmd(update)
|
|
||||||
if not Cmd: return
|
|
||||||
update.message.reply_markdown_v2(
|
|
||||||
'*Pong\!*',
|
|
||||||
reply_to_message_id=update.message.message_id)
|
|
||||||
|
|
||||||
#def cTime(update:Update, context:CallbackContext) -> None:
|
#def cTime(update:Update, context:CallbackContext) -> None:
|
||||||
# update.message.reply_markdown_v2(
|
# update.message.reply_markdown_v2(
|
||||||
# CharEscape(choice(Locale.__('time')).format(time.ctime().replace(' ', ' ')), 'MARKDOWN_SPEECH'),
|
# CharEscape(choice(Locale.__('time')).format(time.ctime().replace(' ', ' ')), 'MARKDOWN_SPEECH'),
|
||||||
|
69
WinDog.py
69
WinDog.py
@@ -12,12 +12,17 @@ from types import SimpleNamespace
|
|||||||
#from traceback import format_exc as TraceText
|
#from traceback import format_exc as TraceText
|
||||||
import mastodon, telegram
|
import mastodon, telegram
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
|
from html import unescape as HtmlUnescape
|
||||||
|
from markdown import markdown
|
||||||
from telegram import Update, ForceReply, Bot
|
from telegram import Update, ForceReply, Bot
|
||||||
from telegram.utils.helpers import escape_markdown
|
from telegram.utils.helpers import escape_markdown
|
||||||
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext
|
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext
|
||||||
from urllib import parse as UrlParse
|
from urllib import parse as UrlParse
|
||||||
from urllib.request import urlopen, Request
|
from urllib.request import urlopen, Request
|
||||||
|
|
||||||
|
# <https://daringfireball.net/projects/markdown/syntax#backslash>
|
||||||
|
MdEscapes = '\\`*_{}[]()<>#+-.!'
|
||||||
|
|
||||||
Db = {"Chats": {}}
|
Db = {"Chats": {}}
|
||||||
Locale = {"Fallback": {}}
|
Locale = {"Fallback": {}}
|
||||||
|
|
||||||
@@ -30,7 +35,26 @@ for Dir in ('Lib', 'Mod'):
|
|||||||
|
|
||||||
Endpoints = {
|
Endpoints = {
|
||||||
"start": cStart,
|
"start": cStart,
|
||||||
|
#"help": cHelp,
|
||||||
|
#"config": cConfig,
|
||||||
|
#"source": cSource,
|
||||||
|
"ping": cPing,
|
||||||
"echo": cEcho,
|
"echo": cEcho,
|
||||||
|
"wish": percenter,
|
||||||
|
"level": percenter,
|
||||||
|
#"hug": multifun,
|
||||||
|
#"pat": multifun,
|
||||||
|
#"poke": multifun,
|
||||||
|
#"cuddle": multifun,
|
||||||
|
#"floor": multifun,
|
||||||
|
#"hands": multifun,
|
||||||
|
#"sessocto": multifun,
|
||||||
|
#"hash": cHash,
|
||||||
|
#"eval": cEval,
|
||||||
|
#"exec": cExec,
|
||||||
|
#"web": cWeb,
|
||||||
|
#"unsplash": cUnsplash,
|
||||||
|
#"safebooru": cSafebooru,
|
||||||
}
|
}
|
||||||
|
|
||||||
def SetupLocale() -> None:
|
def SetupLocale() -> None:
|
||||||
@@ -86,9 +110,19 @@ def CharEscape(String:str, Escape:str='') -> str:
|
|||||||
String = String.replace(c, '\\'+c)
|
String = String.replace(c, '\\'+c)
|
||||||
return String
|
return String
|
||||||
|
|
||||||
def MarkdownCode(Text:str, Block:bool):
|
def InferMdEscape(Raw:str, Plain:str) -> str:
|
||||||
|
Chs = ''
|
||||||
|
for Ch in MdEscapes:
|
||||||
|
if Ch in Raw and Ch in Plain:
|
||||||
|
Chs += Ch
|
||||||
|
return Chs
|
||||||
|
|
||||||
|
def MarkdownCode(Text:str, Block:bool) -> str:
|
||||||
return '```\n' + CharEscape(Text.strip(), 'MARKDOWN') + '\n```'
|
return '```\n' + CharEscape(Text.strip(), 'MARKDOWN') + '\n```'
|
||||||
|
|
||||||
|
def MdToTxt(Md:str) -> str:
|
||||||
|
return BeautifulSoup(markdown(Md), 'html.parser').get_text(' ')
|
||||||
|
|
||||||
def CmdAllowed(update) -> bool:
|
def CmdAllowed(update) -> bool:
|
||||||
if not TGRestrict:
|
if not TGRestrict:
|
||||||
return True
|
return True
|
||||||
@@ -114,9 +148,14 @@ def ParseCmd(Msg) -> dict:
|
|||||||
"Name": Name,
|
"Name": Name,
|
||||||
"Body": Name.join(Msg.split(Name)[1:]).strip(),
|
"Body": Name.join(Msg.split(Name)[1:]).strip(),
|
||||||
"Tokens": GetRawTokens(Msg),
|
"Tokens": GetRawTokens(Msg),
|
||||||
|
"User": {"Name": "", "Tag": "", "Id": ""},
|
||||||
|
"Tagged": {},
|
||||||
})
|
})
|
||||||
|
|
||||||
def filters(update:Update, context:CallbackContext=None) -> None:
|
def filters(update:Update, context:CallbackContext=None) -> None:
|
||||||
|
Cmd = ParseCmd(update.message.text)
|
||||||
|
if Cmd.Tokens[0][0] in CmdPrefixes and Cmd.Name in Endpoints:
|
||||||
|
Endpoints[Cmd.Name](update, Cmd)
|
||||||
if Debug and Dumper:
|
if Debug and Dumper:
|
||||||
Text = update.message.text
|
Text = update.message.text
|
||||||
Text = (Text.replace('\n', '\\n') if Text else '')
|
Text = (Text.replace('\n', '\\n') if Text else '')
|
||||||
@@ -146,9 +185,9 @@ def setfilter(update:Update, context:CallbackContext) -> None:
|
|||||||
def RandPercent() -> int:
|
def RandPercent() -> int:
|
||||||
Num = randint(0,100)
|
Num = randint(0,100)
|
||||||
if Num == 100:
|
if Num == 100:
|
||||||
Num = str(Num) + '\.00'
|
Num = f'{Num}.00'
|
||||||
else:
|
else:
|
||||||
Num = str(Num) + '\.' + str(randint(0,9)) + str(randint(0,9))
|
Num = f'{Num}.{randint(0,9)}{randint(0,9)}'
|
||||||
return Num
|
return Num
|
||||||
|
|
||||||
def RandHexStr(Len:int) -> str:
|
def RandHexStr(Len:int) -> str:
|
||||||
@@ -177,15 +216,18 @@ def SendMsg(Context, Data):
|
|||||||
Manager = Context['Manager'] if 'Manager' in Context else None
|
Manager = Context['Manager'] if 'Manager' in Context else None
|
||||||
else:
|
else:
|
||||||
[Event, Manager] = [Context, Context]
|
[Event, Manager] = [Context, Context]
|
||||||
|
TextPlain = MdToTxt(Data['Text'])
|
||||||
if isinstance(Manager, mastodon.Mastodon):
|
if isinstance(Manager, mastodon.Mastodon):
|
||||||
Manager.status_post(
|
Manager.status_post(
|
||||||
(Data['Text'] + '\n\n@' + Event['account']['acct']),
|
(TextPlain + '\n\n@' + Event['account']['acct']),
|
||||||
in_reply_to_id=Event['status']['id'],
|
in_reply_to_id=Event['status']['id'],
|
||||||
visibility=('direct' if Event['status']['visibility'] == 'direct' else 'unlisted')
|
visibility=('direct' if Event['status']['visibility'] == 'direct' else 'unlisted')
|
||||||
)
|
)
|
||||||
elif isinstance(Manager, telegram.Update):
|
elif isinstance(Manager, telegram.Update):
|
||||||
Event.message.reply_markdown_v2(
|
Event.message.reply_markdown_v2(
|
||||||
Data['Text'],
|
CharEscape(HtmlUnescape(Data['Text']), InferMdEscape(HtmlUnescape(Data['Text']), TextPlain)),
|
||||||
|
#Event.message.reply_text(
|
||||||
|
#TextPlain,
|
||||||
reply_to_message_id=Event.message.message_id
|
reply_to_message_id=Event.message.message_id
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -198,12 +240,10 @@ def Main() -> None:
|
|||||||
updater = Updater(TGToken)
|
updater = Updater(TGToken)
|
||||||
dispatcher = updater.dispatcher
|
dispatcher = updater.dispatcher
|
||||||
|
|
||||||
dispatcher.add_handler(CommandHandler('start', cStart))
|
#dispatcher.add_handler(CommandHandler('start', cStart))
|
||||||
dispatcher.add_handler(CommandHandler('config', cConfig))
|
dispatcher.add_handler(CommandHandler('config', cConfig))
|
||||||
dispatcher.add_handler(CommandHandler('help', cHelp))
|
dispatcher.add_handler(CommandHandler('help', cHelp))
|
||||||
dispatcher.add_handler(CommandHandler('source', cSource))
|
dispatcher.add_handler(CommandHandler('source', cSource))
|
||||||
dispatcher.add_handler(CommandHandler('echo', cEcho2))
|
|
||||||
dispatcher.add_handler(CommandHandler('ping', cPing))
|
|
||||||
#dispatcher.add_handler(CommandHandler('time', cTime))
|
#dispatcher.add_handler(CommandHandler('time', cTime))
|
||||||
dispatcher.add_handler(CommandHandler('hash', cHash))
|
dispatcher.add_handler(CommandHandler('hash', cHash))
|
||||||
dispatcher.add_handler(CommandHandler('eval', cEval))
|
dispatcher.add_handler(CommandHandler('eval', cEval))
|
||||||
@@ -212,31 +252,28 @@ def Main() -> None:
|
|||||||
dispatcher.add_handler(CommandHandler('unsplash', cUnsplash))
|
dispatcher.add_handler(CommandHandler('unsplash', cUnsplash))
|
||||||
dispatcher.add_handler(CommandHandler('safebooru', cSafebooru))
|
dispatcher.add_handler(CommandHandler('safebooru', cSafebooru))
|
||||||
|
|
||||||
for Cmd in ('wish', 'level'):
|
|
||||||
dispatcher.add_handler(CommandHandler(Cmd, percenter))
|
|
||||||
for Cmd in ('hug', 'pat', 'poke', 'cuddle', 'floor', 'hands', 'sessocto'):
|
for Cmd in ('hug', 'pat', 'poke', 'cuddle', 'floor', 'hands', 'sessocto'):
|
||||||
dispatcher.add_handler(CommandHandler(Cmd, multifun))
|
dispatcher.add_handler(CommandHandler(Cmd, multifun))
|
||||||
|
|
||||||
#dispatcher.add_handler(CommandHandler('setfilter', setfilter))
|
#dispatcher.add_handler(CommandHandler('setfilter', setfilter))
|
||||||
dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, filters))
|
dispatcher.add_handler(MessageHandler(Filters.text | Filters.command, filters))
|
||||||
|
|
||||||
print('Starting WinDog...')
|
print('Starting WinDog...')
|
||||||
updater.start_polling()
|
updater.start_polling()
|
||||||
|
|
||||||
#if MastodonUrl and MastodonToken:
|
if MastodonUrl and MastodonToken:
|
||||||
Mastodon = mastodon.Mastodon(api_base_url=MastodonUrl, access_token=MastodonToken)
|
Mastodon = mastodon.Mastodon(api_base_url=MastodonUrl, access_token=MastodonToken)
|
||||||
class mmyListener(mastodon.StreamListener):
|
class MastodonListener(mastodon.StreamListener):
|
||||||
def on_notification(self, Event):
|
def on_notification(self, Event):
|
||||||
if Event['type'] == 'mention':
|
if Event['type'] == 'mention':
|
||||||
Msg = BeautifulSoup(Event['status']['content'], 'html.parser').get_text(' ').strip().replace('\t', ' ')
|
Msg = BeautifulSoup(Event['status']['content'], 'html.parser').get_text(' ').strip().replace('\t', ' ')
|
||||||
if not Msg.split('@')[0]:
|
if not Msg.split('@')[0]:
|
||||||
Msg = ' '.join('@'.join(Msg.split('@')[1:]).strip().split(' ')[1:]).strip()
|
Msg = ' '.join('@'.join(Msg.split('@')[1:]).strip().split(' ')[1:]).strip()
|
||||||
if Msg[0] in '.!/':
|
if Msg[0] in CmdPrefixes:
|
||||||
Cmd = ParseCmd(Msg)
|
Cmd = ParseCmd(Msg)
|
||||||
if Cmd.Name in Endpoints:
|
if Cmd.Name in Endpoints:
|
||||||
Endpoints[Cmd.Name]({"Event": Event, "Manager": Mastodon}, Cmd)
|
Endpoints[Cmd.Name]({"Event": Event, "Manager": Mastodon}, Cmd)
|
||||||
|
Mastodon.stream_user(MastodonListener())
|
||||||
Mastodon.stream_user(mmyListener())
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
time.sleep(9**9)
|
time.sleep(9**9)
|
||||||
|
@@ -1,9 +1,13 @@
|
|||||||
# Some bot modules
|
# Program core
|
||||||
|
# ...
|
||||||
|
|
||||||
|
# Required by some bot modules
|
||||||
urllib3
|
urllib3
|
||||||
|
|
||||||
# Mastodon support
|
# Mastodon support
|
||||||
Mastodon.py
|
Mastodon.py
|
||||||
beautifulsoup4
|
beautifulsoup4
|
||||||
|
Markdown
|
||||||
|
|
||||||
# Telegram support
|
# Telegram support
|
||||||
python-telegram-bot
|
python-telegram-bot
|
||||||
|
Reference in New Issue
Block a user