当前位置: 首页 > news >正文

网站开发属于什么大学专业网站建设算入会计分录

网站开发属于什么大学专业,网站建设算入会计分录,网站未在腾讯云备案,一流的嘉兴网站建设我们今天的任务是创建一个“照片”机器人,该机器人将向用户发送照片。这只是一个示例,因此不会从网上获取照片,也不支持群聊功能。仅处理本地图片。但有一个好处: 我们将学会如何创建自定义键盘,如何发送照片以及创建指令。准备图…

我们今天的任务是创建一个“照片”机器人,该机器人将向用户发送照片。这只是一个示例,因此不会从网上获取照片,也不支持群聊功能。仅处理本地图片。但有一个好处: 我们将学会如何创建自定义键盘,如何发送照片以及创建指令。

准备图片

让我们下载5张完全不知名的照片。请注意: 我们将多次向用户发送相同的文件,因此我们应在Telegram服务器的流量和磁盘空间上精打细算。令人惊叹的是,我们只需在服务器上传一次文件,然后就可以通过唯一的文件ID来发送文件(图片、音频文件、文档、语音信息等) 。好了,那么现在让我们在发送给机器人时获取每张图片的ID

一如既往,在intelildea中创建一个新项目,并在src目录下创建两个文件:Main.java和PhotoBot.java。打开第一个文件,然后输入如下内容:

import org.telegram.telegrambots.ApiContextInitializer;
import org.telegram.telegrambots.TelegramBotsApi;
import org.telegram.telegrambots.exceptions.TelegramApiException;public class Main {public static void main(String[] args) {ApiContextInitializer.init();TelegramBotsApi botsApi = new TelegramBotsApi();try {botsApi.registerBot(new PhotoBot());} catch (TelegramApiException e) {e.printStackTrace();}System.out.println("PhotoBot successfully started!");}
}

这段代码将在我们的机器人成功启动时打印“PhotoBot successfully started!”。然后,保存它并打开PhotoBot.java文件。请粘贴之前课程中的以下代码:

