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

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

<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 发布请求,它对我来说非常适合

<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 发送带有电报机器人表单的照片??

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

您的代码运行良好,但重定向到 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>

在页面的头部添加此代码。

本文链接:https://www.f2er.com/1304.html

大家都在问