度百 发表于 5 天前

....求屌大mjj帮忙写个tg机器人✅✅

<i class="pstatus"> 本帖最后由 度百 于 2024-11-18 18:20 编辑 </i><br />
<br />
我有个telegram机器人需求,就是实时转发某个频道消息到指定群组,转发内容需要支持频道的图片、视频、文本、文件等(平时多是文本),需要完全都转发过去。频道和群组都能设置机器人为管理员权限。<br />
<br />
有空的mjj老哥求帮助,费用不要太高啊<img src="https://hostloc.com/static/image/smiley/default/lol.gif" smilieid="12" border="0" alt="" />

落叶 发表于 5 天前

前两天刚用ai写了一个<img src="https://hostloc.com/static/image/smiley/default/titter.gif" smilieid="9" border="0" alt="" />

度百 发表于 5 天前

<div class="quote"><blockquote><font size="2"><a href="https://hostloc.com/forum.php?mod=redirect&goto=findpost&pid=16041092&ptid=1365034" target="_blank"><font color="#999999">落叶 发表于 2024-11-18 18:22</font></a></font><br />
前两天刚用ai写了一个</blockquote></div><br />
<img src="https://hostloc.com/static/image/smiley/yct/022.gif" smilieid="42" border="0" alt="" />

洪河的红水 发表于 5 天前

不是直接在群组关联频道之后,在频道发的信息会自动转发到群里吗

落叶 发表于 5 天前

<div class="quote"><blockquote><font size="2"><a href="https://hostloc.com/forum.php?mod=redirect&goto=findpost&pid=16041094&ptid=1365034" target="_blank"><font color="#999999">度百 发表于 2024-11-18 18:23</font></a></font></blockquote></div><br />
@sfdatou 来TG联系我 给你挂上测试 满意的话 再说

落叶 发表于 5 天前

<div class="quote"><blockquote><font size="2"><a href="https://hostloc.com/forum.php?mod=redirect&goto=findpost&pid=16041098&ptid=1365034" target="_blank"><font color="#999999">洪河的红水 发表于 2024-11-18 18:26</font></a></font><br />
不是直接在群组关联频道之后,在频道发的信息会自动转发到群里吗</blockquote></div><br />
关联需要权限 他应该是想要转发其他人的消息 然后到自己群组 我就是这个需求 自己写了个 现在稳定运行着

deyu 发表于 5 天前

<div class="quote"><blockquote><font size="2"><a href="https://hostloc.com/forum.php?mod=redirect&goto=findpost&pid=16041103&ptid=1365034" target="_blank"><font color="#999999">落叶 发表于 2024-11-18 18:27</font></a></font><br />
关联需要权限 他应该是想要转发其他人的消息 然后到自己群组 我就是这个需求 自己写了个 现在稳定运行着 ...</blockquote></div><br />
啊 我也想搞一个, 可惜不会写

落叶 发表于 5 天前

<div class="quote"><blockquote><font size="2"><a href="https://hostloc.com/forum.php?mod=redirect&goto=findpost&pid=16041184&ptid=1365034" target="_blank"><font color="#999999">deyu 发表于 2024-11-18 18:57</font></a></font><br />
啊 我也想搞一个, 可惜不会写</blockquote></div><br />
如果都是自己群的话 建个机器人都行 不是自己群的话 想要转发别的群 要不然自己手动 要不然申请API自己写代码

z先生 发表于 5 天前

既然两边都能设置成管理员,那就都是自己的,把群组和频道关联一起不就行了

RPRX耳阝鱼禾 发表于 5 天前

