te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>python - The function of replying to telegram bot messages does not work. How it fix? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - The function of replying to telegram bot messages does not work. How it fix? - Stack Overflow

programmeradmin4浏览0评论

The main task during development is a technical support bot. The user writes a problem to the telegram bot, which forwards it to a supergroup topic. Inside the topic, support discusses the problem and calls the reply function to send information to the user in private messages. It is not possible to correctly call the reply method, the bot, instead of responding to reply, simply forwards all messages from the topic to private messages. Example System like Livegram Bot.

async def handle_group_replies(update: Update, context: CallbackContext):
    """Handler message group"""
    # Check message in group
    if update.message.reply_to_message and update.message.chat_id == GROUP_CHAT_ID:
        # Check message reply bot
        if update.message.reply_to_message.from_user.id == context.bot.id:
            # Take ID topic from message reply
            thread_id = update.message.reply_to_message.message_thread_id

            # Check topic in dictionary topic_to_user
            if thread_id in topic_to_user:
                user_id = topic_to_user[thread_id]  # Take ID user from ID topic

                # Type message 
                try:
                    if update.message.text:
                        await context.bot.send_message(
                            chat_id=user_id,
                            text=update.message.text
                        )
                    elif update.message.photo:
                        await context.bot.send_photo(
                            chat_id=user_id,
                            photo=update.message.photo[-1].file_id
                        )
                    elif update.message.document:
                        await context.bot.send_document(
                            chat_id=user_id,
                            document=update.message.document.file_id
                        )
                    elif update.message.audio:
                        await context.bot.send_audio(
                            chat_id=user_id,
                            audio=update.message.audio.file_id
                        )
                    elif update.message.video:
                        await context.bot.send_video(
                            chat_id=user_id,
                            video=update.message.video.file_id
                        )
                    elif update.message.voice:
                        await context.bot.send_voice(
                            chat_id=user_id,
                            voice=update.message.voice.file_id
                        )
                    elif update.message.sticker:
                        await context.bot.send_sticker(
                            chat_id=user_id,
                            sticker=update.message.sticker.file_id
                        )

                    logger.info(f"Reply from the group was sent to the user (ID: {user_id}).")
                except Exception as e:
                    logger.error(f"Error sending response to user: {e}")
                    await update.message.reply_text("Failed to send message.")
            else:
                logger.warning(f"Topic with ID {thread_id} not found in topic_to_user.")
        else:
            logger.debug(f"The message is not a response to a bot message, ignore it.")
    else:
        logger.debug("The message is not a response or was not sent in the group, ignore it.")

At first I tried to work with text, but it turned out that the bot was sending None instead of ignoring it, so I added the appropriate check. Additionally, I tried to rewrite it through another library, but returned to python-telegram-bot. In other cases, the message was completely ignored, for example:


if update.message.reply_to_message and update.message.chat_id == GROUP_CHAT_ID:
        thread_id = update.message.reply_to_message.message_thread_id

        if thread_id in topic_to_user:
            user_id = topic_to_user[thread_id]

            original_message = update.message.reply_to_message

            forward_message = f"{original_message.text}"

            try:
                await context.bot.send_message(chat_id=user_id, text=forward_message)
发布评论

评论列表(0)

  1. 暂无评论