怎么用带有js/jQuery的电报机器人以html的形式发送照片?

本教程将介绍如何用带有js/jQuery的电报机器人以html的形式发送照片?的处理方法,这篇教程是从别的地方看到的,然后加了一些国外程序员的疑问与解答,希望能对你有所帮助,好了,下面开始学习吧。

怎么用带有js/jQuery的电报机器人以html的形式发送照片? 教程 第1张

问题描述

之前,我有一些类似于向用户发送消息的代码

<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&amp;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

问题是,我怎么使用URL中带有回复ID的js/jQuery发送带有电报机器人的表单的照片?

推荐答案

您的代码运行正常,但在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>

将此代码添加到页面的标题部分。

好了关于怎么用带有js/jQuery的电报机器人以html的形式发送照片?的教程就到这里就结束了,希望趣模板源码网找到的这篇技术文章能帮助到大家,更多技术教程可以在站内搜索。