-
-
Notifications
You must be signed in to change notification settings - Fork 364
/
bot.py
94 lines (83 loc) · 3.75 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
93
94
# 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(
level=logging.INFO, format="[%(levelname)s] %(asctime)s - %(message)s"
)
log = logging.getLogger("ChannelAutoPost")
# start the bot
log.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(None, apiid, apihash).start(bot_token=bottoken)
except Exception as exc:
log.error("Environment vars are missing! Kindly recheck.")
log.info("Bot is quiting...")
log.error(exc)
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/xditya/ChannelAutoForwarder"),
Button.url("Dev", url="https://xditya.me"),
],
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/xditya/ChannelAutoForwarder).\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 @xditya_Bot :)"
)
@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:
log.error(
"TO_CHANNEL ID is wrong or I can't send messages there (make me admin).\nTraceback:\n%s",
exc,
)
log.info("Bot has started.")
log.info("Do visit https://xditya.me !")
datgbot.run_until_disconnected()