|
1 | 1 | # This file is part of the ChannelAutoForwarder distribution (https://github.com/xditya/ChannelAutoForwarder).
|
2 | 2 | # Copyright (c) 2021-2022 Aditya
|
3 |
| -# |
4 |
| -# This program is free software: you can redistribute it and/or modify |
5 |
| -# it under the terms of the GNU General Public License as published by |
| 3 | +# |
| 4 | +# This program is free software: you can redistribute it and/or modify |
| 5 | +# it under the terms of the GNU General Public License as published by |
6 | 6 | # the Free Software Foundation, version 3.
|
7 |
| -# |
8 |
| -# This program is distributed in the hope that it will be useful, but |
9 |
| -# WITHOUT ANY WARRANTY; without even the implied warranty of |
10 |
| -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 7 | +# |
| 8 | +# This program is distributed in the hope that it will be useful, but |
| 9 | +# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
11 | 11 | # General Public License for more details.
|
12 |
| -# |
| 12 | +# |
13 | 13 | # License can be found in < https://github.com/xditya/ChannelAutoForwarder/blob/main/License> .
|
14 | 14 |
|
15 | 15 | import logging
|
16 |
| -import asyncio |
17 | 16 | from telethon import TelegramClient, events, Button
|
18 | 17 | from decouple import config
|
19 | 18 |
|
20 |
| -logging.basicConfig(format='[%(levelname) 5s/%(asctime)s] %(name)s: %(message)s', level=logging.INFO) |
| 19 | +logging.basicConfig( |
| 20 | + format="[%(levelname) 5s/%(asctime)s] %(name)s: %(message)s", level=logging.INFO |
| 21 | +) |
21 | 22 |
|
22 | 23 | # start the bot
|
23 |
| -print("Starting...") |
| 24 | +logging.info("Starting...") |
24 | 25 | try:
|
25 | 26 | apiid = config("APP_ID", cast=int)
|
26 | 27 | apihash = config("API_HASH")
|
27 | 28 | bottoken = config("BOT_TOKEN")
|
28 | 29 | frm = config("FROM_CHANNEL", cast=lambda x: [int(_) for _ in x.split(" ")])
|
29 |
| - tochnl = config("TO_CHANNEL", cast=lambda x: [int(_) for _ in x.split(" ")]) |
30 |
| - datgbot = TelegramClient('bot', apiid, apihash).start(bot_token=bottoken) |
| 30 | + tochnls = config("TO_CHANNEL", cast=lambda x: [int(_) for _ in x.split(" ")]) |
| 31 | + datgbot = TelegramClient("bot", apiid, apihash).start(bot_token=bottoken) |
31 | 32 | except:
|
32 |
| - print("Environment vars are missing! Kindly recheck.") |
33 |
| - print("Bot is quiting...") |
| 33 | + logging.error("Environment vars are missing! Kindly recheck.") |
| 34 | + logging.info("Bot is quiting...") |
34 | 35 | exit()
|
35 | 36 |
|
36 | 37 |
|
37 | 38 | @datgbot.on(events.NewMessage(pattern="/start"))
|
38 | 39 | async def _(event):
|
39 |
| - 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://t.me/its_xditya")], link_preview=False) |
| 40 | + await event.reply( |
| 41 | + 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)..", |
| 42 | + buttons=[ |
| 43 | + Button.url("Repo", url="https://github.com/xditya/ChannelAutoForwarder"), |
| 44 | + Button.url("Dev", url="https://t.me/its_xditya"), |
| 45 | + ], |
| 46 | + link_preview=False, |
| 47 | + ) |
40 | 48 |
|
41 | 49 |
|
42 | 50 | @datgbot.on(events.NewMessage(pattern="/help"))
|
43 | 51 | async def helpp(event):
|
44 |
| - 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 :)") |
| 52 | + await event.reply( |
| 53 | + "**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 :)" |
| 54 | + ) |
45 | 55 |
|
46 |
| -@datgbot.on(events.NewMessage(incoming=True, chats=frm)) |
47 |
| -async def _(event): |
48 |
| - for tochnl in tochnl: |
| 56 | + |
| 57 | +@datgbot.on(events.NewMessage(incoming=True, chats=frm)) |
| 58 | +async def _(event): |
| 59 | + for tochnl in tochnls: |
49 | 60 | try:
|
50 | 61 | if event.poll:
|
51 | 62 | return
|
52 | 63 | if event.photo:
|
53 | 64 | photo = event.media.photo
|
54 |
| - await datgbot.send_file(tochnl, photo, caption = event.text, link_preview = False) |
| 65 | + await datgbot.send_file( |
| 66 | + tochnl, photo, caption=event.text, link_preview=False |
| 67 | + ) |
55 | 68 | elif event.media:
|
56 | 69 | try:
|
57 | 70 | if event.media.webpage:
|
58 |
| - await datgbot.send_message(tochnl, event.text, link_preview = False) |
59 |
| - except: |
| 71 | + await datgbot.send_message( |
| 72 | + tochnl, event.text, link_preview=False |
| 73 | + ) |
| 74 | + except Exception: |
60 | 75 | media = event.media.document
|
61 |
| - await datgbot.send_file(tochnl, media, caption = event.text, link_preview = False) |
| 76 | + await datgbot.send_file( |
| 77 | + tochnl, media, caption=event.text, link_preview=False |
| 78 | + ) |
62 | 79 | finally:
|
63 | 80 | return
|
64 | 81 | else:
|
65 |
| - await datgbot.send_message(tochnl, event.text, link_preview = False) |
66 |
| - except: |
67 |
| - print("TO_CHANNEL ID is wrong or I can't send messages there (make me admin).") |
| 82 | + await datgbot.send_message(tochnl, event.text, link_preview=False) |
| 83 | + except Exception as exc: |
| 84 | + logging.error( |
| 85 | + "TO_CHANNEL ID is wrong or I can't send messages there (make me admin).\nTraceback:\n%s", |
| 86 | + exc, |
| 87 | + ) |
68 | 88 |
|
69 | 89 |
|
70 |
| -print("Bot has started.") |
71 |
| -print("Do visit @its_xditya..") |
| 90 | +logging.info("Bot has started.") |
| 91 | +logging.info("Do visit @its_xditya..") |
72 | 92 | datgbot.run_until_disconnected()
|
0 commit comments