我可以借,有需要可以私信我<i class="pstatus"> 本帖最后由 耳阝鱼禾 于 2024-11-18 22:18 编辑 </i><br />
<div class="blockcode"><div id="code_Mnz"><ol><li><br /><li>from telegram import Update<br /><li>from telegram.ext import Updater, MessageHandler, Filters, CallbackContext<br /><li>import logging<br /><li>import pickle<br /><li>import os<br /><li>from datetime import datetime, timedelta<br /><li>import pytz<br /><li>logging.basicConfig(level=logging.INFO)<br /><li><br /><li>BOT_TOKEN = 'BOT KEY'&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;#你的电报机器人KEY<br /><li>CHANNEL_ID = '频道ID'&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; #你要监听的频道ID<br /><li>GROUP_ID = '群组ID'&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;#将频道内容转发到这个群组的&nbsp;&nbsp;群组ID<br /><li><br /><li>PROCESSED_MESSAGES_FILE = 'processed_messages.pkl'<br /><li>def load_processed_messages():<br /><li>&nbsp; &nbsp; if os.path.exists(PROCESSED_MESSAGES_FILE):<br /><li>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;with open(PROCESSED_MESSAGES_FILE, 'rb') as f:<br /><li>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;return pickle.load(f)<br /><li>&nbsp; &nbsp; return set()<br /><li>def save_processed_message(message_id):<br /><li>&nbsp; &nbsp; processed_messages.add(message_id)<br /><li>&nbsp; &nbsp; with open(PROCESSED_MESSAGES_FILE, 'wb') as f:<br /><li>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;pickle.dump(processed_messages, f)<br /><li>&nbsp; &nbsp; os.chmod(PROCESSED_MESSAGES_FILE, 0o777)<br /><li>def reset_processed_messages():<br /><li>&nbsp; &nbsp; global processed_messages<br /><li>&nbsp; &nbsp; processed_messages = set()<br /><li>&nbsp; &nbsp; save_processed_message(None)<br /><li>def is_today_message(message_date):<br /><li>&nbsp; &nbsp; beijing_tz = pytz.timezone('Asia/Shanghai')<br /><li>&nbsp; &nbsp; now_beijing = datetime.now(beijing_tz)<br /><li>&nbsp; &nbsp; message_date_beijing = message_date.astimezone(beijing_tz)<br /><li>&nbsp; &nbsp; return message_date_beijing.date() == now_beijing.date()<br /><li>processed_messages = load_processed_messages()<br /><li>def forward_message(update: Update, context: CallbackContext):<br /><li>&nbsp; &nbsp; if update.channel_post and update.channel_post.chat.id == int(CHANNEL_ID):<br /><li>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;message_id = update.channel_post.message_id<br /><li>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;message_date = update.channel_post.date<br /><li>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;if is_today_message(message_date) and message_id not in processed_messages:<br /><li>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;context.bot.forward_message(chat_id=GROUP_ID, from_chat_id=CHANNEL_ID, message_id=message_id)<br /><li>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;save_processed_message(message_id)<br /><li>def main():<br /><li>&nbsp; &nbsp; updater = Updater(token=BOT_TOKEN, use_context=True)<br /><li>&nbsp; &nbsp; dispatcher = updater.dispatcher<br /><li>&nbsp; &nbsp; channel_handler = MessageHandler(Filters.chat(int(CHANNEL_ID)), forward_message)<br /><li>&nbsp; &nbsp; dispatcher.add_handler(channel_handler)<br /><li>&nbsp; &nbsp; updater.job_queue.run_daily(reset_processed_messages, time=datetime.time(0, 0, 0, tzinfo=pytz.timezone('Asia/Shanghai')))<br /><li>&nbsp; &nbsp; updater.start_polling()<br /><li>&nbsp; &nbsp; updater.idle()<br /><li>if __name__ == '__main__':<br /><li>&nbsp; &nbsp; main()<br /><li></ol></div><em onclick="copycode($('code_Mnz'));">复制代码</em></div><br />
<br />
<br />
# 安装方式用宝塔或者1panel的话<br />
# 将代码在网站目录下创建 bot.py 文件<br />
# 例如宝塔路径 /www/wwwroot/telegram/bot.py<br />
<br />
# SSH 连接服务器<br />
<br />
#安装python3环境<br />
<br /><div class="blockcode"><div id="code_bfc"><ol><li>sudo apt update </ol></div><em onclick="copycode($('code_bfc'));">复制代码</em></div><div class="blockcode"><div id="code_P7B"><ol><li>sudo apt install python3-pip </ol></div><em onclick="copycode($('code_P7B'));">复制代码</em></div><br />
<br />
# CD到代码路径的目录<br />
<br /><div class="blockcode"><div id="code_CNz"><ol><li>cd /www/wwwroot/telegram/ </ol></div><em onclick="copycode($('code_CNz'));">复制代码</em></div><br />
<br />
#在代码路径的目录下 安装依赖<br />
<br /><div class="blockcode"><div id="code_zl5"><ol><li>pip install python-telegram-bot pytz </ol></div><em onclick="copycode($('code_zl5'));">复制代码</em></div><div class="blockcode"><div id="code_iYq"><ol><li>pip3 install -r requirements.txt </ol></div><em onclick="copycode($('code_iYq'));">复制代码</em></div><br />
<br />
查询环境<br /><div class="blockcode"><div id="code_n9J"><ol><li>python3 --version </ol></div><em onclick="copycode($('code_n9J'));">复制代码</em></div><div class="blockcode"><div id="code_xNz"><ol><li>pip3 --version </ol></div><em onclick="copycode($('code_xNz'));">复制代码</em></div><br />
<br />
确保环境没问题后<br />
<br />
开机自启,到宝塔 计划任务&nbsp;&nbsp;创建任务&nbsp;&nbsp;每小时执行一次<br />
<br />
写入以下内容<br /><div class="blockcode"><div id="code_Fxk"><ol><li>pkill -f bot.py<br /><li>cd /www/wwwroot/telegram<br /><li>nohup python3 bot.py &amp; </ol></div><em onclick="copycode($('code_Fxk'));">复制代码</em></div><br />
页: [1] 2
查看完整版本: ....求屌大mjj帮忙写个tg机器人✅✅