如何使用带有 js/jquery html 形式的电报机器人发送照片?

How to send a photo using telegram bot with js/jquery in html form?

以前我有一些像这样的代码来向用户发送消息

<button class="notif btn btn-success"
href="https://api.telegram.org/bot{{ config('app.token') }}/sendMessage?chat_id={{ $rp->report_idsender }}&text=Halo%20{{ $rp->sender_name }}%20permintaan%20anda%20dengan%20id%20{{ $rp->id }}%20sudah%20di%20close%20">Notif</button>

我正在使用 jquery & js 从 href 获取 URL 并执行 HTTPS post 请求它对我来说非常完美

<script type="text/javascript">
$(".notif").unbind().click(function() {
var url = $(this).attr("href");
console.log(url);
var exe = $.post(url, function() {
alert('Success');
})
});
</script>

但现在我想用回复 ID 将照片发送到 Telegram 上的群组,代码如下所示:

<form method="POST"
action="https://api.telegram.org/bot{{ config('app.token') }}/sendPhoto" enctype="multipart/form-data">
<input type="text" name="chat_id" value="{{ config('app.idgroup') }}" hidden />
<input type="text" name="reply_to_message_id" value="{{ $rp->msg_id }}" hidden />
<input type="text" name="allow_sending_without_reply" value="true" hidden />
<br />
<label for="caption"> Caption</label>
<input type="text" name="caption" placeholder="caption" />
<br />
<input type="file" name="photo" />
<br />
<input type="submit" value="sendPhoto" />
</form>

此代码的问题在于,在我提交表单后,它打开了一个包含 JSON 响应的页面,而我只想像在之前的代码中那样提醒它。

json response tab picture

问题是,如何使用 js/jquery 和 URL 中的回复 ID 发送带有电报机器人表格的照片??

您的代码工作正常,但是重定向到 action 中设置的页面,您可以使用以下代码来防止默认的提交按钮行为和 ajax 保持在同一页面并显示成功留言。

<script type="text/javascript">
    $(document).on("submit", "form", function (event) {
        event.preventDefault();
        $.ajax({
            url: $(this).attr("action"),
            type: $(this).attr("method"),
            dataType: "JSON",
            data: new FormData(this),
            processData: false,
            contentType: false,
            success: function (data, status) {
                alert('Success');
            },
            error: function (xhr, desc, err) {
                alert('Error');
            }
        });
    });
</script>

将此代码添加到页面的头部。