fossbot.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. # FossBot - a bot for Matrix that converts links and does other stuff
  2. # Copyright (C) 2022 Zortazert and other contributors
  3. # This program is free software: you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation, either version 3 of the License, or
  6. # (at your option) any later version.
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU General Public License for more details.
  11. # You should have received a copy of the GNU General Public License
  12. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  13. from matrix_bot_api.matrix_bot_api import MatrixBotAPI
  14. from matrix_bot_api.mregex_handler import MRegexHandler
  15. from matrix_bot_api.mcommand_handler import MCommandHandler
  16. import requests
  17. import json
  18. import re
  19. import tempfile
  20. import random
  21. # Matrix account information must be stored in a file called
  22. # user-pass.json
  23. with open('user-pass.json', 'r') as f:
  24. user = json.load(f)
  25. USERNAME = user["Username"] # Bot's username
  26. PASSWORD = user["Password"] # Bot's password
  27. SERVER = user["Server"] # Matrix server URL
  28. # Put the instances of the libre services you want to use here. Put
  29. # only the domain name here, without the protocol, e.g. "yewtu.be" and
  30. # not "https://yewtu.be"
  31. INVIDIOUS_INSTANCE = "inv.vern.cc"
  32. NITTER_INSTANCE = "nt.vern.cc"
  33. TEDDIT_INSTANCE = "lr.vern.cc"
  34. def help_callback(room, event):
  35. room.send_text("""FossBot is a matrixbot that can do multiple things:
  36. - Detect sent youtube, reddit or twitter links and send back a message link with a proxy site such as: invidious, teddit and nitter.
  37. Commands:
  38. - !fbh or !help
  39. Print out this help message.
  40. - !fby QUERY or !invidious QUERY
  41. Print out YouTube search results as Invidious links with title and channel information. Uses the Invidious API
  42. - !fbp QUERY or !peertube QUERY
  43. Print out PeerTube search results as PeerTube instance links with title and channel information. Uses the Sepia Search API
  44. - !fbl QUERY or !lbry QUERY
  45. Print out LBRY search results as librarian links with title and channel information. Uses the lighthouse API
  46. - !fbc QUERY or !crypto QUERY <number of><currency>
  47. Print out in USD values of crypto currencies. Uses the rate.sx API.
  48. - !fbn QUERY or !neow QUERY
  49. Print out the amount of neow coins a user on neow
  50. - !fbw QUERY or !wikipedia QUERY
  51. Print out wikipedia search result snippets of different articles including links to those articles.
  52. - !fbU QUERY or !uncyclopedia QUERY
  53. Print out uncyclopedia search result snippets of different articles including links to those articles.
  54. - !fbu QUERY or !urban QUERY
  55. Print out urban dictionary search result descriptions and examples of slang words.
  56. - !fbg QUERY or !git QUERY
  57. Print out repository information for GitHub and Gitea projects.
  58. - !fbd QUERY or !dictionary QUERY
  59. Print out dictionary defintion of a word
  60. - !fbt QUERY or !tenor QUERY
  61. Send out tenor gifs. Uses the tenor API.
  62. - !fbs QUERY or !search QUERY
  63. Print out search results of a search. Uses the librex API.
  64. - !fbm QUERY or !meme QUERY
  65. Print out origins/descriptions of the QUERY meme. Scrapes knowyourmeme.
  66. - !fbrg QUERY = !garfield
  67. Sends a random garfield.""")
  68. print(event['sender']+"\nPosted:\n"+event["content"]["body"])
  69. def garfield_callback(room,event):
  70. if event["content"]["body"].startswith("!fbrg ") == True:
  71. query = event["content"]["body"].replace("!fbm ","")
  72. if event["content"]["body"].startswith("!garfield ") == True:
  73. query = event["content"]["body"].replace("!garfield ","")
  74. else:
  75. query = re.sub("^!fbrg|\[(.*?)\]: !fbrg ", "", event["content"]["body"])
  76. try:
  77. url = f'https://www.gocomics.com/{datetime.datetime.strftime(query, "%Y/%m/%d")}'
  78. except:
  79. url = "https://www.gocomics.com/random/garfield"
  80. print(url)
  81. data = requests.get(url, allow_redirects=True).text
  82. loc = data.find('https://assets.amuniversal.com/')
  83. image = data[loc:loc+63]
  84. temp_dir = tempfile.TemporaryDirectory().name
  85. with open(temp_dir, 'wb') as file:
  86. file.write(requests.get(image).content)
  87. with open(temp_dir, 'rb') as file:
  88. bot = MatrixBotAPI(USERNAME, PASSWORD, SERVER)
  89. upload = bot.client.upload(file, "image/png")
  90. room.send_image(upload, f"garfield_meme.gif")
  91. def knowyourmeme_callback(room,event):
  92. headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20121201 icecat/17.0.1'}
  93. if event["content"]["body"].startswith("!fbm ") == True:
  94. query = event["content"]["body"].replace("!fbm ","")
  95. if event["content"]["body"].startswith("!meme ") == True:
  96. query = event["content"]["body"].replace("!meme ","")
  97. else:
  98. query = re.sub("^!fbm|\[(.*?)\]: !fbm ", "", event["content"]["body"])
  99. room.send_text(f"Searching knowyourmeme for {query}")
  100. search = f"https://knowyourmeme.com/search?q={query}"
  101. search_result = requests.get(search, headers=headers)
  102. search_data = str(search_result.content)
  103. if "No results" in search_data:
  104. room.send_text("Nothing found :(")
  105. quit()
  106. links = re.findall('<a href="/memes/.+?">',search_data)
  107. for i, link in enumerate(links[15:]):
  108. url = "https://knowyourmeme.com" + link.replace("<a href=\"", "").replace("\">", "").replace("\" rel=\"nofollow","")
  109. if "trending" in url:
  110. break
  111. elif i == 2:
  112. break
  113. try:
  114. selected = "https://knowyourmeme.com"+links[i+18].replace("<a href=\"", "").replace("\">", "").replace("\" rel=\"nofollow","")
  115. result = requests.get(selected, headers=headers)
  116. data = str(result.content)
  117. paragraph = re.findall('<p>(.+?)</p>',data)
  118. cleaner = re.compile('<.*?>')
  119. #about = re.sub(cleaner, '', paragraph[2])
  120. origin = re.sub(cleaner, '', paragraph[3])
  121. #room.send_text("About:" + about.replace("\\'","'"))
  122. room.send_text(origin.replace("\\'","'"))
  123. except:
  124. room.send_text("Some error probably")
  125. print(event['sender']+"\nPosted:\n"+event["content"]["body"])
  126. def librex_callback(room, event):
  127. if event["content"]["body"].startswith("!fbs ") == True:
  128. query = event["content"]["body"].replace("!fbs ","")
  129. if event["content"]["body"].startswith("!search ") == True:
  130. query = event["content"]["body"].replace("!search ","")
  131. else:
  132. query = re.sub("^!fbs|\[(.*?)\]: !fbs ", "", event["content"]["body"])
  133. room.send_text(f"Searching librex for {query}")
  134. librex_instance = "https://search.davidovski.xyz/"
  135. librex_search = librex_instance + "api.php?q=" + query + "&p=1&type=0&invidious=https://invidio.xamh.de&nitter=https://nitter.net&reddit=https://teddit.net"
  136. print(librex_search)
  137. data = requests.get(librex_search)
  138. json_stuff = json.loads(data.text)
  139. for i, search in enumerate(json_stuff):
  140. room.send_text(search["title"]+"\n"+search["url"].replace("www.reddit.com",TEDDIT_INSTANCE).replace("reddit.com",TEDDIT_INSTANCE))
  141. if i == 2:
  142. break
  143. print(event['sender']+"\nPosted:\n"+event["content"]["body"])
  144. def tenor_callback(room, event):
  145. if event["content"]["body"].startswith("!fbt ") == True:
  146. query = event["content"]["body"].replace("!fbt ","")
  147. if event["content"]["body"].startswith("!tenor ") == True:
  148. query = event["content"]["body"].replace("!tenor ","")
  149. else:
  150. query = re.sub("^!fbt|\[(.*?)\]: !fbt ", "", event["content"]["body"])
  151. #headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20121201 icecat/17.0.1'}
  152. #search = f"https://tenor.com/search/{query}-gifs"
  153. #search_result = requests.get(search, headers=headers)
  154. #search_data = str(search_result.content)
  155. #links = re.findall('<img src=.+?>',search_data)
  156. #if "No Results" in search_data:
  157. # room.send_text("ERROR: Nothing found :(")
  158. #else:
  159. # r = random.randint(0,9)
  160. # url = re.findall(r'(https?://\S+)', links[33+r])
  161. # url = url[0].replace("\"", "")
  162. # print(url)
  163. # #room.send_text(f"<img src=\"{url}\">")
  164. # temp_dir = tempfile.TemporaryDirectory().name
  165. # thumbnail_data = requests.get(url)
  166. key = "LIVDSRZULELA"
  167. size = str(10)
  168. search = f"https://g.tenor.com/v1/search?q={query}&key={key}&limit={size}"
  169. data = requests.get(search)
  170. json_stuff = json.loads(data.text)
  171. if json_stuff["next"] == "0":
  172. room.send_text("Nothing found :(")
  173. elif query == "":
  174. room.send_text("ERROR: use !fbt QUERY or !tenor QUERY")
  175. else:
  176. try:
  177. r = random.randint(0,10)
  178. print(json_stuff["results"][r]["media"][0]["mediumgif"]["url"])
  179. temp_dir = tempfile.TemporaryDirectory().name
  180. thumbnail_data = requests.get(json_stuff["results"][r]["media"][0]["mediumgif"]["url"])
  181. except:
  182. r = 0
  183. print(json_stuff["results"][r]["media"][0]["mediumgif"]["url"])
  184. temp_dir = tempfile.TemporaryDirectory().name
  185. thumbnail_data = requests.get(json_stuff["results"][r]["media"][0]["mediumgif"]["url"])
  186. with open(temp_dir, 'wb') as file:
  187. file.write(thumbnail_data.content)
  188. with open(temp_dir, 'rb') as file:
  189. bot = MatrixBotAPI(USERNAME, PASSWORD, SERVER)
  190. upload = bot.client.upload(file, "image/png")
  191. room.send_image(upload, f"tenor_meme.gif")
  192. room.send_text(str(json_stuff["results"][r]["media"][0]["mediumgif"]["url"]))
  193. print(event['sender']+"\nPosted:\n"+event["content"]["body"])
  194. def dictionary_callback(room, event):
  195. if event["content"]["body"].startswith("!fbd ") == True:
  196. query = event["content"]["body"].replace("!fbd ","")
  197. if event["content"]["body"].startswith("!dictionary ") == True:
  198. query = event["content"]["body"].replace("!dictionary ","")
  199. else:
  200. query = re.sub("^!fbd|\[(.*?)\]: !fbd ", "", event["content"]["body"])
  201. data = requests.get(f"https://api.dictionaryapi.dev/api/v2/entries/en/{query}").json()
  202. try:
  203. definition = data[0]["meanings"][0]["definitions"][0]["definition"]
  204. except:
  205. definition = "No definitions found."
  206. room.send_text(f"""Searching the Dictionary for {query}:
  207. {definition}""")
  208. print(event['sender']+"\nPosted:\n"+event["content"]["body"])
  209. def urban_callback(room, event):
  210. if event["content"]["body"].startswith("!fbu ") == True:
  211. message = event["content"]["body"].replace("!fbu ","")
  212. if event["content"]["body"].startswith("!urban ") == True:
  213. message = event["content"]["body"].replace("!urban ","")
  214. else:
  215. message = re.sub("^!fbu|\[(.*?)\]: !fbu ", "", event["content"]["body"])
  216. api = f"https://api.urbandictionary.com/v0/define?term={message}"
  217. data = requests.get(api)
  218. json_stuff = json.loads(data.text)
  219. room.send_text(f"Searching urban dictionary for {message}")
  220. for i, result in enumerate(json_stuff["list"]):
  221. room.send_text(result["word"]+"\n"+result["definition"][:128].replace("\n","").replace("[","").replace("]","")+"..."+"\n"+result["example"][:128].replace("\n","").replace("[","").replace("]","")+"...")
  222. if i == 1:
  223. break
  224. print(event['sender']+"\nPosted:\n"+event["content"]["body"])
  225. def git_callback(room, event):
  226. if event["content"]["body"].startswith("!fbg ") == True:
  227. message = event["content"]["body"].replace("!fbg ","")
  228. if event["content"]["body"].startswith("!git ") == True:
  229. message = event["content"]["body"].replace("!git ","")
  230. else:
  231. message = re.sub("^!fbg|\[(.*?)\]: !fbg ", "", event["content"]["body"])
  232. query = message.split("/")
  233. if len(query) == 3:
  234. room.send_text("ERROR: Please add a repo name to your url.")
  235. elif query[2] == "github.com":
  236. github_api = f"https://api.github.com/repos/{query[3]}/{query[4]}"
  237. data = requests.get(github_api)
  238. json_stuff = json.loads(data.text)
  239. mb_size= round(float(json_stuff["size"]) / 1024, 1)
  240. if json_stuff["license"] == None:
  241. licence = "No Licence"
  242. else:
  243. licence = json_stuff["license"]["name"]
  244. languages = []
  245. data_languages = requests.get(f"https://api.github.com/repos/{query[3]}/{query[4]}/languages").json()
  246. for key in data_languages:
  247. languages.append(key)
  248. room.send_text(f"""Repo Name: {json_stuff["name"]}
  249. Author Name: {json_stuff["owner"]["login"]}
  250. Size: {mb_size} mb
  251. Licence: {licence}
  252. Branch: {json_stuff["default_branch"]}
  253. Issues: {json_stuff["open_issues"]}
  254. Languages: {', '.join(languages)}""")
  255. else:
  256. try:
  257. gitea_api = f"https://{query[2]}/api/v1/repos/{query[3]}/{query[4]}"
  258. data = requests.get(gitea_api)
  259. json_stuff = json.loads(data.text)
  260. mb_size= round(float(json_stuff["size"]) / 1024, 1)
  261. languages=[]
  262. data_languages = requests.get(f"https://{query[2]}/api/v1/repos/{query[3]}/{query[4]}/languages").json()
  263. for key in data_languages:
  264. languages.append(key)
  265. room.send_text(f"""Repo Name: {json_stuff["name"]}
  266. Author Name: {json_stuff["owner"]["username"]}
  267. Branch: {json_stuff["default_branch"]}
  268. Size: {mb_size} mb
  269. Issues: {json_stuff["open_issues_count"]}
  270. Pull Requests: {json_stuff["open_pr_counter"]}
  271. Languages: {', '.join(languages)}""")
  272. except:
  273. room.send_text("ERROR: Most likely this link isn't a gitea instance link or a github link. At the moment only gitea websites with gitea api support and github links are supported.")
  274. print(event['sender']+"\nPosted:\n"+event["content"]["body"])
  275. def wikipedia_callback(room, event):
  276. if event["content"]["body"].startswith("!fbw ") == True:
  277. message = event["content"]["body"].replace("!fbw ","")
  278. if event["content"]["body"].startswith("!wikipedia ") == True:
  279. message = event["content"]["body"].replace("!wikipedia ","")
  280. else:
  281. message = re.sub("^!fbw|\[(.*?)\]: !fbw ", "", event["content"]["body"])
  282. query = message
  283. wikipedia_api = f"https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch={query}&format=json"
  284. data = requests.get(wikipedia_api)
  285. json_stuff = json.loads(data.text)
  286. room.send_text(f"Searching wikipedia for {query}")
  287. for index, post in enumerate(json_stuff["query"]["search"]):
  288. clean = re.compile('<.*?>')
  289. room.send_text(re.sub(clean, '', post["title"]+"\n"+post["snippet"].replace("&quot;","\"")+"\n"+"https://en.wikipedia.org/w/index.php?curid="+str(post["pageid"])))
  290. if index == 2:
  291. break
  292. print(event['sender']+"\nPosted:\n"+event["content"]["body"])
  293. def uncyclopedia_callback(room, event):
  294. if event["content"]["body"].startswith("!fbU ") == True:
  295. message = event["content"]["body"].replace("!fbU ","")
  296. if event["content"]["body"].startswith("!uncyclopedia ") == True:
  297. message = event["content"]["body"].replace("!uncyclopedia ","")
  298. else:
  299. message = re.sub("^!fbU|\[(.*?)\]: !fbU ", "", event["content"]["body"])
  300. query = message
  301. wikipedia_api = f"https://en.uncyclopedia.co/w/api.php?action=query&list=search&srsearch={query}&format=json"
  302. data = requests.get(wikipedia_api)
  303. json_stuff = json.loads(data.text)
  304. room.send_text(f"Searching Uncyclopedia for {query}")
  305. for index, post in enumerate(json_stuff["query"]["search"]):
  306. clean = re.compile('<.*?>')
  307. room.send_text(re.sub(clean, '', post["title"]+"\n"+post["snippet"]+"\n"+"https://en.uncyclopedia.co/w/index.php?curid="+str(post["pageid"])))
  308. if index == 2:
  309. break
  310. print(event['sender']+"\nPosted:\n"+event["content"]["body"])
  311. def neow_callback(room, event):
  312. if event["content"]["body"].startswith("!fbn ") == True:
  313. message = event["content"]["body"].replace("!fbn ","")
  314. if event["content"]["body"].startswith("!neow ") == True:
  315. message = event["content"]["body"].replace("!neow ","")
  316. else:
  317. message = re.sub("^!fbn|\[(.*?)\]: !fbn ", "", event["content"]["body"])
  318. query = message
  319. neow_search = "https://neow.matthewevan.xyz/user/" + query + "/neowcoins.txt"
  320. data = requests.get(neow_search).text.rstrip()
  321. room.send_text(query + " has " + data + " coins")
  322. print(event['sender']+"\nPosted:\n"+event["content"]["body"])
  323. def crypto_callback(room, event):
  324. if event["content"]["body"].startswith("!fbc ") == True:
  325. message = event["content"]["body"].replace("!fbc ","")
  326. if event["content"]["body"].startswith("!crypto ") == True:
  327. message = event["content"]["body"].replace("!crypto ","")
  328. else:
  329. message = re.sub("^!fbc|\[(.*?)\]: !fbc ", "", event["content"]["body"])
  330. query = message
  331. ratesx_search = "https://rate.sx/" + query
  332. data = requests.get(ratesx_search).text.rstrip()
  333. if str(query).isalpha() == True:
  334. room.send_text("ERROR: Must specify <number of><currency>")
  335. else:
  336. room.send_text("In USD that's " + data)
  337. print(event['sender']+"\nPosted:\n"+event["content"]["body"])
  338. def peertube_callback(room, event):
  339. if event["content"]["body"].startswith("!fbp ") == True:
  340. message = event["content"]["body"].replace("!fbp ","")
  341. if event["content"]["body"].startswith("!peertube ") == True:
  342. message = event["content"]["body"].replace("!peertube ","")
  343. else:
  344. message = re.sub("^!fbp|\[(.*?)\]: !fbp ", "", event["content"]["body"])
  345. query = message
  346. peertube_search = "https://sepiasearch.org/api/v1/search/videos?search=" + query
  347. data = requests.get(peertube_search)
  348. json_stuff = json.loads(data.text)
  349. room.send_text("Searching PeerTube for " + query)
  350. for index, vid in enumerate(json_stuff["data"]):
  351. room.send_text(vid["name"]+"\n"+vid["channel"]["displayName"]+"\n"+vid["url"])
  352. if index == 2:
  353. break
  354. print(event['sender']+"\nPosted:\n"+event["content"]["body"])
  355. def invidious_callback(room, event):
  356. if event["content"]["body"].startswith("!fby ") == True:
  357. message = event["content"]["body"].replace("!fby ","")
  358. if event["content"]["body"].startswith("!invidious ") == True:
  359. message = event["content"]["body"].replace("!invidious ","")
  360. else:
  361. message = re.sub("^!fby|\[(.*?)\]: !fby ", "", event["content"]["body"])
  362. query = message
  363. invidious_search = "https://invidio.xamh.de/api/v1/search?q=" + query
  364. data = requests.get(invidious_search)
  365. json_stuff = json.loads(data.text)
  366. room.send_text("Searching YouTube for " + query)
  367. for index, vid in enumerate(json_stuff):
  368. room.send_text(vid["title"]+"\n"+vid["author"]+"\n"+"https://" + INVIDIOUS_INSTANCE + "/watch?v="+vid["videoId"])
  369. if index == 2:
  370. break
  371. print(event['sender']+"\nPosted:\n"+event["content"]["body"])
  372. def lbry_callback(room, event):
  373. size = str(3)
  374. if event["content"]["body"].startswith("!fbl ") == True:
  375. message = event["content"]["body"].replace("!fbl ","")
  376. if event["content"]["body"].startswith("!lbry ") == True:
  377. message = event["content"]["body"].replace("!lbry ","")
  378. else:
  379. message = re.sub("^!fbl|\[(.*?)\]: !fbl ", "", event["content"]["body"])
  380. query = message
  381. search = 'https://lighthouse.lbry.com/search?s=' + query + '&include=channel,title,&size=' + size
  382. data = requests.get(search)
  383. json_stuff = json.loads(data.text)
  384. room.send_text("Searching the LBRY network for: " + query)
  385. for x in json_stuff:
  386. pre = "https://lbry.ix.tc/"
  387. if x["channel"]:
  388. pre += x["channel"] + "/"
  389. url = pre + x["name"]
  390. room.send_text(x["title"]+"\n"+url)
  391. print(event['sender']+"\nPosted:\n"+event["content"]["body"])
  392. def youtube_callback(room, event):
  393. message = event["content"]["body"]
  394. if "youtu.be" in message:
  395. room.send_text("Hi, " + event['sender'] + " here is an Invidious link of the YouTube link you just sent:\n"+message.replace("youtu.be",INVIDIOUS_INSTANCE))
  396. else:
  397. room.send_text("Hi, " + event['sender'] + " here is an Invidious link of the YouTube link you just sent:\n"+message.replace("youtube.com",INVIDIOUS_INSTANCE))
  398. print(event['sender']+"\nPosted:\n"+event["content"]["body"])
  399. def twitter_callback(room, event):
  400. message = event["content"]["body"]
  401. if "mobile" in message:
  402. message = message.replace("mobile.twitter.com",NITTER_INSTANCE)
  403. else:
  404. message = message.replace("twitter.com",NITTER_INSTANCE)
  405. room.send_text("Hi, " + event['sender'] + " here is a nitter link of the Twitter link you just sent:\n"+message)
  406. print(event['sender']+"\nPosted:\n"+event["content"]["body"])
  407. def reddit_callback(room, event):
  408. message = event["content"]["body"]
  409. if "www.reddit.com" in message:
  410. room.send_text("Hi, " + event['sender'] + " here is a teddit link of the Reddit link you just sent:\n"+message.replace("www.reddit.com",TEDDIT_INSTANCE))
  411. else:
  412. room.send_text("Hi, " + event['sender'] + " here is a teddit link of the Reddit link you just sent:\n"+message.replace("reddit.com",TEDDIT_INSTANCE))
  413. print(event['sender']+"\nPosted:\n"+event["content"]["body"])
  414. def main():
  415. # Create an instance of the MatrixBotAPI
  416. bot = MatrixBotAPI(USERNAME, PASSWORD, SERVER)
  417. youtube_com_handler = MRegexHandler("^((?:https?:)?//)?((?:www|m).)?((?:youtube(-nocookie)?.com|youtu.be))(/(?:[\w-]+?v=|embed/|v/)?)([\w-]+)(\S+)?$", youtube_callback)
  418. bot.add_handler(youtube_com_handler)
  419. #youtube_com_handler = MRegexHandler("https://youtube.com/", youtube_callback)
  420. #bot.add_handler(youtube_com_handler)
  421. #youtube_handler = MRegexHandler("https://youtu.be/", youtube_callback)
  422. #bot.add_handler(youtube_handler)
  423. #youtube_www_handler = MRegexHandler("https://www.youtube.com/", youtube_callback)
  424. #bot.add_handler(youtube_www_handler)
  425. twitter_handler = MRegexHandler("https://mobile.twitter.com/|https://twitter.com/", twitter_callback)
  426. bot.add_handler(twitter_handler)
  427. reddit_handler = MRegexHandler("https://reddit.com/", reddit_callback)
  428. bot.add_handler(reddit_handler)
  429. www_reddit_handler = MRegexHandler("https://www.reddit.com/", reddit_callback)
  430. bot.add_handler(www_reddit_handler)
  431. invidious_handler = MRegexHandler("^!invidious", invidious_callback)
  432. bot.add_handler(invidious_handler)
  433. invidious_abrev_handler = MRegexHandler("^!fby|\[(.*?)\]: !fby", invidious_callback)
  434. bot.add_handler(invidious_abrev_handler)
  435. lbry_handler = MRegexHandler("^!lbry", lbry_callback)
  436. bot.add_handler(lbry_handler)
  437. lbry_abrev_handler = MRegexHandler("^!fbl|\[(.*?)\]: !fbl", lbry_callback)
  438. bot.add_handler(lbry_abrev_handler)
  439. crypto_handler = MRegexHandler("^!crypto", crypto_callback)
  440. bot.add_handler(crypto_handler)
  441. crypto_abrev_handler = MRegexHandler("^!fbc|\[(.*?)\]: !fbc", crypto_callback)
  442. bot.add_handler(crypto_abrev_handler)
  443. neow_handler = MRegexHandler("^!neow", neow_callback)
  444. bot.add_handler(neow_handler)
  445. neow_abrev_handler = MRegexHandler("^!fbn|\[(.*?)\]: !fbn", neow_callback)
  446. bot.add_handler(neow_abrev_handler)
  447. help_abrev_handler = MRegexHandler("^!fbh|\[(.*?)\]: !fbh", help_callback)
  448. bot.add_handler(help_abrev_handler)
  449. help_handler = MRegexHandler("^!help", help_callback)
  450. bot.add_handler(help_handler)
  451. peertube_handler = MRegexHandler("^!peertube", peertube_callback)
  452. bot.add_handler(peertube_handler)
  453. peertube_abrev_handler = MRegexHandler("^!fbp|\[(.*?)\]: !fbp", peertube_callback)
  454. bot.add_handler(peertube_abrev_handler)
  455. wikipedia_handler = MRegexHandler("^!wikipedia", wikipedia_callback)
  456. bot.add_handler(wikipedia_handler)
  457. wikipedia_abrev_handler = MRegexHandler("^!fbw|\[(.*?)\]: !fbw", wikipedia_callback)
  458. bot.add_handler(wikipedia_abrev_handler)
  459. uncyclopedia_handler = MRegexHandler("^!uncyclopedia", uncyclopedia_callback)
  460. bot.add_handler(uncyclopedia_handler)
  461. uncyclopedia_abrev_handler = MRegexHandler("^!fbU|\[(.*?)\]: !fbU", uncyclopedia_callback)
  462. bot.add_handler(uncyclopedia_abrev_handler)
  463. git_handler = MRegexHandler("^!git", git_callback)
  464. bot.add_handler(git_handler)
  465. git_abrev_handler = MRegexHandler("^!fbg|\[(.*?)\]: !fbg", git_callback)
  466. bot.add_handler(git_abrev_handler)
  467. urban_handler = MRegexHandler("^!urban", urban_callback)
  468. bot.add_handler(urban_handler)
  469. urban_abrev_handler = MRegexHandler("^!fbu|\[(.*?)\]: !fbu", urban_callback)
  470. bot.add_handler(urban_abrev_handler)
  471. dictionary_handler = MRegexHandler("^!dictionary", dictionary_callback)
  472. bot.add_handler(dictionary_handler)
  473. dictionary_abrev_handler = MRegexHandler("^!fbd|\[(.*?)\]: !fbd", dictionary_callback)
  474. bot.add_handler(dictionary_abrev_handler)
  475. tenor_abrev_handler = MRegexHandler("^!fbt|\[(.*?)\]: !fbt", tenor_callback)
  476. bot.add_handler(tenor_abrev_handler)
  477. tenor_handler = MRegexHandler("^!tenor", tenor_callback)
  478. bot.add_handler(tenor_handler)
  479. search_abrev_handler = MRegexHandler("^!fbs|\[(.*?)\]: !fbs", librex_callback)
  480. bot.add_handler(search_abrev_handler)
  481. search_handler = MRegexHandler("^!search", librex_callback)
  482. bot.add_handler(search_handler)
  483. meme_abrev_handler = MRegexHandler("^!fbm|\[(.*?)\]: !fbm", knowyourmeme_callback)
  484. bot.add_handler(meme_abrev_handler)
  485. meme_handler = MRegexHandler("^!meme", knowyourmeme_callback)
  486. bot.add_handler(meme_handler)
  487. garfield_abrev_handler = MRegexHandler("^!fbrg|\[(.*?)\]: !fbrg", garfield_callback)
  488. bot.add_handler(garfield_abrev_handler)
  489. garfield_handler = MRegexHandler("^!garfield", garfield_callback)
  490. bot.add_handler(garfield_handler)
  491. bot.start_polling()
  492. # Infinitely read stdin to stall main thread while the bot runs in other threads
  493. while True:
  494. input()
  495. def main_loop():
  496. while True:
  497. try:
  498. main()
  499. break
  500. except Exception as err:
  501. print(err)
  502. continue
  503. main_loop()