import org.telegram.telegrambots.api.methods.send.SendMessage;
import org.telegram.telegrambots.api.objects.Update;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.exceptions.TelegramApiException;public class PhotoBot extends TelegramLongPollingBot {@Overridepublic void onUpdateReceived(Update update) {// We check if the update has a message and the message has textif (update.hasMessage() && update.getMessage().hasText()) {// Set variablesString message_text = update.getMessage().getText();long chat_id = update.getMessage().getChatId();SendMessage message = new SendMessage() // Create a message object object.setChatId(chat_id).setText(message_text);try {sendMessage(message); // Sending our message object to user} catch (TelegramApiException e) {e.printStackTrace();}}}@Overridepublic String getBotUsername() {// Return bot username// If bot username is @MyAmazingBot, it must return 'MyAmazingBot'return "PhotoBot";}@Overridepublic String getBotToken() {// Return bot token from BotFatherreturn "12345:qwertyuiopASDGFHKMK";}
}

现在让我们更新我们的 onUpdateReceived 方法。我们想要发送我们发送给机器人的图片的file_id。让我们检查消息中是否包含照片对象:

@Override
public void onUpdateReceived(Update update) {// We check if the update has a message and the message has textif (update.hasMessage() && update.getMessage().hasText()) {// Set variablesString message_text = update.getMessage().getText();long chat_id = update.getMessage().getChatId();SendMessage message = new SendMessage() // Create a message object object.setChatId(chat_id).setText(message_text);try {sendMessage(message); // Sending our message object to user} catch (TelegramApiException e) {e.printStackTrace();}} else if (update.hasMessage() && update.getMessage().hasPhoto()) {// Message contains photo}
}

我们希望我们的机器人发送 file_id 对应的图片, 需要这样做

else if (update.hasMessage() && update.getMessage().hasPhoto()) {// Message contains photo// Set variableslong chat_id = update.getMessage().getChatId();// Array with photo objects with different sizes// We will get the biggest photo from that arrayList<PhotoSize> photos = update.getMessage().getPhoto();// Know file_idString f_id = photos.stream().sorted(Comparator.comparing(PhotoSize::getFileSize).reversed()).findFirst().orElse(null).getFileId();// Know photo widthint f_width = photos.stream().sorted(Comparator.comparing(PhotoSize::getFileSize).reversed()).findFirst().orElse(null).getWidth();// Know photo heightint f_height = photos.stream().sorted(Comparator.comparing(PhotoSize::getFileSize).reversed()).findFirst().orElse(null).getHeight();// Set photo captionString caption = "file_id: " + f_id + "\nwidth: " + Integer.toString(f_width) + "\nheight: " + Integer.toString(f_height);SendPhoto msg = new SendPhoto().setChatId(chat_id).setPhoto(f_id).setCaption(caption);try {sendPhoto(msg); // Call method to send the photo with caption} catch (TelegramApiException e) {e.printStackTrace();}
}

太棒了!现在我们知道照片的文件ID,所以我们可以通过文件ID发送它们。让我们在发送命令 /pic 时让我们的机器人用那张照片回答。

if (update.hasMessage() && update.getMessage().hasText()) {// Set variablesString message_text = update.getMessage().getText();long chat_id = update.getMessage().getChatId();if (message_text.equals("/start")) {// User send /startSendMessage message = new SendMessage() // Create a message object object.setChatId(chat_id).setText(message_text);try {sendMessage(message); // Sending our message object to user} catch (TelegramApiException e) {e.printStackTrace();}} else if (message_text.equals("/pic")) {// User sent /picSendPhoto msg = new SendPhoto().setChatId(chat_id).setPhoto("AgADAgAD6qcxGwnPsUgOp7-MvnQ8GecvSw0ABGvTl7ObQNPNX7UEAAEC").setCaption("Photo");try {sendPhoto(msg); // Call method to send the photo} catch (TelegramApiException e) {e.printStackTrace();}} else {// Unknown commandSendMessage message = new SendMessage() // Create a message object object.setChatId(chat_id).setText("Unknown command");try {sendMessage(message); // Sending our message object to user} catch (TelegramApiException e) {e.printStackTrace();}}
}

现在让我们来看看ReplyKeyboardMarkup。我们将创建这样的自定义键盘

好吧,你已经知道如何让我们的机器人识别命令了。让我们再做一个if 分支处理 /markup 命令。

请记住!当您按下按钮时,它会将按钮上的文本发送给机器人。例如,如果我在按钮上输入“你好”文本,那么当我按下按钮时,它就会为我发送“你好”的文本。

else if (message_text.equals("/markup")) {SendMessage message = new SendMessage() // Create a message object object.setChatId(chat_id).setText("Here is your keyboard");// Create ReplyKeyboardMarkup objectReplyKeyboardMarkup keyboardMarkup = new ReplyKeyboardMarkup();// Create the keyboard (list of keyboard rows)List<KeyboardRow> keyboard = new ArrayList<>();// Create a keyboard rowKeyboardRow row = new KeyboardRow();// Set each button, you can also use KeyboardButton objects if you need something else than textrow.add("Row 1 Button 1");row.add("Row 1 Button 2");row.add("Row 1 Button 3");// Add the first row to the keyboardkeyboard.add(row);// Create another keyboard rowrow = new KeyboardRow();// Set each button for the second linerow.add("Row 2 Button 1");row.add("Row 2 Button 2");row.add("Row 2 Button 3");// Add the second row to the keyboardkeyboard.add(row);// Set the keyboard to the markupkeyboardMarkup.setKeyboard(keyboard);// Add it to the messagemessage.setReplyMarkup(keyboardMarkup);try {sendMessage(message); // Sending our message object to user} catch (TelegramApiException e) {e.printStackTrace();}
}

太棒了!现在让我们教我们的机器人对这些按钮做出反应:

else if (message_text.equals("Row 1 Button 1")) {SendPhoto msg = new SendPhoto().setChatId(chat_id).setPhoto("AgADAgAD6qcxGwnPsUgOp7-MvnQ8GecvSw0ABGvTl7ObQNPNX7UEAAEC").setCaption("Photo");try {sendPhoto(msg); // Call method to send the photo} catch (TelegramApiException e) {e.printStackTrace();}
}

现在,当用户按下带有“Row 1 Button 1”文本的按钮时,机器人将通过文件ID向用户发送图片:

当用户向机器人发送 /hide 命令时,让我们执行“隐藏键盘”功能。这可以通过ReplyMarkupRemove来实现。

else if (message_text.equals("/hide")) {SendMessage msg = new SendMessage().setChatId(chat_id).setText("Keyboard hidden");ReplyKeyboardRemove keyboardMarkup = new ReplyKeyboardRemove();msg.setReplyMarkup(keyboardMarkup);try {sendMessage(msg); // Call method to send the photo} catch (TelegramApiException e) {e.printStackTrace();}
}

查看完整代码

import org.telegram.telegrambots.ApiContextInitializer;
import org.telegram.telegrambots.TelegramBotsApi;
import org.telegram.telegrambots.exceptions.TelegramApiException;public class Main {public static void main(String[] args) {ApiContextInitializer.init();TelegramBotsApi botsApi = new TelegramBotsApi();try {botsApi.registerBot(new PhotoBot());} catch (TelegramApiException e) {e.printStackTrace();}System.out.println("PhotoBot successfully started!");}
}
import org.telegram.telegrambots.api.methods.send.SendMessage;
import org.telegram.telegrambots.api.methods.send.SendPhoto;
import org.telegram.telegrambots.api.objects.PhotoSize;
import org.telegram.telegrambots.api.objects.Update;
import org.telegram.telegrambots.api.objects.replykeyboard.ReplyKeyboardMarkup;
import org.telegram.telegrambots.api.objects.replykeyboard.ReplyKeyboardRemove;
import org.telegram.telegrambots.api.objects.replykeyboard.buttons.KeyboardRow;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.exceptions.TelegramApiException;import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;public class PhotoBot extends TelegramLongPollingBot {@Overridepublic void onUpdateReceived(Update update) {// We check if the update has a message and the message has textif (update.hasMessage() && update.getMessage().hasText()) {// Set variablesString message_text = update.getMessage().getText();long chat_id = update.getMessage().getChatId();if (message_text.equals("/start")) {SendMessage message = new SendMessage() // Create a message object object.setChatId(chat_id).setText(message_text);try {sendMessage(message); // Sending our message object to user} catch (TelegramApiException e) {e.printStackTrace();}} else if (message_text.equals("/pic")) {SendPhoto msg = new SendPhoto().setChatId(chat_id).setPhoto("AgADAgAD6qcxGwnPsUgOp7-MvnQ8GecvSw0ABGvTl7ObQNPNX7UEAAEC").setCaption("Photo");try {sendPhoto(msg); // Call method to send the photo} catch (TelegramApiException e) {e.printStackTrace();}} else if (message_text.equals("/markup")) {SendMessage message = new SendMessage() // Create a message object object.setChatId(chat_id).setText("Here is your keyboard");// Create ReplyKeyboardMarkup objectReplyKeyboardMarkup keyboardMarkup = new ReplyKeyboardMarkup();// Create the keyboard (list of keyboard rows)List<KeyboardRow> keyboard = new ArrayList<>();// Create a keyboard rowKeyboardRow row = new KeyboardRow();// Set each button, you can also use KeyboardButton objects if you need something else than textrow.add("Row 1 Button 1");row.add("Row 1 Button 2");row.add("Row 1 Button 3");// Add the first row to the keyboardkeyboard.add(row);// Create another keyboard rowrow = new KeyboardRow();// Set each button for the second linerow.add("Row 2 Button 1");row.add("Row 2 Button 2");row.add("Row 2 Button 3");// Add the second row to the keyboardkeyboard.add(row);// Set the keyboard to the markupkeyboardMarkup.setKeyboard(keyboard);// Add it to the messagemessage.setReplyMarkup(keyboardMarkup);try {sendMessage(message); // Sending our message object to user} catch (TelegramApiException e) {e.printStackTrace();}} else if (message_text.equals("Row 1 Button 1")) {SendPhoto msg = new SendPhoto().setChatId(chat_id).setPhoto("AgADAgAD6qcxGwnPsUgOp7-MvnQ8GecvSw0ABGvTl7ObQNPNX7UEAAEC").setCaption("Photo");try {sendPhoto(msg); // Call method to send the photo} catch (TelegramApiException e) {e.printStackTrace();}} else if (message_text.equals("/hide")) {SendMessage msg = new SendMessage().setChatId(chat_id).setText("Keyboard hidden");ReplyKeyboardRemove keyboardMarkup = new ReplyKeyboardRemove();msg.setReplyMarkup(keyboardMarkup);try {sendMessage(msg); // Call method to send the photo} catch (TelegramApiException e) {e.printStackTrace();}} else {SendMessage message = new SendMessage() // Create a message object object.setChatId(chat_id).setText("Unknown command");try {sendMessage(message); // Sending our message object to user} catch (TelegramApiException e) {e.printStackTrace();}}} else if (update.hasMessage() && update.getMessage().hasPhoto()) {// Message contains photo// Set variableslong chat_id = update.getMessage().getChatId();List<PhotoSize> photos = update.getMessage().getPhoto();String f_id = photos.stream().sorted(Comparator.comparing(PhotoSize::getFileSize).reversed()).findFirst().orElse(null).getFileId();int f_width = photos.stream().sorted(Comparator.comparing(PhotoSize::getFileSize).reversed()).findFirst().orElse(null).getWidth();int f_height = photos.stream().sorted(Comparator.comparing(PhotoSize::getFileSize).reversed()).findFirst().orElse(null).getHeight();String caption = "file_id: " + f_id + "\nwidth: " + Integer.toString(f_width) + "\nheight: " + Integer.toString(f_height);SendPhoto msg = new SendPhoto().setChatId(chat_id).setPhoto(f_id).setCaption(caption);try {sendPhoto(msg); // Call method to send the message} catch (TelegramApiException e) {e.printStackTrace();}}}@Overridepublic String getBotUsername() {// Return bot username// If bot username is @MyAmazingBot, it must return 'MyAmazingBot'return "PhotoBot";}@Overridepublic String getBotToken() {// Return bot token from BotFatherreturn "12345:qwertyuiopASDGFHKMK";}
}

http://www.dtcms.com/a/401940.html

相关文章:

  • 怎么做简易网站网页设计心得体会2篇
  • app开发与网站开发网站建设 浏览器兼容
  • 广州专业网站改版哪家好企业网站建设网站优化
  • 广州网站建设 名片制作 网站管理自助建站自己要做网站的来看下
  • 旅行社网站建设方案论文个人阿里云账号可以做网站备案
  • 国外免费做网站软件响应式网站用什么工具做
  • 手机网站 好处建设部网站上怎样查询企业业绩
  • php网站开发发展趋势518机械加工网
  • 来安县城乡规划建设局网站重庆市城乡建设施工安全管理总站网站
  • 网站的美观性诸城 建设外贸网站
  • 网站开发上传视频教程网站开发开发
  • 紫金网站建设价格php做网站
  • 标准论坛网站建设如何建立自己音乐网站
  • 购物网站建设的目标建设类似衣联网的网站
  • 长沙销售公司 网站网络营销案例范文
  • 南昌网站建设kaiu工程机械外贸网站建设
  • 长寿做网站长沙网站设计精选柚v米科技
  • 网站几几年做的怎么查编辑制作网页的基础是
  • seo厂家费用低最好的关键词排名优化软件
  • 网站规划的注意事项做番号网站违法么
  • 苏州天狮建设监理有限公司网站网站交易平台建设
  • 企业网站建设原则有哪些零售管理系统哪个软件好
  • 高邮做网站免费企业电话名录
  • 东莞建设工程交易中心门户网站嘉兴做网站费用
  • 立邦漆官方网站官网中英文双语网站 滑动切换
  • 电商网站建设策划网站首页一般做多大
  • 网站开发包括网站设计网络网站建设10大指标
  • 个人学做网站山东省住房和城乡建设厅服务网站
  • 做衣服网站的实验感想广州番禺最新通告
  • 福鼎市城市建设监察大队网站北京计算机编程培训学校