Skip to content
This repository was archived by the owner on May 3, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM debian:latest

RUN apt update && apt upgrade -y
RUN apt install git python3-pip ffmpeg -y

RUN mkdir /app
WORKDIR /app
COPY . /app

RUN pip3 install --upgrade pip
RUN pip3 install -U -r requirements.txt

CMD python3 bot.py
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
All Enviroment Variables can be found in [`env.sample`](https://github.com/kaif-00z/TgRedditBot/blob/main/env.sample) !

## Deploy to Heroku
[![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://dashboard.heroku.com/new?button-url=https%3A%2F%2Fgithub.com%2Fkaif-00z%2FTgRedditBot&template=https%3A%2F%2Fgithub.com%2Fkaif-00z%2FCTgRedditBot)
[![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://dashboard.heroku.com/new?button-url=https%3A%2F%2Fgithub.com%2Fkaif-00z%2FTgRedditBot&template=https%3A%2F%2Fgithub.com%2Fkaif-00z%2FTgRedditBot)

## Deploy to Railway
[![Deploy on Railway](https://railway.app/button.svg)](https://railway.app/new/template/boH0zQ?referralCode=RZv4LZ)

## Deploying Locally
Fill Your Env Variables in .env file !
Expand Down
14 changes: 7 additions & 7 deletions app.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "RedditTgBot",
"name": "TgRedditBot",
"description": "A Normal Telegram Bot Which can control reddit from telegram",
"logo": "https://telegra.ph/file/2a37745048a2d07323e05.jpg",
"keywords": ["Telegram","Python", "Reddit","Stream","Interaction"],
"repository": "https://github.com/kaif-00z/RedditTgBot",
"repository": "https://github.com/kaif-00z/TgRedditBot",
"success_url": "https://t.me/BotsBakery",
"env": {
"API_ID": {
Expand All @@ -24,19 +24,19 @@
},
"REDDIT_CLIENT_ID": {
"description": "Put your reddit client id.",
"value": "",
"value": ""
},
"REDDIT_CLIENT_SECRET": {
"description": "Put your reddit client secret.",
"value": "",
"value": ""
},
"REDDIT_USERNAME": {
"description": "Put your reddit username.",
"value": "",
"value": ""
},
"REDDIT_PASSWORD": {
"description": "Put your reedit password.",
"value": "",
"value": ""
},
"REDIS_URI": {
"description": "Redis endpoint URL, from redislabs.com",
Expand All @@ -48,7 +48,7 @@
}
},
"formation": {
"bot": {
"worker": {
"quantity": 1,
"size": "free"
}
Expand Down
184 changes: 146 additions & 38 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,28 @@
import sys
from datetime import datetime as dt
from logging import DEBUG, INFO, FileHandler, StreamHandler, basicConfig, getLogger
from platform import python_version, release, system
from traceback import format_exc

from asyncpraw import Reddit
from asyncpraw.const import __version__ as praw_version
from redis import Redis
from telethon import Button, TelegramClient, events
from telethon import Button, TelegramClient
from telethon import __version__ as telethon_version
from telethon import events
from telethon.tl.types import DocumentAttributeVideo
from telethon.utils import get_display_name

from helper import *
from strings import *
from var import Var

file = "tgreddit_bot.log"
file = "TgRedditBot.log"
if os.path.exists(file):
os.remove(file)

basicConfig(
format="%(asctime)s || %(name)s [%(levelname)s] : %(message)s",
format="[%(levelname)s] [%(asctime)s] [%(name)s] : %(message)s",
handlers=[FileHandler(file), StreamHandler()],
level=INFO,
datefmt="%m/%d/%Y, %H:%M:%S",
Expand All @@ -58,7 +62,8 @@
bot = TelegramClient(None, Var.API_ID, Var.API_HASH).start(bot_token=Var.BOT_TOKEN)
LOGS.info("Successfully Connected with Telegram")
except Exception as e:
LOGS.critical(str(e))
LOGS.critical("Something Went Wrong While Connecting To Telegram")
LOGS.error(str(e))
exit()

try:
Expand All @@ -72,7 +77,8 @@
)
LOGS.info("Successfully Connected with Reddit")
except Exception as exc:
LOGS.critical(str(exc))
LOGS.critical("Something Went Wrong While Connecting To Reddit")
LOGS.error(str(exc))
exit()

try:
Expand All @@ -87,11 +93,13 @@
)
LOGS.info("successfully connected to Redis database")
except Exception as eo:
LOGS.critical(str(eo))
LOGS.critical("Something Went Wrong While Connecting To Redis")
LOGS.error(str(eo))
exit()

UPTIME = dt.now()
FUTURE = {}
# STREAM_INBOX = []

# ================Show Case===============================================

Expand Down Expand Up @@ -123,19 +131,14 @@ async def help(event):
async def loggs(event):
xx = await event.reply(PRO)
await event.reply(
file="tgreddit_bot.log",
file=file,
force_document=True,
thumb="thumb.jpg",
)
await xx.delete()


async def about(event, edit=False):
from platform import python_version, release, system

from asyncpraw.const import __version__ as praw_version
from telethon import __version__ as telethon_version

x = ABOUT.format(
ts(int((dt.now() - UPTIME).seconds) * 1000),
f"{python_version()}",
Expand Down Expand Up @@ -169,11 +172,10 @@ async def restart(event):
return await event.reply(AD)
xx = await event.reply(PRO)
try:
"""
await bash("git pull")
if os.path.exists(".git"):
await bash("git pull")
if os.path.exists("requirements.txt"):
await bash("pip3 install -U -r requirements.txt")
"""
await xx.edit("`Restarting...`")
dB.set("RESTART", str([xx.chat_id, xx.id]))
os.execl(sys.executable, sys.executable, "bot.py")
Expand All @@ -194,10 +196,19 @@ async def on_start():
"wget https://telegra.ph/file/2a37745048a2d07323e05.jpg -O thumb.jpg"
)
xxx = eval(dB.get("WATCH_LIST") or "{}")
for username in xxx.keys():
future = asyncio.ensure_future(watch(username, xxx[username]))
if username not in FUTURE:
FUTURE.update({username: future})
if xxx:
for username in xxx.keys():
vlu = username.split("|")
future = asyncio.ensure_future(watch(vlu[0], vlu[1]))
if username not in FUTURE:
FUTURE.update({username: future})
LOGS.info("Subreddit(s) Streaming Started...")
"""
if dB.get("INBOX_STREAM"):
_future = asyncio.ensure_future(inbox_stream())
STREAM_INBOX.append(_future)
LOGS.info("Inbox Streaming Started...")
"""
except Exception:
await bash(
"wget https://telegra.ph/file/2a37745048a2d07323e05.jpg -O thumb.jpg"
Expand Down Expand Up @@ -441,6 +452,7 @@ async def front_feed(event):
link_preview=False,
parse_mode="HTML",
)
await asyncio.sleep(1)
await xx.delete()
except Exception as error:
await xx.edit(f"`Error` - {error}")
Expand Down Expand Up @@ -506,13 +518,13 @@ async def watcher(event):
return await xx.edit("`UserName Not Found`")
try:
w_list = eval(dB.get("WATCH_LIST") or "{}")
future = asyncio.ensure_future(watch(username, event.sender_id))
if username not in FUTURE:
FUTURE.update({username: future})
w_list.update({username: event.sender_id})
future = asyncio.ensure_future(watch(username, event.chat_id))
if f"{username}|{event.chat_id}" not in FUTURE:
FUTURE.update({f"{username}|{event.chat_id}": future})
w_list.update({f"{username}|{event.chat_id}": event.chat_id})
dB.set("WATCH_LIST", str(w_list))
return await xx.edit(
f"`Successfully Added This on Watching List, To Unwatch this do` `/unwatch {username}`"
f"`Successfully Added This on This Chat's Watching List, To Unwatch this do` `/unwatch {username}`"
)
await xx.edit("`Already on Watching List`")
except Exception as error:
Expand All @@ -529,15 +541,18 @@ async def unwatcher(event):
if not username:
return await xx.edit("`Username Not Given`")
try:
if FUTURE.get(username):
_username = f"{username}|{event.chat_id}"
if FUTURE.get(_username):
w_list = eval(dB.get("WATCH_LIST") or "{}")
FUTURE[username].cancel()
del FUTURE[username]
if w_list.get(username):
del w_list[username]
FUTURE[_username].cancel()
del FUTURE[_username]
if w_list.get(_username):
del w_list[_username]
dB.set("WATCH_LIST", str(w_list))
return await xx.edit("`Succesfully Removed it from Watching List`")
await xx.edit("`Username Not Found In Watching List`")
return await xx.edit(
"`Succesfully Removed it from This Chat's Watching List`"
)
await xx.edit("`Username Not Found In This Chat's Watching List`")
except Exception as error:
await xx.edit(f"`Error` - {error}")
LOGS.error(format_exc())
Expand All @@ -547,14 +562,41 @@ async def unwatcher(event):
async def watch_list(event):
if str(event.sender_id) not in Var.OWNER:
return await event.reply(AD)
xx = await event.reply(PRO)
await event.reply(
"`Choose...`",
buttons=[
[
Button.inline("All Chats", data="alchwlst"),
Button.inline("This Chat Only", data="tschwlst"),
]
],
),


@bot.on(events.callbackquery.CallbackQuery(data=re.compile("alchwlst")))
async def lst_watch(event):
try:
txt = "**Watch List**\n\n"
txt = "**Watch List Of All Chats**\n\n"
for u in FUTURE.keys():
txt += f"[{u}](https://reddit.com/r/{u})\n"
await xx.edit(txt)
vlu = u.split("|")
txt += f"[{vlu[0]}](https://reddit.com/r/{vlu[0]}) - `[{vlu[1]}]`\n"
await event.edit(txt, link_preview=False)
except Exception as error:
await xx.edit(f"`Error` - {error}")
await event.edit(f"`Error` - {error}")
LOGS.error(format_exc())


@bot.on(events.callbackquery.CallbackQuery(data=re.compile("tschwlst")))
async def lst_ch_watch(event):
try:
txt = "**Watch List Of This Chat**\n\n"
for u in FUTURE.keys():
vlu = u.split("|")
if str(event.chat_id) in vlu:
txt += f"[{vlu[0]}](https://reddit.com/r/{vlu[0]})\n"
await event.edit(txt, link_preview=False)
except Exception as error:
await event.edit(f"`Error` - {error}")
LOGS.error(format_exc())


Expand All @@ -563,7 +605,7 @@ async def incoreply(event):
if not event.reply_to:
return
if str(event.sender_id) not in Var.OWNER:
return await event.reply(AD)
return
reply = await event.get_reply_message()
msg = event.text
try:
Expand All @@ -577,7 +619,7 @@ async def incoreply(event):
xx = await event.reply(PRO)
try:
if msg.startswith((".", "/")):
return await xx.edit("`You Can't start the message with'.' and '/'`")
return await xx.edit("`You Can't start the message with '.' and '/'`")
submission = await reddit.submission(url=url)
await submission.reply(msg)
await xx.edit("`Successfully Replied`")
Expand Down Expand Up @@ -641,6 +683,72 @@ async def subinfo(event):
LOGS.error(format_exc())


""" Todo Stuffs"""

"""

@bot.on(events.NewMessage(incoming=True, pattern="^/inbox$"))
async def _inbox_opt(event):
if str(event.sender_id) not in Var.OWNER:
return await event.reply(AD)
btn = [
[
Button.inline("Enable it", data="strmin"),
Button.inline("Disable it", data="disstrmin"),
]
]
await event.reply("`Stream Inbox:`", buttons=btn)


@bot.on(events.callbackquery.CallbackQuery(data=re.compile("strmin")))
async def _stream_inbox(event):
xx = await event.edit(PRO)
if STREAM_INBOX:
return await xx.edit("`Inbox Is Already Streaming... 😑😑`")
try:
future = asyncio.ensure_future(inbox_stream())
STREAM_INBOX.append(future)
dB.set("INBOX_STREAM", "True")
await xx.edit("`Successfully Enabled Inbox Streaming...`")
LOGS.info("Successfully Enabled Inbox Streaming...")
except Exception as error:
await xx.edit(f"`Error` - {error}")
LOGS.error(format_exc())


@bot.on(events.callbackquery.CallbackQuery(data=re.compile("disstrmin")))
async def _stop_stream_inbox(event):
xx = await event.edit(PRO)
if not STREAM_INBOX:
return await xx.edit("`Inbox Streaming Already Disabled... 😑😑`")
try:
STREAM_INBOX[0].cancel()
STREAM_INBOX.clear()
dB.delete("INBOX_STREAM")
await xx.edit("`Successfully Disabled Inbox Streaming...`")
LOGS.info("Successfully Disabled Inbox Streaming...")
except Exception as error:
await xx.edit(f"`Error` - {error}")
LOGS.error(format_exc())


async def inbox_stream():
ids = [int(id) for id in Var.OWNER_ID.split()]
try:
async for pm in reddit.inbox.stream(skip_existing=True):
for id in ids:
await bot.send_message(
id,
INBOX.format(pm.subject, pm.body, pm.author, pm.was_comment),
buttons=[[Button.url("VIEW", url=f"https://redd.it/{pm.id}")]],
)
except Exception as error:
for id in ids:
await bot.send_message(id, f"`Error` - {error}")
LOGS.error(format_exc())
"""


LOGS.info("Bot has Started...")
bot.loop.run_until_complete(on_start())
bot.loop.run_forever()