thedirector.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. '''
  2. bot for the cyberpunk network
  3. obs: still in tests but stable
  4. '''
  5. from time import sleep
  6. import telegram
  7. from telegram.error import NetworkError, Unauthorized
  8. import unicodedata
  9. from threading import Thread
  10. from praw import Reddit
  11. from twitter import Api
  12. import sqlite3
  13. from uuid import uuid4
  14. update_id = None
  15. token = ""
  16. reddit = Reddit(client_id='',
  17. client_secret='',
  18. password='',
  19. user_agent='thedirector00',
  20. username='')
  21. class main():
  22. def runBot(self, bot):
  23. self.bot = bot
  24. try:
  25. update_id = self.bot.getUpdates()[0]['update_id']
  26. except IndexError:
  27. update_id = None
  28. print("last update_id {}".format(update_id))
  29. sleep(1)
  30. while True:
  31. try:
  32. for update in self.bot.getUpdates(offset=update_id, timeout=4):
  33. print("update > {}".format(update))
  34. update_id = update.update_id + 1
  35. try:
  36. thBot = Thread(target=cyBot,args=[bot,update])
  37. thBot.start()
  38. #cyBot(bot, update)
  39. except:
  40. pass
  41. sleep(0.4)
  42. except NetworkError:
  43. sleep(1)
  44. except Unauthorized:
  45. # The user has removed or blocked the bot.
  46. update_id += 1
  47. class socialNet():
  48. global reddit
  49. def twPost(self, content):
  50. api = Api(consumer_key='',
  51. consumer_secret='',
  52. access_token_key='',
  53. access_token_secret='')
  54. hashtag = ''
  55. if not len(content["hashtags"]):
  56. return False
  57. for ht in content["hashtags"]:
  58. hashtag += ht + " "
  59. tweet = "{} {} {}".format(hashtag, content["title"], content["url"])
  60. api.PostUpdate(tweet)
  61. #print(tweet)
  62. def rdSubmit(self, post):
  63. # this is being tested so reddit api doesnt block the bot thinking it is spam
  64. originalrd = reddit.subreddit("cyberpub").submit(post["title"], url=post["url"])
  65. #print("\n\t{}".format(originalrd["id"]))
  66. #crs = reddit.submission(id=originalrd["id"])
  67. #crs.crosspost(subreddit="cryptobrasil", send_replies=False)
  68. #sleep(6)
  69. #reddit.subreddit("cryptobrasil").submit(post["title"], url=post["url"])
  70. class misc():
  71. def isAdm(user_id, admin_list):
  72. for adm in admin_list:
  73. admid = adm.user.id
  74. if admid == user_id:
  75. return True
  76. return False
  77. def isArabic(first_name):
  78. for letter in first_name:
  79. try:
  80. encoding = unicodedata.name(letter).lower()
  81. if 'arabic' in encoding or 'persian' in encoding:
  82. return True
  83. except ValueError:
  84. pass
  85. return False
  86. def parserEntities(entities, post):
  87. titlechk = False
  88. urlchk = False
  89. title = ''
  90. url = ''
  91. hashtags = []
  92. for entity in entities:
  93. if entity.type == "hashtag":
  94. offset = entity.offset
  95. length = offset + entity.length
  96. hashtags.append(post[offset:length])
  97. elif entity.type == "bold" and not titlechk:
  98. titlechk = True
  99. offset = entity.offset
  100. length = offset + entity.length
  101. title = post[offset:length]
  102. elif entity.type == "url" and not urlchk:
  103. urlchk = True
  104. offset = entity.offset
  105. length = offset + entity.length
  106. url = post[offset:length]
  107. if not titlechk or not urlchk:
  108. return False
  109. response = {"hashtags": hashtags, "title": title, "url": url}
  110. return response
  111. class cyBot():
  112. allowed_chats = [-1001218498698, -1001171687704, -1001252516868, -395257705]
  113. allowed_channels = [-1001183412743, -1001308153582, -1001428687999, -1001410198217, -1001446957910, -1001257020477, -1001183412743]
  114. mimetypes = ["application/pdf", "application/epub+zip", "application/x-mobipocket-ebook", "application/msword", "application/x-bittorrent", "text/plain", "application/x-rar-compressed", "application/zip", "application/x-7z-compressed", "audio/mpeg", "audio/mp3", "application/octet-stream"]
  115. misc = misc()
  116. log_channel = "-1001183412743"
  117. file_channel = "@cyfiles"
  118. replytomessage = False
  119. def __init__(self, bot, update):
  120. if update and update.message or update.channel_post or update.callback_query or update.inline_query:
  121. self.bot = bot
  122. self.update = update
  123. conn = sqlite3.connect('cyfiles.db')
  124. db = conn.cursor()
  125. try:
  126. self.chat_id = self.update.message.chat.id
  127. self.message_id = self.update.message.message_id
  128. except:
  129. pass
  130. else:
  131. return
  132. if self.update.message:
  133. if self.update.message.reply_to_message:
  134. self.replytomessage = True
  135. self.target_user_id = self.update.message.reply_to_message.from_user.id
  136. self.target_message_id = self.update.message.reply_to_message.message_id
  137. self.target_fname = self.update.message.reply_to_message.from_user.first_name
  138. self.target_username = update.message.reply_to_message.from_user.username
  139. if self.update.message.document:
  140. file_id = self.update.message.document.file_id
  141. file_name = self.update.message.document.file_name
  142. file_size = self.update.message.document.file_size
  143. file_mimetype = self.update.message.document.mime_type
  144. file_by = update.message.from_user.id
  145. # make backup of file
  146. if file_mimetype in self.mimetypes:
  147. file_data = "`{id}` -- *{name}*\n_{size}_ | {mimetype}\n\nby: `#id{uid}`".format(id=file_id,name=file_name,size=file_size,mimetype=file_mimetype,uid=file_by)
  148. try:
  149. db.execute('insert into files values ("", ?, ?, ?, ?, ?)', (file_id, file_name, file_size, file_mimetype, file_by))
  150. conn.commit()
  151. self.bot.sendDocument(chat_id=self.file_channel, caption=file_data, parse_mode="Markdown", document=file_id, disable_notification=1)
  152. except sqlite3.IntegrityError:
  153. conn.close()
  154. return
  155. if self.update.message.new_chat_members:
  156. for member in self.update.message.new_chat_members:
  157. if misc.isArabic(member.first_name):
  158. self.bot.kickChatMember(chat_id=self.chat_id, user_id=member.id)
  159. self.bot.sendMessage(chat_id=self.chat_id, reply_to_message_id=self.message_id, parse_mode="HTML", text="<b>[ban]</b> <i>allahu akabur!1!</i>")
  160. return
  161. fname = member.first_name.replace('_', '\_')
  162. log_nmember = "{fname}\n`#id{id}`\n\nIn: {ctitle}\n> [{fname}](tg://user?id={id})".format(fname=fname,id=member.id,ctitle=update.message.chat.id)
  163. self.bot.restrictChatMember(chat_id=self.chat_id, user_id=member.id, until_date=None, can_send_messages=0, can_send_media_messages=0, can_send_other_messages=0, can_add_web_page_previews=0)
  164. self.bot.sendMessage(chat_id=self.log_channel, parse_mode="Markdown", text=log_nmember)
  165. captcha = [
  166. [
  167. telegram.InlineKeyboardButton("ClickMe",callback_data=member.id)
  168. ]
  169. ]
  170. reply_markup = telegram.InlineKeyboardMarkup(captcha)
  171. self.bot.sendMessage(chat_id=self.chat_id, reply_to_message_id=self.message_id, parse_mode="Markdown", text="**welcome!\n[simple user verification]**", reply_markup=reply_markup)
  172. if self.update.callback_query:
  173. tgcll = Thread(self.tgCallback())
  174. tgcll.start()
  175. #self.tgCallback()
  176. elif self.update.message and self.update.message.chat.id in self.allowed_chats:
  177. tgmsg = Thread(self.tgMessage())
  178. tgmsg.start()
  179. #self.tgMessage()
  180. elif self.update.channel_post and self.update.channel_post.chat.id in self.allowed_channels:
  181. tgchn = Thread(self.tgChannel())
  182. tgchn.start()
  183. #self.tgChannel()
  184. elif self.update.inline_query:
  185. tginl = Thread(self.tgInline())
  186. tginl.start()
  187. #self.tgInline()
  188. def tgMessage(self):
  189. if not self.update.message.text:
  190. return
  191. backup_channel = "@pr1v8_board"
  192. admin_list = self.bot.getChatAdministrators(chat_id=self.chat_id)
  193. user_id = self.update.message.from_user.id
  194. user_fname = self.update.message.from_user.first_name
  195. sudo = misc.isAdm(user_id, admin_list)
  196. txt = self.update.message.text.split(" ")
  197. command = txt[0]
  198. remain_txt = ' '.join(txt[1:])
  199. query = True
  200. if not remain_txt:
  201. remain_txt = "-"
  202. query = False
  203. if command == "/getme":
  204. me = self.bot.getMe()
  205. self.bot.sendMessage(chat_id=self.chat_id, reply_to_message_id=self.message_id, text="@" + me.username)
  206. elif command == "/sudo" and sudo:
  207. self.bot.sendMessage(chat_id=self.chat_id, reply_to_message_id=self.message_id, text="hello root")
  208. elif command == "/free" and self.replytomessage and sudo:
  209. self.bot.restrictChatMember(chat_id=self.chat_id, user_id=self.target_user_id, until_date=None, can_send_messages=1, can_send_media_messages=1, can_send_other_messages=1, can_add_web_page_previews=1)
  210. self.bot.sendMessage(chat_id=self.chat_id, reply_to_message_id=self.message_id, parse_mode="Markdown", text="user liberado")
  211. elif command == "/link":
  212. link = self.bot.exportChatInviteLink(chat_id=self.chat_id)
  213. self.bot.sendMessage(chat_id=self.chat_id, reply_to_message_id=self.message_id, text=link)
  214. elif command == "/afk" or command == "/off":
  215. afk = "*user* [{fname}](tg://user?id={id}) *is afk*\n *> {reason}*".format(fname=user_fname,id=user_id,reason=remain_txt)
  216. self.bot.sendMessage(chat_id=self.chat_id, reply_to_message_id=self.message_id, parse_mode="Markdown", text=afk)
  217. elif command == "/back" or command == "/on":
  218. back = "*user* [{fname}](tg://user?id={id}) *is back*".format(fname=user_fname,id=user_id)
  219. self.bot.sendMessage(chat_id=self.chat_id, reply_to_message_id=self.message_id, parse_mode="Markdown", text=back)
  220. elif command == "/ban" and self.replytomessage and sudo:
  221. ban = "*user* [{fname}](tg://user?id={id}) *banned*".format(fname=self.target_fname,id=self.target_user_id)
  222. self.bot.kickChatMember(chat_id=self.chat_id, user_id=self.target_user_id)
  223. self.bot.sendMessage(chat_id=self.chat_id, reply_to_message_id=self.message_id, parse_mode="Markdown", text=ban)
  224. elif command == "/unban" and self.replytomessage and sudo:
  225. unban = "*user* [{fname}](tg://user?id={id}) *unbanned*".format(fname=self.target_fname,id=self.target_user_id)
  226. self.bot.unbanChatMember(chat_id=self.chat_id, user_id=self.target_user_id)
  227. self.bot.sendMessage(chat_id=self.chat_id, reply_to_message_id=self.message_id, parse_mode="Markdown", text=unban)
  228. elif command == "/mute" and self.replytomessage and sudo:
  229. mute = "*user* [{fname}](tg://user?id={id}) *muted*".format(fname=self.target_fname,id=self.target_user_id)
  230. self.bot.restrictChatMember(chat_id=self.chat_id, user_id=self.target_user_id, until_date=None, can_send_messages=0, can_send_media_messages=0, can_send_other_messages=0, can_add_web_page_previews=0)
  231. self.bot.sendMessage(chat_id=self.chat_id, reply_to_message_id=self.message_id, parse_mode="Markdown", text=mute)
  232. elif command == "/unmute" and self.replytomessage and sudo:
  233. unmute = "*user* [{fname}](tg://user?id={id}) *unmuted*".format(fname=self.target_fname,id=self.target_user_id)
  234. self.bot.restrictChatMember(chat_id=self.chat_id, user_id=self.target_user_id, until_date=None, can_send_messages=1, can_send_media_messages=1, can_send_other_messages=1, can_add_web_page_previews=1)
  235. self.bot.sendMessage(chat_id=self.chat_id, reply_to_message_id=self.message_id, parse_mode="Markdown", text=unmute)
  236. elif command == "/pin" and self.replytomessage and sudo:
  237. self.bot.pinChatMessage(chat_id=self.chat_id, message_id=self.target_message_id,disable_notification=1)
  238. self.bot.sendMessage(chat_id=self.chat_id, reply_to_message_id=self.message_id, parse_mode="Markdown", text="*message pinned*")
  239. elif command == "/uinfo" and self.replytomessage and sudo:
  240. uinfo = "*user info* | [{fname}](tg://user?id={id})\nname: {fname}\nid: `#id{id}`\nusername: @{username}".format(fname=self.target_fname,id=self.target_user_id,username=self.target_username)
  241. self.bot.sendMessage(chat_id=self.chat_id, reply_to_message_id=self.message_id, parse_mode="Markdown", text=uinfo)
  242. elif command == "/ginfo":
  243. gmember = self.bot.getChatMembersCount(chat_id=self.chat_id)
  244. ginfo = "*group info* | [{title}](tg://chat?id={id})\nTitle: {title}\nid: `{id}`\nMembers: {count}".format(title=self.update.message.chat.title,id=self.chat_id,count=gmember)
  245. self.bot.sendMessage(chat_id=self.chat_id, reply_to_message_id=self.message_id, parse_mode="Markdown", text=ginfo)
  246. elif command == "/sendfile" and sudo:
  247. if len(txt) != 2:
  248. return
  249. self.bot.sendDocument(chat_id=self.chat_id,reply_to_message_id=self.message_id,document=txt[1])
  250. elif command == "/save" and self.replytomessage and sudo:
  251. self.bot.forwardMessage(chat_id=self.backup_channel, from_chat_id=self.chat_id, message_id=self.target_message_id)
  252. elif command == "/cynet":
  253. links_list = [
  254. [
  255. telegram.InlineKeyboardButton("cyPunkrs",url="https://t.me/joinchat/DE9ViFBkvVx65PpKfvtuZg"),
  256. telegram.InlineKeyboardButton("R3neg4des",url="https://t.me/joinchat/DE9ViEUaKNz34rzPzqcjSA")
  257. ],
  258. [
  259. telegram.InlineKeyboardButton("Parisburn", url="https://t.me/parisburns"),
  260. telegram.InlineKeyboardButton("warezme",url="https://t.me/warezme")
  261. ],
  262. [
  263. telegram.InlineKeyboardButton("r/cyberpub", url="https://www.reddit.com/r/cyberpub/"),
  264. telegram.InlineKeyboardButton("Twitter", url="https://twitter.com/0xpr1v8")
  265. ],
  266. [
  267. telegram.InlineKeyboardButton("cyGithub", url="https://github.com/cyberpunkrs/")
  268. ]
  269. ]
  270. reply_markup = telegram.InlineKeyboardMarkup(links_list)
  271. self.bot.sendMessage(chat_id=self.chat_id, reply_to_message_id=self.message_id, text="**cyNetwork**", parse_mode="Markdown", reply_markup=reply_markup)
  272. elif command == "@adm":
  273. adms = ""
  274. for adm in admin_list:
  275. username = adm.user.username
  276. if not username:
  277. username = "null"
  278. else:
  279. username = "- @{}".format(username)
  280. adms += "<a href=\"tg://user?id={id}\">{fname}</a> {username}\n".format(fname=adm.user.first_name,id=adm.user.id,username=username)
  281. self.bot.sendMessage(chat_id=self.chat_id, reply_to_message_id=self.message_id, parse_mode="HTML", text=adms)
  282. elif command == "/rtfm":
  283. self.bot.sendMessage(chat_id=self.chat_id, reply_to_message_id=self.message_id, parse_mode="Markdown", text="**read the fucking manual**\nhttps://en.wikipedia.org/wiki/RTFM")
  284. elif command == "/w":
  285. captcha = [
  286. [
  287. telegram.InlineKeyboardButton("me",callback_data='w')
  288. ]
  289. ]
  290. reply_markup = telegram.InlineKeyboardMarkup(captcha)
  291. self.bot.sendMessage(chat_id=self.chat_id, reply_to_message_id=self.message_id, text="**Quem ta on fdps**", parse_mode="Markdown", reply_markup=reply_markup)
  292. def tgCallback(self):
  293. cid = self.update.callback_query.message.chat.id
  294. uid = str(self.update.callback_query.from_user.id)
  295. if self.update.callback_query.data == uid:
  296. self.bot.restrictChatMember(chat_id=cid, user_id=self.update.callback_query.from_user.id, until_date=None, can_send_messages=1, can_send_media_messages=1, can_send_other_messages=1, can_add_web_page_previews=1)
  297. self.bot.deleteMessage(chat_id=cid, message_id=self.update.callback_query.message.message_id)
  298. self.bot.sendMessage(chat_id=cid, reply_to_message_id=self.update.callback_query.message.reply_to_message.message_id, parse_mode="HTML", text="user <b>{}</b> liberado".format(self.update.callback_query.from_user.first_name))
  299. elif self.update.callback_query.data == 'w':
  300. self.bot.sendMessage(chat_id=cid, reply_to_message_id=self.update.callback_query.message.reply_to_message.message_id, parse_mode="Markdown", text=" {} present@".format(self.update.callback_query.from_user.first_name))
  301. def tgChannel(self):
  302. #print("run channel\n\n")
  303. snet = socialNet()
  304. conn = sqlite3.connect('cyfiles.db')
  305. db = conn.cursor()
  306. if self.update.channel_post.document:
  307. file_id = self.update.channel_post.document.file_id
  308. file_name = self.update.channel_post.document.file_name
  309. file_size = self.update.channel_post.document.file_size
  310. file_mimetype = self.update.channel_post.document.mime_type
  311. file_by = self.update.channel_post.chat.id
  312. # make backup of file
  313. if file_mimetype in self.mimetypes:
  314. #file_data = "`{id}` -- *{name}*\n_{size}_ | {mimetype}\n\nby: `#id{uid}`".format(id=file_id,name=file_name,size=file_size,mimetype=file_mimetype,uid=file_by)
  315. try:
  316. db.execute('insert into files values ("", ?, ?, ?, ?, ?)', (file_id, file_name, file_size, file_mimetype, file_by))
  317. conn.commit()
  318. except sqlite3.IntegrityError:
  319. conn.close()
  320. return
  321. if self.update.channel_post.entities:
  322. self.post = misc.parserEntities(self.update.channel_post.entities, self.update.channel_post.text)
  323. if not self.post:
  324. return
  325. snet.rdSubmit(self.post)
  326. snet.twPost(self.post)
  327. def tgInline(self):
  328. qid = self.update.inline_query.id
  329. if self.update.inline_query.query:
  330. value = self.update.inline_query.query
  331. textquery = [
  332. telegram.InlineQueryResultArticle(
  333. id=4,
  334. title="text",
  335. input_message_content=telegram.InputTextMessageContent(
  336. message_text=value,
  337. parse_mode=telegram.ParseMode.MARKDOWN
  338. )
  339. )
  340. ]
  341. self.bot.answerInlineQuery(inline_query_id=qid, results=textquery)
  342. return
  343. inlinequery = [
  344. telegram.InlineQueryResultArticle(
  345. id=uuid4(),
  346. title="cyberpunkrs",
  347. input_message_content=telegram.InputTextMessageContent(
  348. message_text="@cyberpunkrs",
  349. parse_mode="Markdown"
  350. ),
  351. url="https://t.me/cyberpunkrs"
  352. ),
  353. telegram.InlineQueryResultArticle(
  354. id=uuid4(),
  355. title="cyberpub",
  356. input_message_content=telegram.InputTextMessageContent(
  357. message_text="[cyberpub](https://reddit.com/r/cyberpub)",
  358. parse_mode="Markdown"
  359. ),
  360. )
  361. ]
  362. self.bot.answerInlineQuery(inline_query_id=qid, results=inlinequery)
  363. if __name__ == '__main__':
  364. bot = main()
  365. btobj = telegram.Bot(token)
  366. bot.runBot(btobj)