inlinefuncs.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. import aiohttp
  2. import json
  3. import sys
  4. import asyncio
  5. from pyrogram.types import (
  6. InlineQueryResultArticle,
  7. InputTextMessageContent,
  8. InlineKeyboardButton,
  9. InlineQueryResultPhoto,
  10. InputMediaPhoto
  11. )
  12. from googletrans import Translator
  13. from search_engine_parser import GoogleSearch
  14. from pykeyboard import InlineKeyboard
  15. from sys import version as pyver
  16. from motor import version as mongover
  17. from pyrogram import __version__ as pyrover, filters
  18. from time import time, ctime
  19. from wbb.modules.userbot import eval_executor_func
  20. from wbb.utils.fetch import fetch
  21. from wbb.utils.functions import test_speedtest, get_http_status_code
  22. from wbb.utils.formatter import convert_seconds_to_minutes as time_convert
  23. from wbb.utils.pastebin import paste
  24. from wbb.core.types import InlineQueryResultCachedDocument
  25. from wbb import (
  26. arq, app, app2, USERBOT_USERNAME,
  27. BOT_USERNAME, LOG_GROUP_ID, USERBOT_ID,
  28. SUDOERS
  29. )
  30. async def inline_help_func(__HELP__):
  31. buttons = InlineKeyboard(row_width=2)
  32. buttons.add(
  33. InlineKeyboardButton(
  34. 'Get More Help.',
  35. url=f"t.me/{BOT_USERNAME}?start=start"
  36. ),
  37. InlineKeyboardButton(
  38. "Go Inline!",
  39. switch_inline_query_current_chat=""
  40. )
  41. )
  42. answerss = [
  43. InlineQueryResultArticle(
  44. title="Inline Commands",
  45. description="Help Related To Inline Usage.",
  46. input_message_content=InputTextMessageContent(__HELP__),
  47. thumb_url="https://hamker.me/cy00x5x.png",
  48. reply_markup=buttons
  49. )
  50. ]
  51. answerss = await alive_function(answerss)
  52. return answerss
  53. async def alive_function(answers):
  54. buttons = InlineKeyboard(row_width=2)
  55. bot_state = 'Dead' if not await app.get_me() else 'Alive'
  56. ubot_state = 'Dead' if not await app2.get_me() else 'Alive'
  57. buttons.add(
  58. InlineKeyboardButton(
  59. 'Stats',
  60. callback_data='stats_callback'
  61. ),
  62. InlineKeyboardButton(
  63. "Go Inline!",
  64. switch_inline_query_current_chat=""
  65. )
  66. )
  67. msg = f"""
  68. **[William✨](https://github.com/thehamkercat/WilliamButcherBot):**
  69. **MainBot:** `{bot_state}`
  70. **UserBot:** `{ubot_state}`
  71. **Python:** `{pyver.split()[0]}`
  72. **Pyrogram:** `{pyrover}`
  73. **MongoDB:** `{mongover}`
  74. **Platform:** `{sys.platform}`
  75. **Profiles:** [BOT](t.me/{BOT_USERNAME}) | [UBOT](t.me/{USERBOT_USERNAME})
  76. """
  77. answers.append(
  78. InlineQueryResultArticle(
  79. title='Alive',
  80. description="Check Bot's Stats",
  81. thumb_url="https://static2.aniimg.com/upload/20170515/414/c/d/7/cd7EEF.jpg",
  82. input_message_content=InputTextMessageContent(
  83. msg,
  84. disable_web_page_preview=True
  85. ),
  86. reply_markup=buttons,
  87. )
  88. )
  89. return answers
  90. async def translate_func(answers, lang, tex):
  91. i = Translator().translate(tex, dest=lang)
  92. msg = f"""
  93. __**Translated to {lang}**__
  94. **INPUT:**
  95. {tex}
  96. **OUTPUT:**
  97. {i.text}"""
  98. answers.append(
  99. InlineQueryResultArticle(
  100. title=f'Translated to {lang}',
  101. description=i.text,
  102. input_message_content=InputTextMessageContent(msg)
  103. )
  104. )
  105. return answers
  106. async def urban_func(answers, text):
  107. results = await arq.urbandict(text)
  108. limit = 0
  109. for i in results:
  110. if limit > 48:
  111. break
  112. limit += 1
  113. try:
  114. msg = f"""
  115. **Query:** {text}
  116. **Definition:** __{results[i].definition}__
  117. **Example:** __{results[i].example}__"""
  118. answers.append(
  119. InlineQueryResultArticle(
  120. title=results[i].word,
  121. description=results[i].definition,
  122. input_message_content=InputTextMessageContent(msg)
  123. ))
  124. except KeyError:
  125. pass
  126. return answers
  127. async def google_search_func(answers, text):
  128. gresults = await GoogleSearch().async_search(text)
  129. limit = 0
  130. for i in gresults:
  131. if limit > 48:
  132. break
  133. limit += 1
  134. try:
  135. msg = f"""
  136. [{i['titles']}]({i['links']})
  137. {i['descriptions']}"""
  138. answers.append(
  139. InlineQueryResultArticle(
  140. title=i['titles'],
  141. description=i['descriptions'],
  142. input_message_content=InputTextMessageContent(
  143. msg,
  144. disable_web_page_preview=True
  145. )
  146. ))
  147. except KeyError:
  148. pass
  149. return answers
  150. async def wall_func(answers, text):
  151. results = await arq.wall(text)
  152. limit = 0
  153. for i in results:
  154. if limit > 48:
  155. break
  156. limit += 1
  157. try:
  158. answers.append(
  159. InlineQueryResultPhoto(
  160. photo_url=results[i].url_image,
  161. thumb_url=results[i].url_thumb,
  162. caption=f"[Source]({results[i].url_image})"
  163. ))
  164. except KeyError:
  165. pass
  166. return answers
  167. async def saavn_func(answers, text):
  168. buttons_list = []
  169. results = await arq.saavn(text)
  170. for i in results:
  171. buttons = InlineKeyboard(row_width=1)
  172. buttons.add(
  173. InlineKeyboardButton(
  174. 'Download | Play',
  175. url=results[i].media_url
  176. )
  177. )
  178. buttons_list.append(buttons)
  179. duration = await time_convert(results[i].duration)
  180. caption = f"""
  181. **Title:** {results[i].song}
  182. **Album:** {results[i].album}
  183. **Duration:** {duration}
  184. **Release:** {results[i].year}
  185. **Singers:** {results[i].singers}"""
  186. description = f"{results[i].album} | {duration} " \
  187. + f"| {results[i].singers} ({results[i].year})"
  188. try:
  189. answers.append(
  190. InlineQueryResultArticle(
  191. title=results[i].song,
  192. input_message_content=InputTextMessageContent(
  193. caption,
  194. disable_web_page_preview=True
  195. ),
  196. description=description,
  197. thumb_url=results[i].image,
  198. reply_markup=buttons_list[i]
  199. ))
  200. except (KeyError, ValueError):
  201. pass
  202. return answers
  203. async def deezer_func(answers, text):
  204. buttons_list = []
  205. results = await arq.deezer(text, 5)
  206. for i in results:
  207. buttons = InlineKeyboard(row_width=1)
  208. buttons.add(
  209. InlineKeyboardButton(
  210. 'Download | Play',
  211. url=results[i].url
  212. )
  213. )
  214. buttons_list.append(buttons)
  215. duration = await time_convert(results[i].duration)
  216. caption = f"""
  217. **Title:** {results[i].title}
  218. **Artist:** {results[i].artist}
  219. **Duration:** {duration}
  220. **Source:** [Deezer]({results[i].source})"""
  221. description = f"{results[i].artist} | {duration}"
  222. try:
  223. answers.append(
  224. InlineQueryResultArticle(
  225. title=results[i].title,
  226. thumb_url=results[i].thumbnail,
  227. description=description,
  228. input_message_content=InputTextMessageContent(
  229. caption,
  230. disable_web_page_preview=True
  231. ),
  232. reply_markup=buttons_list[i]
  233. ))
  234. except (KeyError, ValueError):
  235. pass
  236. return answers
  237. # Used my api key here, don't fuck with it
  238. async def shortify(url):
  239. if "." not in url:
  240. return
  241. header = {
  242. "Authorization": "Bearer ad39983fa42d0b19e4534f33671629a4940298dc",
  243. 'Content-Type': 'application/json'
  244. }
  245. payload = {
  246. "long_url": f"{url}"
  247. }
  248. payload = json.dumps(payload)
  249. async with aiohttp.ClientSession() as session:
  250. async with session.post("https://api-ssl.bitly.com/v4/shorten", headers=header, data=payload) as resp:
  251. data = await resp.json()
  252. msg = f"**Original Url:** {url}\n**Shortened Url:** {data['link']}"
  253. a = []
  254. b = InlineQueryResultArticle(
  255. title="Link Shortened!",
  256. description=data['link'],
  257. input_message_content=InputTextMessageContent(
  258. msg, disable_web_page_preview=True)
  259. )
  260. a.append(b)
  261. return a
  262. async def torrent_func(answers, text):
  263. results = await arq.torrent(text)
  264. limit = 0
  265. for i in results:
  266. if limit > 48:
  267. break
  268. title = results[i].name
  269. size = results[i].size
  270. seeds = results[i].seeds
  271. leechs = results[i].leechs
  272. upload_date = results[i].uploaded + " Ago"
  273. magnet = results[i].magnet
  274. caption = f"""
  275. **Title:** __{title}__
  276. **Size:** __{size}__
  277. **Seeds:** __{seeds}__
  278. **Leechs:** __{leechs}__
  279. **Uploaded:** __{upload_date}__
  280. **Magnet:** `{magnet}`"""
  281. description = f"{size} | {upload_date} | Seeds: {seeds}"
  282. try:
  283. answers.append(
  284. InlineQueryResultArticle(
  285. title=title,
  286. description=description,
  287. input_message_content=InputTextMessageContent(
  288. caption,
  289. disable_web_page_preview=True
  290. )
  291. )
  292. )
  293. limit += 1
  294. except (KeyError, ValueError):
  295. pass
  296. return answers
  297. async def youtube_func(answers, text):
  298. results = await arq.youtube(text)
  299. limit = 0
  300. for i in results:
  301. if limit > 48:
  302. break
  303. limit += 1
  304. buttons = InlineKeyboard(row_width=1)
  305. video_url = f"https://youtube.com{results[i].url_suffix}"
  306. buttons.add(
  307. InlineKeyboardButton(
  308. 'Watch',
  309. url=video_url
  310. )
  311. )
  312. caption = f"""
  313. **Title:** {results[i].title}
  314. **Views:** {results[i].views}
  315. **Channel:** {results[i].channel}
  316. **Duration:** {results[i].duration}
  317. **Uploaded:** {results[i].publish_time}
  318. **Description:** {results[i].long_desc}"""
  319. description = f"{results[i].views} | {results[i].channel} | " \
  320. + f"{results[i].duration} | {results[i].publish_time}"
  321. try:
  322. answers.append(
  323. InlineQueryResultArticle(
  324. title=results[i].title,
  325. thumb_url=results[i].thumbnails[0],
  326. description=description,
  327. input_message_content=InputTextMessageContent(
  328. caption,
  329. disable_web_page_preview=True
  330. ),
  331. reply_markup=buttons
  332. ))
  333. except (KeyError, ValueError):
  334. pass
  335. return answers
  336. async def lyrics_func(answers, text):
  337. song = await arq.lyrics(text)
  338. lyrics = song.lyrics
  339. song = lyrics.splitlines()
  340. song_name = song[0]
  341. artist = song[1]
  342. if len(lyrics) > 4095:
  343. lyrics = await paste(lyrics)
  344. lyrics = f"**LYRICS_TOO_LONG:** [URL]({lyrics})"
  345. msg = f"__{lyrics}__"
  346. answers.append(
  347. InlineQueryResultArticle(
  348. title=song_name,
  349. description=artist,
  350. input_message_content=InputTextMessageContent(msg)
  351. ))
  352. return answers
  353. async def eval_func(answers, text, user_id):
  354. if user_id not in SUDOERS:
  355. msg = "**ERROR**\n__THIS FEATURE IS ONLY FOR SUDO USERS__"
  356. answers.append(
  357. InlineQueryResultArticle(
  358. title="ERROR",
  359. description="THIS FEATURE IS ONLY FOR SUDO USERS",
  360. input_message_content=InputTextMessageContent(msg)
  361. ))
  362. return answers
  363. if str(text)[-1] != ":":
  364. msg = "**ERROR**\n__Put A ':' After The Code To Execute It__"
  365. answers.append(
  366. InlineQueryResultArticle(
  367. title="ERROR",
  368. description="Put A ':' After The Code To Execute It",
  369. input_message_content=InputTextMessageContent(msg)
  370. ))
  371. return answers
  372. text = text[0:-1]
  373. start_time = time()
  374. result = await eval_executor_func(text)
  375. output = result[1]
  376. result = result[0]
  377. msg = f"{result}"
  378. end_time = time()
  379. answers.append(
  380. InlineQueryResultArticle(
  381. title=f"Took {round(end_time - start_time)} Seconds.",
  382. description=output,
  383. input_message_content=InputTextMessageContent(msg)
  384. ))
  385. return answers
  386. async def github_user_func(answers, text):
  387. URL = f"https://api.github.com/users/{text}"
  388. result = await fetch(URL)
  389. buttons = InlineKeyboard(row_width=1)
  390. buttons.add(InlineKeyboardButton(
  391. text="Open On Github",
  392. url=f"https://github.com/{text}"
  393. ))
  394. caption = f"""
  395. **Info Of {result['name']}**
  396. **Username:** `{text}`
  397. **Bio:** `{result['bio']}`
  398. **Profile Link:** [Here]({result['html_url']})
  399. **Company:** `{result['company']}`
  400. **Created On:** `{result['created_at']}`
  401. **Repositories:** `{result['public_repos']}`
  402. **Blog:** `{result['blog']}`
  403. **Location:** `{result['location']}`
  404. **Followers:** `{result['followers']}`
  405. **Following:** `{result['following']}`"""
  406. answers.append(
  407. InlineQueryResultPhoto(
  408. photo_url=result['avatar_url'],
  409. caption=caption,
  410. reply_markup=buttons
  411. ))
  412. return answers
  413. async def github_repo_func(answers, text):
  414. text = text.replace("https://github.com/", "")
  415. text = text.replace("http://github.com/", "")
  416. if text[-1] == "/":
  417. text = text[0:-1]
  418. URL = f"https://api.github.com/repos/{text}"
  419. URL2 = f"https://api.github.com/repos/{text}/contributors"
  420. results = await asyncio.gather(fetch(URL), fetch(URL2))
  421. r = results[0]
  422. r1 = results[1]
  423. commits = 0
  424. for developer in r1:
  425. commits += developer['contributions']
  426. buttons = InlineKeyboard(row_width=1)
  427. buttons.add(
  428. InlineKeyboardButton(
  429. 'Open On Github',
  430. url=f"https://github.com/{text}"
  431. )
  432. )
  433. caption = f"""
  434. **Info Of {r['full_name']}**
  435. **Stars:** `{r['stargazers_count']}`
  436. **Watchers:** `{r['watchers_count']}`
  437. **Forks:** `{r['forks_count']}`
  438. **Commits:** `{commits}`
  439. **Is Fork:** `{r['fork']}`
  440. **Language:** `{r['language']}`
  441. **Contributors:** `{len(r1)}`
  442. **License:** `{r['license']['name']}`
  443. **Repo Owner:** [{r['owner']['login']}]({r['owner']['html_url']})
  444. **Created On:** `{r['created_at']}`
  445. **Homepage:** {r['homepage']}
  446. **Description:** __{r['description']}__"""
  447. answers.append(
  448. InlineQueryResultArticle(
  449. title="Found Repo",
  450. description=text,
  451. reply_markup=buttons,
  452. input_message_content=InputTextMessageContent(
  453. caption,
  454. disable_web_page_preview=True
  455. )
  456. ))
  457. return answers
  458. async def tg_search_func(answers, text, user_id):
  459. if user_id not in SUDOERS:
  460. msg = "**ERROR**\n__THIS FEATURE IS ONLY FOR SUDO USERS__"
  461. answers.append(
  462. InlineQueryResultArticle(
  463. title="ERROR",
  464. description="THIS FEATURE IS ONLY FOR SUDO USERS",
  465. input_message_content=InputTextMessageContent(msg)
  466. ))
  467. return answers
  468. if str(text)[-1] != ":":
  469. msg = "**ERROR**\n__Put A ':' After The Text To Search__"
  470. answers.append(
  471. InlineQueryResultArticle(
  472. title="ERROR",
  473. description="Put A ':' After The Text To Search",
  474. input_message_content=InputTextMessageContent(msg)
  475. ))
  476. return answers
  477. text = text[0:-1]
  478. async for message in app2.search_global(text, limit=49):
  479. buttons = InlineKeyboard(row_width=2)
  480. buttons.add(
  481. InlineKeyboardButton(
  482. text="Origin",
  483. url=message.link if message.link else "https://t.me/telegram"
  484. ),
  485. InlineKeyboardButton(
  486. text="Search again",
  487. switch_inline_query_current_chat="search"
  488. )
  489. )
  490. name = message.from_user.first_name if message.from_user.first_name else "NO NAME"
  491. caption = f"""
  492. **Query:** {text}
  493. **Name:** {str(name)} [`{message.from_user.id}`]
  494. **Chat:** {str(message.chat.title)} [`{message.chat.id}`]
  495. **Date:** {ctime(message.date)}
  496. **Text:** >>
  497. {message.text.markdown if message.text else message.caption if message.caption else '[NO_TEXT]'}
  498. """
  499. result = InlineQueryResultArticle(
  500. title=name,
  501. description=message.text if message.text else "[NO_TEXT]",
  502. reply_markup=buttons,
  503. input_message_content=InputTextMessageContent(
  504. caption,
  505. disable_web_page_preview=True
  506. )
  507. )
  508. answers.append(result)
  509. return answers
  510. async def music_inline_func(answers, query):
  511. chat_id = -1001445180719
  512. group_invite = "https://t.me/joinchat/vSDE2DuGK4Y4Nzll"
  513. try:
  514. messages = [m async for m in app2.search_messages(chat_id, query, filter="audio", limit=199)]
  515. except Exception as e:
  516. print(e)
  517. msg = f"You Need To Join Here With Your Bot And Userbot To Get Cached Music.\n{group_invite}"
  518. answers.append(
  519. InlineQueryResultArticle(
  520. title="ERROR",
  521. description="Click Here To Know More.",
  522. input_message_content=InputTextMessageContent(
  523. msg,
  524. disable_web_page_preview=True
  525. )
  526. )
  527. )
  528. return answers
  529. messages_ids_and_duration = []
  530. for f_ in messages:
  531. messages_ids_and_duration.append(
  532. {"message_id": f_.message_id,
  533. "duration": f_.audio.duration if f_.audio.duration else 0}
  534. )
  535. messages = list(
  536. {v["duration"]: v for v in messages_ids_and_duration}.values())
  537. messages_ids = []
  538. for ff_ in messages:
  539. messages_ids.append(ff_['message_id'])
  540. messages = await app.get_messages(chat_id, messages_ids[0:48])
  541. for message_ in messages:
  542. answers.append(
  543. InlineQueryResultCachedDocument(
  544. file_id=message_.audio.file_id,
  545. title=message_.audio.title
  546. )
  547. )
  548. return answers
  549. async def wiki_func(answers, text):
  550. data = await arq.wiki(text)
  551. msg = f"""
  552. **QUERY:**
  553. {data['title']}
  554. **ANSWER:**
  555. __{data['answer']}__"""
  556. answers.append(
  557. InlineQueryResultArticle(
  558. title=data['title'],
  559. description=data['answer'],
  560. input_message_content=InputTextMessageContent(msg)
  561. )
  562. )
  563. return answers
  564. async def speedtest_init(query):
  565. answers = []
  566. user_id = query.from_user.id
  567. if user_id not in SUDOERS:
  568. msg = "**ERROR**\n__THIS FEATURE IS ONLY FOR SUDO USERS__"
  569. answers.append(
  570. InlineQueryResultArticle(
  571. title="ERROR",
  572. description="THIS FEATURE IS ONLY FOR SUDO USERS",
  573. input_message_content=InputTextMessageContent(msg)
  574. ))
  575. return answers
  576. msg = "**Click The Button Below To Perform A Speedtest**"
  577. button = InlineKeyboard(row_width=1)
  578. button.add(InlineKeyboardButton(
  579. text="Test", callback_data="test_speedtest"))
  580. answers.append(
  581. InlineQueryResultArticle(
  582. title="Click Here",
  583. input_message_content=InputTextMessageContent(msg),
  584. reply_markup=button
  585. )
  586. )
  587. return answers
  588. """ callback query for the function above """
  589. @app.on_callback_query(filters.regex(f"test_speedtest"))
  590. async def test_speedtest_cq(_, cq):
  591. if cq.from_user.id not in SUDOERS:
  592. await cq.answer("This Isn't For You!")
  593. return
  594. inline_message_id = cq.inline_message_id
  595. await app.edit_inline_text(
  596. inline_message_id,
  597. "**Testing**"
  598. )
  599. download, upload, info = await test_speedtest()
  600. msg = f"""
  601. **Download:** `{download}`
  602. **Upload:** `{upload}`
  603. **Latency:** `{info['latency']} ms`
  604. **Country:** `{info['country']} [{info['cc']}]`
  605. **Latitude:** `{info['lat']}`
  606. **Longitude:** `{info['lon']}`
  607. """
  608. await app.edit_inline_text(
  609. inline_message_id,
  610. msg
  611. )
  612. pastebin_cache = {}
  613. async def pastebin_func(answers, link):
  614. global pastebin_cache
  615. link = link.split("/")
  616. if link[3] == "c":
  617. chat, message_id = int("-100" + link[4]), int(link[5])
  618. else:
  619. chat, message_id = link[3], link[4]
  620. m = await app.get_messages(chat, message_ids=int(message_id))
  621. if m.text not in pastebin_cache:
  622. link = await paste(m.text)
  623. preview = link + "/preview.png"
  624. pastebin_cache[m.text] = link
  625. status_code = await get_http_status_code(preview)
  626. i = 0
  627. while status_code != 200:
  628. if i == 5:
  629. break
  630. status_code = await get_http_status_code(preview)
  631. await asyncio.sleep(0.2)
  632. i += 1
  633. await app.send_photo(USERBOT_ID, preview) # To Pre-cache the media
  634. else:
  635. link = pastebin_cache[m.text]
  636. preview = link + "/preview.png"
  637. buttons = InlineKeyboard(row_width=1)
  638. buttons.add(InlineKeyboardButton(text="Paste Link", url=link))
  639. answers.append(
  640. InlineQueryResultPhoto(
  641. photo_url=preview,
  642. reply_markup=buttons
  643. )
  644. )
  645. return answers