forked from Movindutb/ChannelAutoPost
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
74 lines (64 loc) · 3.44 KB
/
bot.py
File metadata and controls
74 lines (64 loc) · 3.44 KB
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
# This file is part of the ChannelAutoForwarder distribution (https://github.com/xditya/ChannelAutoForwarder).
# Copyright (c) 2021 Adiya
#
# 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
import asyncio
from telethon import TelegramClient, events, Button
from decouple import config
from telethon.tl.functions.users import GetFullUserRequest
logging.basicConfig(format='[%(levelname) 5s/%(asctime)s] %(name)s: %(message)s', level=logging.INFO)
# start the bot
print("Starting...")
try:
apiid = config("APP_ID", cast=int)
apihash = config("API_HASH")
bottoken = config("BOT_TOKEN")
frm = config("FROM_CHANNEL", cast=int)
tochnl = config("TO_CHANNEL", cast=int)
datgbot = TelegramClient('bot', apiid, apihash).start(bot_token=bottoken)
except:
print("Environment vars are missing! Kindly recheck.")
print("Bot is quiting...")
exit()
@datgbot.on(events.NewMessage(pattern="/start"))
async def _(event):
ok = await datgbot(GetFullUserRequest(event.sender_id))
await event.reply(f"Hi `{ok.user.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://t.me/its_xditya")], 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):
if not event.is_private:
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)
return
except:
media = event.media.document
await datgbot.send_file(tochnl, media, caption = event.text, link_preview = False)
return
else:
await datgbot.send_message(tochnl, event.text, link_preview = False)
except:
print("TO_CHANNEL ID is wrong or I can't send messages there (make me admin).")
print("Bot has started.")
print("Do visit @its_xditya..")
datgbot.run_until_disconnected()