forked from saikatwtf/ChannelAutoPost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
92 lines (81 loc) · 3.72 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# This file is part of the ChannelAutoForwarder distribution (https://github.com/xditya/ChannelAutoForwarder).
# Copyright (c) 2021-2022 Aditya
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# License can be found in < https://github.com/xditya/ChannelAutoForwarder/blob/main/License> .
import logging
from telethon import TelegramClient, events, Button
from decouple import config
logging.basicConfig(
format="[%(levelname) 5s/%(asctime)s] %(name)s: %(message)s", level=logging.INFO
)
# start the bot
logging.info("Starting...")
try:
apiid = config("APP_ID", cast=int)
apihash = config("API_HASH")
bottoken = config("BOT_TOKEN")
frm = config("FROM_CHANNEL", cast=lambda x: [int(_) for _ in x.split(" ")])
tochnls = config("TO_CHANNEL", cast=lambda x: [int(_) for _ in x.split(" ")])
datgbot = TelegramClient("bot", apiid, apihash).start(bot_token=bottoken)
except:
logging.error("Environment vars are missing! Kindly recheck.")
logging.info("Bot is quiting...")
exit()
@datgbot.on(events.NewMessage(pattern="/start"))
async def _(event):
await event.reply(
f"Hi `{event.sender.first_name}`!\n\nI am a channel auto-post bot!! Read /help to know more!\n\nI can be used in only two channels (one user) at a time. Kindly deploy your own bot.\n\n[More bots](https://t.me/its_xditya)..",
buttons=[
Button.url("Repo", url="https://github.com/saikatwtf/ChannelAutoPost"),
Button.url("Dev", url="https://telegram.me/AnnihilusOP_bot"),
],
link_preview=False,
)
@datgbot.on(events.NewMessage(pattern="/help"))
async def helpp(event):
await event.reply(
"**Help**\n\nThis bot will send all new posts in one channel to the other channel. (without forwarded tag)!\nIt can be used only in two channels at a time, so kindly deploy your own bot from [here](https://github.com/saikatwtf/ChannelAutoPost).\n\nAdd me to both the channels and make me an admin in both, and all new messages would be autoposted on the linked channel!!\n\nLiked the bot? Drop a ♥ to @AnnihilusOP :)"
)
@datgbot.on(events.NewMessage(incoming=True, chats=frm))
async def _(event):
for tochnl in tochnls:
try:
if event.poll:
return
if event.photo:
photo = event.media.photo
await datgbot.send_file(
tochnl, photo, caption=event.text, link_preview=False
)
elif event.media:
try:
if event.media.webpage:
await datgbot.send_message(
tochnl, event.text, link_preview=False
)
except Exception:
media = event.media.document
await datgbot.send_file(
tochnl, media, caption=event.text, link_preview=False
)
finally:
return
else:
await datgbot.send_message(tochnl, event.text, link_preview=False)
except Exception as exc:
logging.error(
"TO_CHANNEL ID is wrong or I can't send messages there (make me admin).\nTraceback:\n%s",
exc,
)
logging.info("Bot has started.")
logging.info("Do visit @its_xditya..")
datgbot.run_until_disconnected()