inlinefuncs.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  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
  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. async def webss(url):
  238. start_time = time()
  239. if "." not in url:
  240. return
  241. screenshot = await fetch(f"https://patheticprogrammers.cf/ss?site={url}")
  242. end_time = time()
  243. try:
  244. m = await app.send_photo(LOG_GROUP_ID, photo=screenshot['url'])
  245. except TypeError:
  246. return
  247. await m.delete()
  248. a = []
  249. pic = InlineQueryResultPhoto(
  250. photo_url=screenshot['url'],
  251. caption=(f"`{url}`\n__Took {round(end_time - start_time)} Seconds.__")
  252. )
  253. a.append(pic)
  254. return a
  255. # Used my api key here, don't fuck with it
  256. async def shortify(url):
  257. if "." not in url:
  258. return
  259. header = {
  260. "Authorization": "Bearer ad39983fa42d0b19e4534f33671629a4940298dc",
  261. 'Content-Type': 'application/json'
  262. }
  263. payload = {
  264. "long_url": f"{url}"
  265. }
  266. payload = json.dumps(payload)
  267. async with aiohttp.ClientSession() as session:
  268. async with session.post("https://api-ssl.bitly.com/v4/shorten", headers=header, data=payload) as resp:
  269. data = await resp.json()
  270. msg = f"**Original Url:** {url}\n**Shortened Url:** {data['link']}"
  271. a = []
  272. b = InlineQueryResultArticle(
  273. title="Link Shortened!",
  274. description=data['link'],
  275. input_message_content=InputTextMessageContent(
  276. msg, disable_web_page_preview=True)
  277. )
  278. a.append(b)
  279. return a
  280. async def torrent_func(answers, text):
  281. results = await arq.torrent(text)
  282. limit = 0
  283. for i in results:
  284. if limit > 48:
  285. break
  286. title = results[i].name
  287. size = results[i].size
  288. seeds = results[i].seeds
  289. leechs = results[i].leechs
  290. upload_date = results[i].uploaded + " Ago"
  291. magnet = results[i].magnet
  292. caption = f"""
  293. **Title:** __{title}__
  294. **Size:** __{size}__
  295. **Seeds:** __{seeds}__
  296. **Leechs:** __{leechs}__
  297. **Uploaded:** __{upload_date}__
  298. **Magnet:** `{magnet}`"""
  299. description = f"{size} | {upload_date} | Seeds: {seeds}"
  300. try:
  301. answers.append(
  302. InlineQueryResultArticle(
  303. title=title,
  304. description=description,
  305. input_message_content=InputTextMessageContent(
  306. caption,
  307. disable_web_page_preview=True
  308. )
  309. )
  310. )
  311. limit += 1
  312. except (KeyError, ValueError):
  313. pass
  314. return answers
  315. async def youtube_func(answers, text):
  316. results = await arq.youtube(text)
  317. limit = 0
  318. for i in results:
  319. if limit > 48:
  320. break
  321. limit += 1
  322. buttons = InlineKeyboard(row_width=1)
  323. video_url = f"https://youtube.com{results[i].url_suffix}"
  324. buttons.add(
  325. InlineKeyboardButton(
  326. 'Watch',
  327. url=video_url
  328. )
  329. )
  330. caption = f"""
  331. **Title:** {results[i].title}
  332. **Views:** {results[i].views}
  333. **Channel:** {results[i].channel}
  334. **Duration:** {results[i].duration}
  335. **Uploaded:** {results[i].publish_time}
  336. **Description:** {results[i].long_desc}"""
  337. description = f"{results[i].views} | {results[i].channel} | " \
  338. + f"{results[i].duration} | {results[i].publish_time}"
  339. try:
  340. answers.append(
  341. InlineQueryResultArticle(
  342. title=results[i].title,
  343. thumb_url=results[i].thumbnails[0],
  344. description=description,
  345. input_message_content=InputTextMessageContent(
  346. caption,
  347. disable_web_page_preview=True
  348. ),
  349. reply_markup=buttons
  350. ))
  351. except (KeyError, ValueError):
  352. pass
  353. return answers
  354. async def lyrics_func(answers, text):
  355. song = await arq.lyrics(text)
  356. lyrics = song.lyrics
  357. song = lyrics.splitlines()
  358. song_name = song[0]
  359. artist = song[1]
  360. if len(lyrics) > 4095:
  361. lyrics = await paste(lyrics)
  362. lyrics = f"**LYRICS_TOO_LONG:** [URL]({lyrics})"
  363. msg = f"__{lyrics}__"
  364. answers.append(
  365. InlineQueryResultArticle(
  366. title=song_name,
  367. description=artist,
  368. input_message_content=InputTextMessageContent(msg)
  369. ))
  370. return answers
  371. async def eval_func(answers, text, user_id):
  372. if user_id not in SUDOERS:
  373. msg = "**ERROR**\n__THIS FEATURE IS ONLY FOR SUDO USERS__"
  374. answers.append(
  375. InlineQueryResultArticle(
  376. title="ERROR",
  377. description="THIS FEATURE IS ONLY FOR SUDO USERS",
  378. input_message_content=InputTextMessageContent(msg)
  379. ))
  380. return answers
  381. if str(text)[-1] != ":":
  382. msg = "**ERROR**\n__Put A ':' After The Code To Execute It__"
  383. answers.append(
  384. InlineQueryResultArticle(
  385. title="ERROR",
  386. description="Put A ':' After The Code To Execute It",
  387. input_message_content=InputTextMessageContent(msg)
  388. ))
  389. return answers
  390. text = text[0:-1]
  391. start_time = time()
  392. result = await eval_executor_func(text)
  393. output = result[1]
  394. result = result[0]
  395. msg = f"{result}"
  396. end_time = time()
  397. answers.append(
  398. InlineQueryResultArticle(
  399. title=f"Took {round(end_time - start_time)} Seconds.",
  400. description=output,
  401. input_message_content=InputTextMessageContent(msg)
  402. ))
  403. return answers
  404. async def github_user_func(answers, text):
  405. URL = f"https://api.github.com/users/{text}"
  406. result = await fetch(URL)
  407. buttons = InlineKeyboard(row_width=1)
  408. buttons.add(InlineKeyboardButton(
  409. text="Open On Github",
  410. url=f"https://github.com/{text}"
  411. ))
  412. caption = f"""
  413. **Info Of {result['name']}**
  414. **Username:** `{text}`
  415. **Bio:** `{result['bio']}`
  416. **Profile Link:** [Here]({result['html_url']})
  417. **Company:** `{result['company']}`
  418. **Created On:** `{result['created_at']}`
  419. **Repositories:** `{result['public_repos']}`
  420. **Blog:** `{result['blog']}`
  421. **Location:** `{result['location']}`
  422. **Followers:** `{result['followers']}`
  423. **Following:** `{result['following']}`"""
  424. answers.append(
  425. InlineQueryResultPhoto(
  426. photo_url=result['avatar_url'],
  427. caption=caption,
  428. reply_markup=buttons
  429. ))
  430. return answers
  431. async def github_repo_func(answers, text):
  432. text = text.replace("https://github.com/", "")
  433. text = text.replace("http://github.com/", "")
  434. if text[-1] == "/":
  435. text = text[0:-1]
  436. URL = f"https://api.github.com/repos/{text}"
  437. URL2 = f"https://api.github.com/repos/{text}/contributors"
  438. results = await asyncio.gather(fetch(URL), fetch(URL2))
  439. r = results[0]
  440. r1 = results[1]
  441. commits = 0
  442. for developer in r1:
  443. commits += developer['contributions']
  444. buttons = InlineKeyboard(row_width=1)
  445. buttons.add(
  446. InlineKeyboardButton(
  447. 'Open On Github',
  448. url=f"https://github.com/{text}"
  449. )
  450. )
  451. caption = f"""
  452. **Info Of {r['full_name']}**
  453. **Stars:** `{r['stargazers_count']}`
  454. **Watchers:** `{r['watchers_count']}`
  455. **Forks:** `{r['forks_count']}`
  456. **Commits:** `{commits}`
  457. **Is Fork:** `{r['fork']}`
  458. **Language:** `{r['language']}`
  459. **Contributors:** `{len(r1)}`
  460. **License:** `{r['license']['name']}`
  461. **Repo Owner:** [{r['owner']['login']}]({r['owner']['html_url']})
  462. **Created On:** `{r['created_at']}`
  463. **Homepage:** {r['homepage']}
  464. **Description:** __{r['description']}__"""
  465. answers.append(
  466. InlineQueryResultArticle(
  467. title="Found Repo",
  468. description=text,
  469. reply_markup=buttons,
  470. input_message_content=InputTextMessageContent(
  471. caption,
  472. disable_web_page_preview=True
  473. )
  474. ))
  475. return answers
  476. async def tg_search_func(answers, text, user_id):
  477. if user_id not in SUDOERS:
  478. msg = "**ERROR**\n__THIS FEATURE IS ONLY FOR SUDO USERS__"
  479. answers.append(
  480. InlineQueryResultArticle(
  481. title="ERROR",
  482. description="THIS FEATURE IS ONLY FOR SUDO USERS",
  483. input_message_content=InputTextMessageContent(msg)
  484. ))
  485. return answers
  486. if str(text)[-1] != ":":
  487. msg = "**ERROR**\n__Put A ':' After The Text To Search__"
  488. answers.append(
  489. InlineQueryResultArticle(
  490. title="ERROR",
  491. description="Put A ':' After The Text To Search",
  492. input_message_content=InputTextMessageContent(msg)
  493. ))
  494. return answers
  495. text = text[0:-1]
  496. async for message in app2.search_global(text, limit=49):
  497. buttons = InlineKeyboard(row_width=2)
  498. buttons.add(
  499. InlineKeyboardButton(
  500. text="Origin",
  501. url=message.link if message.link else "https://t.me/telegram"
  502. ),
  503. InlineKeyboardButton(
  504. text="Search again",
  505. switch_inline_query_current_chat="search"
  506. )
  507. )
  508. name = message.from_user.first_name if message.from_user.first_name else "NO NAME"
  509. caption = f"""
  510. **Query:** {text}
  511. **Name:** {str(name)} [`{message.from_user.id}`]
  512. **Chat:** {str(message.chat.title)} [`{message.chat.id}`]
  513. **Date:** {ctime(message.date)}
  514. **Text:** >>
  515. {message.text.markdown if message.text else message.caption if message.caption else '[NO_TEXT]'}
  516. """
  517. result = InlineQueryResultArticle(
  518. title=name,
  519. description=message.text if message.text else "[NO_TEXT]",
  520. reply_markup=buttons,
  521. input_message_content=InputTextMessageContent(
  522. caption,
  523. disable_web_page_preview=True
  524. )
  525. )
  526. answers.append(result)
  527. return answers
  528. async def music_inline_func(answers, query):
  529. chat_id = -1001445180719
  530. group_invite = "https://t.me/joinchat/vSDE2DuGK4Y4Nzll"
  531. try:
  532. messages = [m async for m in app2.search_messages(chat_id, query, filter="audio", limit=199)]
  533. except Exception as e:
  534. print(e)
  535. msg = f"You Need To Join Here With Your Bot And Userbot To Get Cached Music.\n{group_invite}"
  536. answers.append(
  537. InlineQueryResultArticle(
  538. title="ERROR",
  539. description="Click Here To Know More.",
  540. input_message_content=InputTextMessageContent(
  541. msg,
  542. disable_web_page_preview=True
  543. )
  544. )
  545. )
  546. return answers
  547. messages_ids_and_duration = []
  548. for f_ in messages:
  549. messages_ids_and_duration.append(
  550. {"message_id": f_.message_id, "duration": f_.audio.duration if f_.audio.duration else 0}
  551. )
  552. messages = list(
  553. {v["duration"]: v for v in messages_ids_and_duration}.values())
  554. messages_ids = []
  555. for ff_ in messages:
  556. messages_ids.append(ff_['message_id'])
  557. messages = await app.get_messages(chat_id, messages_ids[0:48])
  558. for message_ in messages:
  559. answers.append(
  560. InlineQueryResultCachedDocument(
  561. file_id=message_.audio.file_id,
  562. title=message_.audio.title
  563. )
  564. )
  565. return answers
  566. async def wiki_func(answers, text):
  567. data = await arq.wiki(text)
  568. msg = f"""
  569. **QUERY:**
  570. {data['title']}
  571. **ANSWER:**
  572. __{data['answer']}__"""
  573. answers.append(
  574. InlineQueryResultArticle(
  575. title=data['title'],
  576. description=data['answer'],
  577. input_message_content=InputTextMessageContent(msg)
  578. )
  579. )
  580. return answers
  581. async def speedtest_init(query):
  582. answers = []
  583. user_id = query.from_user.id
  584. if user_id not in SUDOERS:
  585. msg = "**ERROR**\n__THIS FEATURE IS ONLY FOR SUDO USERS__"
  586. answers.append(
  587. InlineQueryResultArticle(
  588. title="ERROR",
  589. description="THIS FEATURE IS ONLY FOR SUDO USERS",
  590. input_message_content=InputTextMessageContent(msg)
  591. ))
  592. return answers
  593. msg = "**Click The Button Below To Perform A Speedtest**"
  594. button = InlineKeyboard(row_width=1)
  595. button.add(InlineKeyboardButton(text="Test", callback_data="test_speedtest"))
  596. answers.append(
  597. InlineQueryResultArticle(
  598. title="Click Here",
  599. input_message_content=InputTextMessageContent(msg),
  600. reply_markup=button
  601. )
  602. )
  603. return answers
  604. """ callback query for the function above """
  605. @app.on_callback_query(filters.regex(f"test_speedtest"))
  606. async def test_speedtest_cq(_, cq):
  607. if cq.from_user.id not in SUDOERS:
  608. await cq.answer("This Isn't For You!")
  609. return
  610. inline_message_id = cq.inline_message_id
  611. await app.edit_inline_text(
  612. inline_message_id,
  613. "**Testing**"
  614. )
  615. download, upload, info = await test_speedtest()
  616. msg = f"""
  617. **Download:** `{download}`
  618. **Upload:** `{upload}`
  619. **Latency:** `{info['latency']} ms`
  620. **Country:** `{info['country']} [{info['cc']}]`
  621. **Latitude:** `{info['lat']}`
  622. **Longitude:** `{info['lon']}`
  623. """
  624. await app.edit_inline_text(
  625. inline_message_id,
  626. msg
  627. )