Skip to content

Commit 76246b0

Browse files
committed
feat: fix errors
1 parent d2b27d2 commit 76246b0

File tree

1 file changed

+49
-29
lines changed

1 file changed

+49
-29
lines changed

bot.py

+49-29
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,92 @@
11
# This file is part of the ChannelAutoForwarder distribution (https://github.com/xditya/ChannelAutoForwarder).
22
# 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
66
# 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
1111
# General Public License for more details.
12-
#
12+
#
1313
# License can be found in < https://github.com/xditya/ChannelAutoForwarder/blob/main/License> .
1414

1515
import logging
16-
import asyncio
1716
from telethon import TelegramClient, events, Button
1817
from decouple import config
1918

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+
)
2122

2223
# start the bot
23-
print("Starting...")
24+
logging.info("Starting...")
2425
try:
2526
apiid = config("APP_ID", cast=int)
2627
apihash = config("API_HASH")
2728
bottoken = config("BOT_TOKEN")
2829
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)
3132
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...")
3435
exit()
3536

3637

3738
@datgbot.on(events.NewMessage(pattern="/start"))
3839
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+
)
4048

4149

4250
@datgbot.on(events.NewMessage(pattern="/help"))
4351
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+
)
4555

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:
4960
try:
5061
if event.poll:
5162
return
5263
if event.photo:
5364
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+
)
5568
elif event.media:
5669
try:
5770
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:
6075
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+
)
6279
finally:
6380
return
6481
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+
)
6888

6989

70-
print("Bot has started.")
71-
print("Do visit @its_xditya..")
90+
logging.info("Bot has started.")
91+
logging.info("Do visit @its_xditya..")
7292
datgbot.run_until_disconnected()

0 commit comments

Comments
 (0)