mirror of https://gitlab.com/octospacc/WinDog.git
Add embedded links feature; Update internal API for getting quoted
This commit is contained in:
parent
971113e529
commit
2512df4f98
|
@ -41,10 +41,12 @@ Endpoints = {
|
||||||
#"hands": multifun,
|
#"hands": multifun,
|
||||||
#"sessocto": multifun,
|
#"sessocto": multifun,
|
||||||
"hash": cHash,
|
"hash": cHash,
|
||||||
|
#"time": cTime,
|
||||||
"eval": cEval,
|
"eval": cEval,
|
||||||
"exec": cExec,
|
"exec": cExec,
|
||||||
#"format": cFormat,
|
#"format": cFormat,
|
||||||
#"frame": cFrame,
|
#"frame": cFrame,
|
||||||
|
"embedded": cEmbedded,
|
||||||
"web": cWeb,
|
"web": cWeb,
|
||||||
"translate": cTranslate,
|
"translate": cTranslate,
|
||||||
"unsplash": cUnsplash,
|
"unsplash": cUnsplash,
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
# ================================== #
|
# ==================================== #
|
||||||
# WinDog multi-purpose chatbot #
|
# WinDog multi-purpose chatbot #
|
||||||
# Licensed under AGPLv3 by OctoSpacc #
|
# Licensed under AGPLv3 by OctoSpacc #
|
||||||
# ================================== #
|
# ==================================== #
|
||||||
|
|
||||||
|
from urlextract import URLExtract
|
||||||
|
|
||||||
# Module: Percenter
|
# Module: Percenter
|
||||||
# Provides fun trough percentage-based toys.
|
# Provides fun trough percentage-based toys.
|
||||||
|
@ -38,23 +40,13 @@ 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)
|
||||||
|
|
||||||
# Module: Start
|
# Module: Start
|
||||||
# ...
|
# Salutes the user, for now no other purpose except giving a feel that the bot is working.
|
||||||
def cStart(Context, Data=None) -> None:
|
def cStart(Context, Data=None) -> None:
|
||||||
SendMsg(Context, {"Text": choice(Locale.__('start')).format('stupid')})
|
SendMsg(Context, {"Text": choice(Locale.__('start')).format(Data.User['Name'])})
|
||||||
|
|
||||||
#def cStart(update:Update, context:CallbackContext) -> None:
|
|
||||||
# Cmd = HandleCmd(update)
|
|
||||||
# if not Cmd: return
|
|
||||||
# user = update.effective_user
|
|
||||||
# update.message.reply_markdown_v2(#'Hi\!',
|
|
||||||
# #CharEscape(choice(Locale.__('start')).format(CharEscape(user.mention_markdown_v2(), 'MARKDOWN')), 'MARKDOWN'),
|
|
||||||
# CharEscape(choice(Locale.__('start')), '.!').format(user.mention_markdown_v2()),
|
|
||||||
# reply_to_message_id=update.message.message_id)
|
|
||||||
|
|
||||||
# Module: Help
|
# Module: Help
|
||||||
# ...
|
# Provides help for the bot. For now, it just lists the commands.
|
||||||
def cHelp(Context, Data=None) -> None:
|
def cHelp(Context, Data=None) -> None:
|
||||||
#SendMsg(Context, {"Text": choice(Locale.__('help'))})
|
|
||||||
Commands = ''
|
Commands = ''
|
||||||
for Cmd in Endpoints.keys():
|
for Cmd in Endpoints.keys():
|
||||||
Commands += f'* /{Cmd}\n'
|
Commands += f'* /{Cmd}\n'
|
||||||
|
@ -117,12 +109,12 @@ def cHash(Context, Data=None) -> None:
|
||||||
SendMsg(Context, {"Text": choice(Locale.__('hash.usage')).format(Data.Tokens[0], hashlib.algorithms_available)})
|
SendMsg(Context, {"Text": choice(Locale.__('hash.usage')).format(Data.Tokens[0], hashlib.algorithms_available)})
|
||||||
|
|
||||||
# Module: Eval
|
# Module: Eval
|
||||||
# Execute a Python command (or safe literal operation) in the current context.
|
# Execute a Python command (or safe literal operation) in the current context. Currently not implemented.
|
||||||
def cEval(Context, Data=None) -> None:
|
def cEval(Context, Data=None) -> None:
|
||||||
SendMsg(Context, {"Text": choice(Locale.__('eval'))})
|
SendMsg(Context, {"Text": choice(Locale.__('eval'))})
|
||||||
|
|
||||||
# Module: Exec
|
# Module: Exec
|
||||||
# Execute a system command and return stdout/stderr.
|
# Execute a system command from the allowed ones and return stdout/stderr.
|
||||||
def cExec(Context, Data=None) -> None:
|
def cExec(Context, Data=None) -> None:
|
||||||
if len(Data.Tokens) >= 2 and Data.Tokens[1].lower() in ExecAllowed:
|
if len(Data.Tokens) >= 2 and Data.Tokens[1].lower() in ExecAllowed:
|
||||||
Cmd = Data.Tokens[1].lower()
|
Cmd = Data.Tokens[1].lower()
|
||||||
|
@ -137,15 +129,46 @@ def cExec(Context, Data=None) -> None:
|
||||||
SendMsg(Context, {"Text": choice(Locale.__('eval'))})
|
SendMsg(Context, {"Text": choice(Locale.__('eval'))})
|
||||||
|
|
||||||
# Module: Format
|
# Module: Format
|
||||||
# Reformat text using an handful of rules
|
# Reformat text using an handful of rules. Currently not implemented.
|
||||||
def cFormat(Context, Data=None) -> None:
|
def cFormat(Context, Data=None) -> None:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# Module: Frame
|
# Module: Frame
|
||||||
# Frame someone's message into a platform-styled image.
|
# Frame someone's message into a platform-styled image. Currently not implemented.
|
||||||
def cFrame(Context, Data=None) -> None:
|
def cFrame(Context, Data=None) -> None:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
# Module: Embedded
|
||||||
|
# Rewrite a link trying to make sure we have an embed view.
|
||||||
|
def cEmbedded(Context, Data=None) -> None:
|
||||||
|
if len(Data.Tokens) >= 2:
|
||||||
|
# Find links in command body
|
||||||
|
Text = Data.Body
|
||||||
|
elif Data.Quoted and Data.Quoted['Body']:
|
||||||
|
# Find links in quoted message
|
||||||
|
Text = Data.Quoted['Body']
|
||||||
|
else:
|
||||||
|
# Error
|
||||||
|
return
|
||||||
|
pass
|
||||||
|
Urls = URLExtract().find_urls(Text)
|
||||||
|
if len(Urls) > 0:
|
||||||
|
Proto = 'https://'
|
||||||
|
Url = Urls[0]
|
||||||
|
UrlLow = Url.lower()
|
||||||
|
if UrlLow.startswith('http://') or UrlLow.startswith('https://'):
|
||||||
|
Proto = Url.split('://')[0] + '://'
|
||||||
|
Url = '://'.join(Url.split('://')[1:])
|
||||||
|
UrlLow = '://'.join(UrlLow.split('://')[1:])
|
||||||
|
if UrlLow.startswith('facebook.com/') or UrlLow.startswith('www.facebook.com/') or UrlLow.startswith('m.facebook.com/') or UrlLow.startswith('mbasic.facebook.com/'):
|
||||||
|
Url = 'https://hlb0.octt.eu.org/cors-main.php/https://' + Url
|
||||||
|
Proto = ''
|
||||||
|
elif UrlLow.startswith('instagram.com/'):
|
||||||
|
Url = 'ddinstagram.com/' + Url[len('instagram.com/'):]
|
||||||
|
elif UrlLow.startswith('twitter.com/'):
|
||||||
|
Url = 'fxtwitter.com/' + Url[len('twitter.com/'):]
|
||||||
|
SendMsg(Context, {"TextPlain": Proto+Url})
|
||||||
|
|
||||||
# Module: Web
|
# Module: Web
|
||||||
# Provides results of a DuckDuckGo search.
|
# Provides results of a DuckDuckGo search.
|
||||||
def cWeb(Context, Data=None) -> None:
|
def cWeb(Context, Data=None) -> None:
|
||||||
|
|
62
WinDog.py
62
WinDog.py
|
@ -1,8 +1,8 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
# ================================== #
|
# ==================================== #
|
||||||
# WinDog multi-purpose chatbot #
|
# WinDog multi-purpose chatbot #
|
||||||
# Licensed under AGPLv3 by OctoSpacc #
|
# Licensed under AGPLv3 by OctoSpacc #
|
||||||
# ================================== #
|
# ==================================== #
|
||||||
|
|
||||||
import json, hashlib, re, time, subprocess
|
import json, hashlib, re, time, subprocess
|
||||||
from binascii import hexlify
|
from binascii import hexlify
|
||||||
|
@ -25,8 +25,8 @@ from urllib.request import urlopen, Request
|
||||||
# <https://daringfireball.net/projects/markdown/syntax#backslash>
|
# <https://daringfireball.net/projects/markdown/syntax#backslash>
|
||||||
MdEscapes = '\\`*_{}[]()<>#+-.!|='
|
MdEscapes = '\\`*_{}[]()<>#+-.!|='
|
||||||
|
|
||||||
Db = {"Chats": {}}
|
Db = { "Rooms": {}, "Users": {}, }
|
||||||
Locale = {"Fallback": {}}
|
Locale = { "Fallback": {}, }
|
||||||
|
|
||||||
for Dir in ('Mod', 'Lib'):
|
for Dir in ('Mod', 'Lib'):
|
||||||
for File in listdir(f'./{Dir}WinDog'):
|
for File in listdir(f'./{Dir}WinDog'):
|
||||||
|
@ -140,17 +140,28 @@ 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": {},
|
"User": None,
|
||||||
"Tagged": {},
|
"Quoted": None,
|
||||||
})
|
})
|
||||||
|
|
||||||
def TelegramQueryHandle(update:Update, context:CallbackContext=None) -> None:
|
def TelegramQueryHandle(update:Update, context:CallbackContext=None) -> None:
|
||||||
if update and update.message:
|
if update and update.message:
|
||||||
Cmd = ParseCmd(update.message.text)
|
Cmd = ParseCmd(update.message.text)
|
||||||
if Cmd and Cmd.Tokens[0][0] in CmdPrefixes and Cmd.Name in Endpoints:
|
if Cmd and Cmd.Tokens[0][0] in CmdPrefixes and Cmd.Name in Endpoints:
|
||||||
Cmd.User['Name'] = 'null'
|
Cmd.User = {
|
||||||
Cmd.User['Tag'] = 'null'
|
"Name": update.message.from_user.first_name,
|
||||||
Cmd.User['Id'] = f'{update.message.from_user.id}@telegram'
|
"Tag": update.message.from_user.username,
|
||||||
|
"Id": f'{update.message.from_user.id}@telegram',
|
||||||
|
}
|
||||||
|
if update.message.reply_to_message:
|
||||||
|
Cmd.Quoted = {
|
||||||
|
"Body": update.message.reply_to_message.text,
|
||||||
|
"User": {
|
||||||
|
"Name": update.message.reply_to_message.from_user.first_name,
|
||||||
|
"Tag": update.message.reply_to_message.from_user.username,
|
||||||
|
"Id": f'{update.message.reply_to_message.from_user.id}@telegram',
|
||||||
|
},
|
||||||
|
}
|
||||||
Endpoints[Cmd.Name]({ "Event": update, "Manager": context }, Cmd)
|
Endpoints[Cmd.Name]({ "Event": update, "Manager": context }, Cmd)
|
||||||
if Debug and Dumper:
|
if Debug and Dumper:
|
||||||
Text = update.message.text
|
Text = update.message.text
|
||||||
|
@ -168,16 +179,6 @@ def TelegramQueryHandle(update:Update, context:CallbackContext=None) -> None:
|
||||||
reply_to_message_id=update.message.message_id)
|
reply_to_message_id=update.message.message_id)
|
||||||
'''
|
'''
|
||||||
|
|
||||||
def setfilter(update:Update, context:CallbackContext) -> None:
|
|
||||||
pass
|
|
||||||
'''
|
|
||||||
if CmdAllowed(update):
|
|
||||||
ChatID = update.message.chat.id
|
|
||||||
if ChatID not in Private['Chats'] or 'Filters' not in Private['Chats'][ChatID]:
|
|
||||||
Private['Chats'][ChatID] = {'Filters':{}}
|
|
||||||
Private['Chats'][ChatID]['Filters'][update.message.text] = {'Text':0}
|
|
||||||
'''
|
|
||||||
|
|
||||||
def RandPercent() -> int:
|
def RandPercent() -> int:
|
||||||
Num = randint(0,100)
|
Num = randint(0,100)
|
||||||
if Num == 100:
|
if Num == 100:
|
||||||
|
@ -192,21 +193,10 @@ def RandHexStr(Len:int) -> str:
|
||||||
Hex += choice('0123456789abcdef')
|
Hex += choice('0123456789abcdef')
|
||||||
return Hex
|
return Hex
|
||||||
|
|
||||||
#def CmdArgs(Msg:str, Cfg:tuple=None):
|
|
||||||
# Args = []
|
|
||||||
# Msg = Msg.strip().replace('\t', ' ')
|
|
||||||
# if Cfg:
|
|
||||||
# for i in Cfg:
|
|
||||||
# Args += [Msg.replace(' ', ' ').replace(' ', ' ').split(' ')[:i]]
|
|
||||||
# Msg = Msg
|
|
||||||
# else:
|
|
||||||
# return Msg.replace(' ', ' ').replace(' ', ' ').split(' ')
|
|
||||||
|
|
||||||
def HttpGet(Url:str):
|
def HttpGet(Url:str):
|
||||||
return urlopen(Request(Url, headers={"User-Agent": WebUserAgent}))
|
return urlopen(Request(Url, headers={"User-Agent": WebUserAgent}))
|
||||||
|
|
||||||
def SendMsg(Context, Data, Destination=None) -> None:
|
def SendMsg(Context, Data, Destination=None) -> None:
|
||||||
#Data: Text, Media, Files
|
|
||||||
if type(Context) == dict:
|
if type(Context) == dict:
|
||||||
Event = Context['Event'] if 'Event' in Context else None
|
Event = Context['Event'] if 'Event' in Context else None
|
||||||
Manager = Context['Manager'] if 'Manager' in Context else None
|
Manager = Context['Manager'] if 'Manager' in Context else None
|
||||||
|
@ -255,22 +245,13 @@ def Main() -> None:
|
||||||
SetupDb()
|
SetupDb()
|
||||||
SetupLocale()
|
SetupLocale()
|
||||||
|
|
||||||
#Private['Chats'].update({update.message.chat.id:{}})
|
if TelegramToken:
|
||||||
|
|
||||||
updater = Updater(TelegramToken)
|
updater = Updater(TelegramToken)
|
||||||
dispatcher = updater.dispatcher
|
dispatcher = updater.dispatcher
|
||||||
|
|
||||||
dispatcher.add_handler(CommandHandler('config', cConfig))
|
dispatcher.add_handler(CommandHandler('config', cConfig))
|
||||||
#dispatcher.add_handler(CommandHandler('time', cTime))
|
|
||||||
#dispatcher.add_handler(CommandHandler('repeat', cRepeat))
|
|
||||||
|
|
||||||
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(MessageHandler(Filters.text | Filters.command, TelegramQueryHandle))
|
dispatcher.add_handler(MessageHandler(Filters.text | Filters.command, TelegramQueryHandle))
|
||||||
|
|
||||||
print('Starting WinDog...')
|
|
||||||
updater.start_polling()
|
updater.start_polling()
|
||||||
|
|
||||||
if MastodonUrl and MastodonToken:
|
if MastodonUrl and MastodonToken:
|
||||||
|
@ -296,5 +277,6 @@ if __name__ == '__main__':
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
print('Starting WinDog...')
|
||||||
Main()
|
Main()
|
||||||
print('Closing WinDog...')
|
print('Closing WinDog...')
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
# Required by some bot modules
|
# Required by some bot modules
|
||||||
urllib3
|
urllib3
|
||||||
|
urlextract
|
||||||
|
|
||||||
# Mastodon support
|
# Mastodon support
|
||||||
Mastodon.py
|
Mastodon.py
|
||||||
|
|
Loading…
Reference in New Issue