inlinefuncs.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. """
  2. MIT License
  3. Copyright (c) present TheHamkerCat
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. """
  20. import asyncio
  21. import os
  22. import sys
  23. from html import escape
  24. from re import sub as re_sub
  25. from sys import version as pyver
  26. from time import ctime, time
  27. from fuzzysearch import find_near_matches
  28. from motor import version as mongover
  29. from pykeyboard import InlineKeyboard
  30. from pyrogram import __version__ as pyrover
  31. from pyrogram import filters, enums
  32. from pyrogram.raw.functions import Ping
  33. from pyrogram.types import (
  34. CallbackQuery,
  35. InlineKeyboardButton,
  36. InlineQueryResultArticle,
  37. InlineQueryResultPhoto,
  38. InlineQueryResultCachedDocument,
  39. InputTextMessageContent,
  40. )
  41. from search_engine_parser import GoogleSearch
  42. from wbb import (
  43. BOT_USERNAME,
  44. MESSAGE_DUMP_CHAT,
  45. SUDOERS,
  46. USERBOT_ID,
  47. USERBOT_NAME,
  48. USERBOT_USERNAME,
  49. app,
  50. app2,
  51. arq,
  52. )
  53. from wbb.core.keyboard import ikb
  54. from wbb.core.tasks import _get_tasks_text, all_tasks, rm_task
  55. from wbb.modules.info import get_chat_info, get_user_info
  56. from wbb.modules.music import download_youtube_audio
  57. from wbb.utils.functions import test_speedtest
  58. from wbb.utils.pastebin import paste
  59. keywords_list = [
  60. "image",
  61. "wall",
  62. "tmdb",
  63. "lyrics",
  64. "exec",
  65. "speedtest",
  66. "search",
  67. "ping",
  68. "tr",
  69. "ud",
  70. "yt",
  71. "info",
  72. "google",
  73. "torrent",
  74. "wiki",
  75. "music",
  76. "ytmusic",
  77. ]
  78. async def inline_help_func(__HELP__):
  79. buttons = InlineKeyboard(row_width=4)
  80. buttons.add(
  81. *[
  82. (InlineKeyboardButton(text=i, switch_inline_query_current_chat=i))
  83. for i in keywords_list
  84. ]
  85. )
  86. answerss = [
  87. InlineQueryResultArticle(
  88. title="Inline Commands",
  89. description="Help Related To Inline Usage.",
  90. input_message_content=InputTextMessageContent(
  91. "Click A Button To Get Started."
  92. ),
  93. thumb_url="https://hamker.me/cy00x5x.png",
  94. reply_markup=buttons,
  95. ),
  96. InlineQueryResultArticle(
  97. title="Github Repo",
  98. description="Get Github Respository Of Bot.",
  99. input_message_content=InputTextMessageContent(
  100. "https://github.com/thehamkercat/WilliamButcherBot"
  101. ),
  102. thumb_url="https://hamker.me/gjc9fo3.png",
  103. ),
  104. ]
  105. answerss = await alive_function(answerss)
  106. return answerss
  107. async def alive_function(answers):
  108. buttons = InlineKeyboard(row_width=2)
  109. bot_state = "Dead" if not await app.get_me() else "Alive"
  110. ubot_state = "Dead" if not await app2.get_me() else "Alive"
  111. buttons.add(
  112. InlineKeyboardButton("Stats", callback_data="stats_callback"),
  113. InlineKeyboardButton(
  114. "Go Inline!", switch_inline_query_current_chat=""
  115. ),
  116. )
  117. msg = f"""
  118. **[William✨](https://github.com/thehamkercat/WilliamButcherBot):**
  119. **MainBot:** `{bot_state}`
  120. **UserBot:** `{ubot_state}`
  121. **Python:** `{pyver.split()[0]}`
  122. **Pyrogram:** `{pyrover}`
  123. **MongoDB:** `{mongover}`
  124. **Platform:** `{sys.platform}`
  125. **Profiles:** [BOT](t.me/{BOT_USERNAME}) | [UBOT](t.me/{USERBOT_USERNAME})
  126. """
  127. answers.append(
  128. InlineQueryResultArticle(
  129. title="Alive",
  130. description="Check Bot's Stats",
  131. thumb_url="https://static2.aniimg.com/upload/20170515/414/c/d/7/cd7EEF.jpg",
  132. input_message_content=InputTextMessageContent(
  133. msg, disable_web_page_preview=True
  134. ),
  135. reply_markup=buttons,
  136. )
  137. )
  138. return answers
  139. async def translate_func(answers, lang, tex):
  140. result = await arq.translate(tex, lang)
  141. if not result.ok:
  142. answers.append(
  143. InlineQueryResultArticle(
  144. title="Error",
  145. description=result.result,
  146. input_message_content=InputTextMessageContent(result.result),
  147. )
  148. )
  149. return answers
  150. result = result.result
  151. msg = f"""
  152. __**Translated from {result.src} to {result.dest}**__
  153. **INPUT:**
  154. {tex}
  155. **OUTPUT:**
  156. {result.translatedText}"""
  157. answers.extend(
  158. [
  159. InlineQueryResultArticle(
  160. title=f"Translated from {result.src} to {result.dest}.",
  161. description=result.translatedText,
  162. input_message_content=InputTextMessageContent(msg),
  163. ),
  164. InlineQueryResultArticle(
  165. title=result.translatedText,
  166. input_message_content=InputTextMessageContent(
  167. result.translatedText
  168. ),
  169. ),
  170. ]
  171. )
  172. return answers
  173. async def urban_func(answers, text):
  174. results = await arq.urbandict(text)
  175. if not results.ok:
  176. answers.append(
  177. InlineQueryResultArticle(
  178. title="Error",
  179. description=results.result,
  180. input_message_content=InputTextMessageContent(results.result),
  181. )
  182. )
  183. return answers
  184. results = results.result[0:48]
  185. for i in results:
  186. clean = lambda x: re_sub(r"[\[\]]", "", x)
  187. msg = f"""
  188. **Query:** {text}
  189. **Definition:** __{clean(i.definition)}__
  190. **Example:** __{clean(i.example)}__"""
  191. answers.append(
  192. InlineQueryResultArticle(
  193. title=i.word,
  194. description=clean(i.definition),
  195. input_message_content=InputTextMessageContent(msg),
  196. )
  197. )
  198. return answers
  199. async def google_search_func(answers, text):
  200. gresults = await GoogleSearch().async_search(text)
  201. limit = 0
  202. for i in gresults:
  203. if limit > 48:
  204. break
  205. limit += 1
  206. try:
  207. msg = f"""
  208. [{i['titles']}]({i['links']})
  209. {i['descriptions']}"""
  210. answers.append(
  211. InlineQueryResultArticle(
  212. title=i["titles"],
  213. description=i["descriptions"],
  214. input_message_content=InputTextMessageContent(
  215. msg, disable_web_page_preview=True
  216. ),
  217. )
  218. )
  219. except KeyError:
  220. pass
  221. return answers
  222. async def wall_func(answers, text):
  223. results = await arq.wall(text)
  224. if not results.ok:
  225. answers.append(
  226. InlineQueryResultArticle(
  227. title="Error",
  228. description=results.result,
  229. input_message_content=InputTextMessageContent(results.result),
  230. )
  231. )
  232. return answers
  233. results = results.result[0:48]
  234. for i in results:
  235. answers.append(
  236. InlineQueryResultPhoto(
  237. photo_url=i.url_image,
  238. thumb_url=i.url_thumb,
  239. caption=f"[Source]({i.url_image})",
  240. )
  241. )
  242. return answers
  243. async def torrent_func(answers, text):
  244. results = await arq.torrent(text)
  245. if not results.ok:
  246. answers.append(
  247. InlineQueryResultArticle(
  248. title="Error",
  249. description=results.result,
  250. input_message_content=InputTextMessageContent(results.result),
  251. )
  252. )
  253. return answers
  254. results = results.result[0:48]
  255. for i in results:
  256. title = i.name
  257. size = i.size
  258. seeds = i.seeds
  259. leechs = i.leechs
  260. upload_date = i.uploaded
  261. magnet = i.magnet
  262. caption = f"""
  263. **Title:** __{title}__
  264. **Size:** __{size}__
  265. **Seeds:** __{seeds}__
  266. **Leechs:** __{leechs}__
  267. **Uploaded:** __{upload_date}__
  268. **Magnet:** `{magnet}`"""
  269. description = f"{size} | {upload_date} | Seeds: {seeds}"
  270. answers.append(
  271. InlineQueryResultArticle(
  272. title=title,
  273. description=description,
  274. input_message_content=InputTextMessageContent(
  275. caption, disable_web_page_preview=True
  276. ),
  277. )
  278. )
  279. pass
  280. return answers
  281. async def youtube_func(answers, text):
  282. results = await arq.youtube(text)
  283. if not results.ok:
  284. answers.append(
  285. InlineQueryResultArticle(
  286. title="Error",
  287. description=results.result,
  288. input_message_content=InputTextMessageContent(results.result),
  289. )
  290. )
  291. return answers
  292. results = results.result[0:48]
  293. for i in results:
  294. buttons = InlineKeyboard(row_width=1)
  295. video_url = f"https://youtube.com{i.url_suffix}"
  296. buttons.add(InlineKeyboardButton("Watch", url=video_url))
  297. caption = f"""
  298. **Title:** {i.title}
  299. **Views:** {i.views}
  300. **Channel:** {i.channel}
  301. **Duration:** {i.duration}
  302. **Uploaded:** {i.publish_time}
  303. **Description:** {i.long_desc}"""
  304. description = (
  305. f"{i.views} | {i.channel} | {i.duration} | {i.publish_time}"
  306. )
  307. answers.append(
  308. InlineQueryResultArticle(
  309. title=i.title,
  310. thumb_url=i.thumbnails[0],
  311. description=description,
  312. input_message_content=InputTextMessageContent(
  313. caption, disable_web_page_preview=True
  314. ),
  315. reply_markup=buttons,
  316. )
  317. )
  318. return answers
  319. async def lyrics_func(answers, text):
  320. resp = await arq.lyrics(text)
  321. if not resp.ok:
  322. answers.append(
  323. InlineQueryResultArticle(
  324. title="Error",
  325. description=resp.result,
  326. input_message_content=InputTextMessageContent(resp.result),
  327. )
  328. )
  329. return answers
  330. songs = resp.result
  331. for song in songs:
  332. song_name = song['song']
  333. artist = song['artist']
  334. lyrics = song['lyrics']
  335. msg = f"**{song_name}** | **{artist}**\n\n__{lyrics}__"
  336. if len(msg) > 4095:
  337. msg = await paste(msg)
  338. msg = f"**LYRICS_TOO_LONG:** [URL]({msg})"
  339. answers.append(
  340. InlineQueryResultArticle(
  341. title=song_name,
  342. description=artist,
  343. input_message_content=InputTextMessageContent(msg),
  344. )
  345. )
  346. return answers
  347. async def tg_search_func(answers, text, user_id):
  348. if user_id not in SUDOERS:
  349. msg = "**ERROR**\n__THIS FEATURE IS ONLY FOR SUDO USERS__"
  350. answers.append(
  351. InlineQueryResultArticle(
  352. title="ERROR",
  353. description="THIS FEATURE IS ONLY FOR SUDO USERS",
  354. input_message_content=InputTextMessageContent(msg),
  355. )
  356. )
  357. return answers
  358. if str(text)[-1] != ":":
  359. msg = "**ERROR**\n__Put A ':' After The Text To Search__"
  360. answers.append(
  361. InlineQueryResultArticle(
  362. title="ERROR",
  363. description="Put A ':' After The Text To Search",
  364. input_message_content=InputTextMessageContent(msg),
  365. )
  366. )
  367. return answers
  368. text = text[0:-1]
  369. async for message in app2.search_global(text, limit=49):
  370. buttons = InlineKeyboard(row_width=2)
  371. buttons.add(
  372. InlineKeyboardButton(
  373. text="Origin",
  374. url=message.link if message.link else "https://t.me/telegram",
  375. ),
  376. InlineKeyboardButton(
  377. text="Search again",
  378. switch_inline_query_current_chat="search",
  379. ),
  380. )
  381. name = (
  382. message.from_user.first_name
  383. if message.from_user.first_name
  384. else "NO NAME"
  385. )
  386. caption = f"""
  387. **Query:** {text}
  388. **Name:** {str(name)} [`{message.from_user.id}`]
  389. **Chat:** {str(message.chat.title)} [`{message.chat.id}`]
  390. **Date:** {ctime(message.date)}
  391. **Text:** >>
  392. {message.text.markdown if message.text else message.caption if message.caption else '[NO_TEXT]'}
  393. """
  394. result = InlineQueryResultArticle(
  395. title=name,
  396. description=message.text if message.text else "[NO_TEXT]",
  397. reply_markup=buttons,
  398. input_message_content=InputTextMessageContent(
  399. caption, disable_web_page_preview=True
  400. ),
  401. )
  402. answers.append(result)
  403. return answers
  404. async def music_inline_func(answers, query):
  405. chat_id = -1001445180719
  406. group_invite = "https://t.me/joinchat/vSDE2DuGK4Y4Nzll"
  407. try:
  408. messages = [
  409. m
  410. async for m in app2.search_messages(
  411. chat_id,
  412. query,
  413. filter=enums.MessagesFilter.AUDIO,
  414. limit=100
  415. )
  416. ]
  417. except Exception as e:
  418. print(e)
  419. msg = f"You Need To Join Here With Your Bot And Userbot To Get Cached Music.\n{group_invite}"
  420. answers.append(
  421. InlineQueryResultArticle(
  422. title="ERROR",
  423. description="Click Here To Know More.",
  424. input_message_content=InputTextMessageContent(
  425. msg, disable_web_page_preview=True
  426. ),
  427. )
  428. )
  429. return answers
  430. messages_ids_and_duration = []
  431. for f_ in messages:
  432. messages_ids_and_duration.append(
  433. {
  434. "message_id": f_.message_id,
  435. "duration": f_.audio.duration if f_.audio.duration else 0,
  436. }
  437. )
  438. messages = list(
  439. {v["duration"]: v for v in messages_ids_and_duration}.values()
  440. )
  441. messages_ids = [ff_["message_id"] for ff_ in messages]
  442. messages = await app.get_messages(chat_id, messages_ids[0:48])
  443. return [
  444. InlineQueryResultCachedDocument(
  445. file_id=message_.audio.file_id,
  446. title=message_.audio.title,
  447. )
  448. for message_ in messages
  449. ]
  450. async def wiki_func(answers, text):
  451. data = await arq.wiki(text)
  452. if not data.ok:
  453. answers.append(
  454. InlineQueryResultArticle(
  455. title="Error",
  456. description=data.result,
  457. input_message_content=InputTextMessageContent(data.result),
  458. )
  459. )
  460. return answers
  461. data = data.result
  462. msg = f"""
  463. **QUERY:**
  464. {data.title}
  465. **ANSWER:**
  466. __{data.answer}__"""
  467. answers.append(
  468. InlineQueryResultArticle(
  469. title=data.title,
  470. description=data.answer,
  471. input_message_content=InputTextMessageContent(msg),
  472. )
  473. )
  474. return answers
  475. async def speedtest_init(query):
  476. answers = []
  477. user_id = query.from_user.id
  478. if user_id not in SUDOERS:
  479. msg = "**ERROR**\n__THIS FEATURE IS ONLY FOR SUDO USERS__"
  480. answers.append(
  481. InlineQueryResultArticle(
  482. title="ERROR",
  483. description="THIS FEATURE IS ONLY FOR SUDO USERS",
  484. input_message_content=InputTextMessageContent(msg),
  485. )
  486. )
  487. return answers
  488. msg = "**Click The Button Below To Perform A Speedtest**"
  489. button = InlineKeyboard(row_width=1)
  490. button.add(
  491. InlineKeyboardButton(text="Test", callback_data="test_speedtest")
  492. )
  493. answers.append(
  494. InlineQueryResultArticle(
  495. title="Click Here",
  496. input_message_content=InputTextMessageContent(msg),
  497. reply_markup=button,
  498. )
  499. )
  500. return answers
  501. # CallbackQuery for the function above
  502. @app.on_callback_query(filters.regex("test_speedtest"))
  503. async def test_speedtest_cq(_, cq):
  504. if cq.from_user.id not in SUDOERS:
  505. return await cq.answer("This Isn't For You!")
  506. inline_message_id = cq.inline_message_id
  507. await app.edit_inline_text(inline_message_id, "**Testing**")
  508. loop = asyncio.get_running_loop()
  509. download, upload, info = await loop.run_in_executor(None, test_speedtest)
  510. msg = f"""
  511. **Download:** `{download}`
  512. **Upload:** `{upload}`
  513. **Latency:** `{info['latency']} ms`
  514. **Country:** `{info['country']} [{info['cc']}]`
  515. **Latitude:** `{info['lat']}`
  516. **Longitude:** `{info['lon']}`
  517. """
  518. await app.edit_inline_text(inline_message_id, msg)
  519. async def pmpermit_func(answers, user_id, victim):
  520. if user_id != USERBOT_ID:
  521. return
  522. caption = f"Hi, I'm {USERBOT_NAME}, What are you here for?, You'll be blocked if you send more than 5 messages."
  523. buttons = InlineKeyboard(row_width=2)
  524. buttons.add(
  525. InlineKeyboardButton(
  526. text="To Scam You", callback_data="pmpermit to_scam_you a"
  527. ),
  528. InlineKeyboardButton(
  529. text="For promotion",
  530. callback_data="pmpermit to_scam_you a",
  531. ),
  532. InlineKeyboardButton(
  533. text="Approve me", callback_data="pmpermit approve_me a"
  534. ),
  535. InlineKeyboardButton(
  536. text="Approve", callback_data=f"pmpermit approve {victim}"
  537. ),
  538. InlineKeyboardButton(
  539. text="Block & Delete",
  540. callback_data=f"pmpermit block {victim}",
  541. ),
  542. )
  543. answers.append(
  544. InlineQueryResultArticle(
  545. title="do_not_click_here",
  546. reply_markup=buttons,
  547. input_message_content=InputTextMessageContent(caption),
  548. )
  549. )
  550. return answers
  551. async def ping_func(answers):
  552. ping = Ping(ping_id=app.rnd_id())
  553. t1 = time()
  554. await app.send(ping)
  555. t2 = time()
  556. ping = f"{str(round((t2 - t1) * 1000, 2))} ms"
  557. answers.append(
  558. InlineQueryResultArticle(
  559. title=ping,
  560. input_message_content=InputTextMessageContent(f"__**{ping}**__"),
  561. )
  562. )
  563. return answers
  564. async def yt_music_func(answers, url):
  565. arq_resp = await arq.youtube(url)
  566. loop = asyncio.get_running_loop()
  567. music = await loop.run_in_executor(None, download_youtube_audio, arq_resp)
  568. if not music:
  569. msg = "**ERROR**\n__MUSIC TOO LONG__"
  570. answers.append(
  571. InlineQueryResultArticle(
  572. title="ERROR",
  573. description="MUSIC TOO LONG",
  574. input_message_content=InputTextMessageContent(msg),
  575. )
  576. )
  577. return answers
  578. (
  579. title,
  580. performer,
  581. duration,
  582. audio,
  583. thumbnail,
  584. ) = music
  585. m = await app.send_audio(
  586. MESSAGE_DUMP_CHAT,
  587. audio,
  588. title=title,
  589. duration=duration,
  590. performer=performer,
  591. thumb=thumbnail,
  592. )
  593. os.remove(audio)
  594. os.remove(thumbnail)
  595. answers.append(
  596. InlineQueryResultCachedDocument(title=title, file_id=m.audio.file_id)
  597. )
  598. return answers
  599. async def info_inline_func(answers, peer):
  600. not_found = InlineQueryResultArticle(
  601. title="PEER NOT FOUND",
  602. input_message_content=InputTextMessageContent("PEER NOT FOUND"),
  603. )
  604. try:
  605. user = await app.get_users(peer)
  606. caption, _ = await get_user_info(user, True)
  607. except IndexError:
  608. try:
  609. chat = await app.get_chat(peer)
  610. caption, _ = await get_chat_info(chat, True)
  611. except Exception:
  612. return [not_found]
  613. except Exception:
  614. return [not_found]
  615. answers.append(
  616. InlineQueryResultArticle(
  617. title="Found Peer.",
  618. input_message_content=InputTextMessageContent(
  619. caption, disable_web_page_preview=True
  620. ),
  621. )
  622. )
  623. return answers
  624. async def tmdb_func(answers, query):
  625. response = await arq.tmdb(query)
  626. if not response.ok:
  627. answers.append(
  628. InlineQueryResultArticle(
  629. title="Error",
  630. description=response.result,
  631. input_message_content=InputTextMessageContent(response.result),
  632. )
  633. )
  634. return answers
  635. results = response.result[:49]
  636. for result in results:
  637. if not result.poster and not result.backdrop:
  638. continue
  639. if not result.genre:
  640. genre = None
  641. else:
  642. genre = " | ".join(result.genre)
  643. description = result.overview[0:900] if result.overview else "None"
  644. caption = f"""
  645. **{result.title}**
  646. **Type:** {result.type}
  647. **Rating:** {result.rating}
  648. **Genre:** {genre}
  649. **Release Date:** {result.releaseDate}
  650. **Description:** __{description}__
  651. """
  652. buttons = InlineKeyboard(row_width=1)
  653. buttons.add(
  654. InlineKeyboardButton(
  655. "Search Again",
  656. switch_inline_query_current_chat="tmdb",
  657. )
  658. )
  659. answers.append(
  660. InlineQueryResultPhoto(
  661. photo_url=result.backdrop
  662. if result.backdrop
  663. else result.poster,
  664. caption=caption,
  665. title=result.title,
  666. description=f"{genre} • {result.releaseDate} • {result.rating} • {description}",
  667. reply_markup=buttons,
  668. )
  669. )
  670. return answers
  671. async def image_func(answers, query):
  672. results = await arq.image(query)
  673. if not results.ok:
  674. answers.append(
  675. InlineQueryResultArticle(
  676. title="Error",
  677. description=results.result,
  678. input_message_content=InputTextMessageContent(results.result),
  679. )
  680. )
  681. return answers
  682. results = results.result[:49]
  683. buttons = InlineKeyboard(row_width=2)
  684. buttons.add(
  685. InlineKeyboardButton(
  686. text="Search again",
  687. switch_inline_query_current_chat="image",
  688. ),
  689. )
  690. for i in results:
  691. answers.append(
  692. InlineQueryResultPhoto(
  693. title=i.title,
  694. photo_url=i.url,
  695. thumb_url=i.url,
  696. reply_markup=buttons,
  697. )
  698. )
  699. return answers
  700. async def execute_code(query):
  701. text = query.query.strip()
  702. offset = int((query.offset or 0))
  703. answers = []
  704. languages = (await arq.execute()).result
  705. if len(text.split()) == 1:
  706. answers = [
  707. InlineQueryResultArticle(
  708. title=lang,
  709. input_message_content=InputTextMessageContent(lang),
  710. )
  711. for lang in languages
  712. ][offset: offset + 25]
  713. await query.answer(
  714. next_offset=str(offset + 25),
  715. results=answers,
  716. cache_time=1,
  717. )
  718. elif len(text.split()) == 2:
  719. text = text.split()[1].strip()
  720. languages = list(
  721. filter(
  722. lambda x: find_near_matches(text, x, max_l_dist=1),
  723. languages,
  724. )
  725. )
  726. answers.extend(
  727. [
  728. InlineQueryResultArticle(
  729. title=lang,
  730. input_message_content=InputTextMessageContent(lang),
  731. )
  732. for lang in languages
  733. ][:49]
  734. )
  735. else:
  736. lang = text.split()[1]
  737. code = text.split(None, 2)[2]
  738. response = await arq.execute(lang, code)
  739. if not response.ok:
  740. answers.append(
  741. InlineQueryResultArticle(
  742. title="Error",
  743. input_message_content=InputTextMessageContent(
  744. response.result
  745. ),
  746. )
  747. )
  748. else:
  749. res = response.result
  750. stdout, stderr = escape(res.stdout), escape(res.stderr)
  751. output = stdout or stderr
  752. out = "STDOUT" if stdout else ("STDERR" if stderr else "No output")
  753. msg = f"""
  754. **{lang.capitalize()}:**
  755. ```{code}```
  756. **{out}:**
  757. ```{output}```
  758. """
  759. answers.append(
  760. InlineQueryResultArticle(
  761. title="Executed",
  762. description=output[:20],
  763. input_message_content=InputTextMessageContent(msg),
  764. )
  765. )
  766. await query.answer(results=answers, cache_time=1)
  767. async def task_inline_func(user_id):
  768. if user_id not in SUDOERS:
  769. return
  770. tasks = all_tasks()
  771. text = await _get_tasks_text()
  772. keyb = None
  773. if tasks:
  774. keyb = ikb(
  775. {i: f"cancel_task_{i}" for i in list(tasks.keys())},
  776. row_width=4,
  777. )
  778. return [
  779. InlineQueryResultArticle(
  780. title="Tasks",
  781. reply_markup=keyb,
  782. input_message_content=InputTextMessageContent(
  783. text,
  784. ),
  785. )
  786. ]
  787. @app.on_callback_query(filters.regex("^cancel_task_"))
  788. async def cancel_task_button(_, query: CallbackQuery):
  789. user_id = query.from_user.id
  790. if user_id not in SUDOERS:
  791. return await query.answer("This is not for you.")
  792. task_id = int(query.data.split("_")[-1])
  793. await rm_task(task_id)
  794. tasks = all_tasks()
  795. text = await _get_tasks_text()
  796. keyb = None
  797. if tasks:
  798. keyb = ikb({i: f"cancel_task_{i}" for i in list(tasks.keys())})
  799. await app.edit_inline_text(
  800. query.inline_message_id,
  801. text,
  802. )
  803. if keyb:
  804. await app.edit_inline_reply_markup(
  805. query.inline_message_id,
  806. keyb,
  807. )