本文整理汇总了Java中org.telegram.telegrambots.api.objects.Message类的典型用法代码示例。如果您正苦于以下问题:Java Message类的具体用法?Java Message怎么用?Java Message使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Message类属于org.telegram.telegrambots.api.objects包,在下文中一共展示了Message类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: onUpdateReceived
import org.telegram.telegrambots.api.objects.Message; //导入依赖的package包/类
public void onUpdateReceived(Update update) {
if (update.hasMessage() && update.getMessage().hasPhoto()) {
Message message = update.getMessage();
long chatId = message.getChatId();
// get the last photo - it seems to be the bigger one
List<PhotoSize> photos = message.getPhoto();
PhotoSize photo = photos.get(photos.size() - 1);
String id = photo.getFileId();
try {
GetFile getFile = new GetFile();
getFile.setFileId(id);
String filePath = getFile(getFile).getFileUrl(getBotToken());
// TODO: cache images?
logger.info("== DOWNLOADING IMAGE " + filePath);
URL url = new URL(filePath);
String caption = Classifier.classify(url.openStream());
logger.info("Caption for image " + filePath + ":\n" + caption);
sendPhotoMessage(chatId, id, caption);
} catch (Exception e) {
e.printStackTrace();
}
}
}
示例2: onUpdateReceived
import org.telegram.telegrambots.api.objects.Message; //导入依赖的package包/类
@Override
public void onUpdateReceived(Update update) {
if (update.hasMessage()) {
Message message = update.getMessage();
System.out.println(message);
List<User> newUsers = update.getMessage().getNewChatMembers();
if (newUsers != null) {
String welcomeMessage = "Bienvenido al grupo Java Studio: ";
for (User newUser : newUsers) {
String user = newUser.getUserName().equals("null") ? newUser.getFirstName()
: "@" + newUser.getUserName();
welcomeMessage += user + " ";
}
SendMessage welcomeSendMessage = new SendMessage()
.setChatId(message.getChatId())
.setText(welcomeMessage);
try {
sendMessage(welcomeSendMessage);
} catch (TelegramApiException ex) {
Logger.getLogger(CoffeeAndyBot.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
示例3: setupContext
import org.telegram.telegrambots.api.objects.Message; //导入依赖的package包/类
public static void setupContext(Message message) {
User user = message.getFrom();
Chat chat = message.getChat();
Long chatId = chat.getId();
if (!tgUserContexts.containsKey(chatId)) {
TgContext value = new TgContext();
value.setUser(user);
value.setChat(chat);
tgUserContexts.put(chatId, value);
}
TgContext tgContext = tgUserContexts.get(chatId);
tgContextThreadLocal.set(tgContext);
}
示例4: execute
import org.telegram.telegrambots.api.objects.Message; //导入依赖的package包/类
public TgRequestResult execute(Object instance, List<Message> messages) {
Object[] data = new Object[parameters.length];
for (int i = 0; i < messages.size(); i++) {
Message message = messages.get(i);
TgParameterType parameterType = parameters[i];
Object datum = parameterType.getData(message);
data[i] = datum;
}
try {
Object result = method.invoke(instance, data);
if (result instanceof TgRequestResult) {
return (TgRequestResult) result;
}
} catch (ReflectiveOperationException e) {
throw new TgActionRequestHandlerException(e);
}
return TgRequestResult.OK;
}
示例5: onUpdateReceived
import org.telegram.telegrambots.api.objects.Message; //导入依赖的package包/类
@Override
public synchronized void onUpdateReceived(final Update update) {
if (update.hasMessage()) {
final Message message = update.getMessage();
Thread processThread = new Thread(() -> {
TgContextHolder.setupContext(message);
TgContext tgContext = TgContextHolder.currentContext();
synchronized (tgContext) {
try {
messageHandler.handleMessage(message, tgContext);
}catch (TgBotMessageHandleException e) {
send(tgContext.getChatId(), e.getMessage());
}
}
});
processThread.setName("TgRequest");
processThread.setDaemon(true);
processThread.start();
}
}
示例6: findMatchingActions
import org.telegram.telegrambots.api.objects.Message; //导入依赖的package包/类
private List<TgAction> findMatchingActions(final Message message) {
return
actions.stream()
.filter(action -> {
ValidType commandValidatorVType = ValidType.NOT_EXISTING;
ValidType regexVType = ValidType.NOT_EXISTING;
if (action.isCommandValidatorExisting(message)) {
CommandValidator validator = getCommandValidator(action.getCommandValidatorClass());
commandValidatorVType = ValidType.fromBoolean(validator.validate(message));
}
if (action.isRegexExisting()) {
if (message.hasText() && action.hasRegex()) {
regexVType = ValidType.fromBoolean(action.isRegexMatching(message.getText()));
}
}
return commandValidatorVType.chainType(regexVType).isValid();
}).collect(Collectors.toList());
}
示例7: sendReplyKeyboard
import org.telegram.telegrambots.api.objects.Message; //导入依赖的package包/类
public Message sendReplyKeyboard(long chatId, String message, List<List<String>> keyboardText, boolean oneTimeKeyboard) {
String[][] keyboardTextArray = new String[keyboardText.size()][];
for (int i = 0; i < keyboardText.size(); i++) {
List<String> keyboardRow = keyboardText.get(i);
String[] keyboardRowArray = new String[keyboardRow.size()];
keyboardTextArray[i] = keyboardRowArray;
for (int j = 0; j < keyboardRow.size(); j++) {
keyboardRowArray[j] = keyboardRow.get(j);
}
}
return sendReplyKeyboard(chatId, message, keyboardTextArray, oneTimeKeyboard);
}
示例8: executeCommand
import org.telegram.telegrambots.api.objects.Message; //导入依赖的package包/类
/**
* Executes a command action if the command is registered.
*
* @note If the command is not registered and there is a default consumer,
* that action will be performed
*
* @param absSender absSender
* @param message input message
* @return True if a command or default action is executed, false otherwise
*/
public final boolean executeCommand(AbsSender absSender, Message message) {
if (message.hasText()) {
String text = message.getText();
if (text.startsWith(BotCommand.COMMAND_INIT_CHARACTER)) {
String commandMessage = text.substring(1);
String[] commandSplit = commandMessage.split(BotCommand.COMMAND_PARAMETER_SEPARATOR);
String command = removeUsernameFromCommandIfNeeded(commandSplit[0]);
if (commandRegistryMap.containsKey(command)) {
String[] parameters = Arrays.copyOfRange(commandSplit, 1, commandSplit.length);
commandRegistryMap.get(command).execute(absSender, message.getFrom(), message.getChat(), parameters);
return true;
} else if (defaultConsumer != null) {
defaultConsumer.accept(absSender, message);
return true;
}
}
}
return false;
}
示例9: onUpdateReceived
import org.telegram.telegrambots.api.objects.Message; //导入依赖的package包/类
@Override
public void onUpdateReceived(Update update) {
// 업데이트가 메시지를 가지고 있는지 확인합니다.
if(update.hasMessage()){
Message message = update.getMessage();
String answerMessage = message.getText().toString(); // 메시지를 String 형태로 받아옵니다.
char answerLocation[] = new char[10];
if(answerMessage.startsWith("/ㅁㅅ ")) {
answerMessage.getChars(4, answerMessage.length(), answerLocation, 0);
// 지역명이 전달되었다면 char 배열로 받아옵니다. (최대 10자로 제한두고자 함)
SendMessage sendMessageRequest = new SendMessage();
sendMessageRequest.setChatId(message.getChatId().toString()); //who should get the message? the sender from which we got the message...
String str = new String(answerLocation, 0, answerMessage.length()-4); // 지역명을 String으로 재반환합니다.
sendMessageRequest.setText(AnswerReport.getPMStatus(str)); // 지역명을 AnswerReport 클래스로 넘겨줍니다.
try {
sendMessage(sendMessageRequest); //at the end, so some magic and send the message ;)
} catch (TelegramApiException e) {
e.printStackTrace();
}//end catch()
}//end if()
}//end if()
}
示例10: onUpdateReceived
import org.telegram.telegrambots.api.objects.Message; //导入依赖的package包/类
@Override
public void onUpdateReceived(Update update) {
if (update.hasMessage()) {
Message message = update.getMessage();
SendMessage response = new SendMessage();
Long chatId = message.getChatId();
response.setChatId(chatId);
String text = message.getText();
response.setText(text);
try {
sendMessage(response);
logger.info("Sent message \"{}\" to {}", text, chatId);
} catch (TelegramApiException e) {
logger.error("Failed to send message \"{}\" to {} due to error: {}", text, chatId, e.getMessage());
}
}
}
示例11: showMainMenu
import org.telegram.telegrambots.api.objects.Message; //导入依赖的package包/类
public void showMainMenu(final Message request, final TGSession session) throws TelegramApiException {
final ReplyKeyboardMarkup replyKeyboardMarkup = getMainMenuKeyboard();
final SendMessage msg =
createMessageWithKeyboard(request.getChatId().toString(), request.getMessageId(), replyKeyboardMarkup);
if (session.isNew()) {
session.setNew(false);
msg.setText("Привет, либо ты тут в первый раз, "
+ "либо мы _берега попутали_.\n"
+ "Пиши -- от души.");
} else {
msg.setText("Пиши -- от души.");
}
sendMessage(msg);
}
示例12: executeCommand
import org.telegram.telegrambots.api.objects.Message; //导入依赖的package包/类
/**
* Executes a command action if the command is registered.
*
* @note If the command is not registered and there is a default consumer,
* that action will be performed
*
* @param absSender absSender
* @param message input message
* @return True if a command or default action is executed, false otherwise
*/
public final boolean executeCommand(AbsSender absSender, Message message) {
if (message.hasText()) {
String text = message.getText();
if (text.startsWith(BotCommand.COMMAND_INIT_CHARACTER)) {
String commandMessage = text.substring(1);
String[] commandSplit = commandMessage.split(BotCommand.COMMAND_PARAMETER_SEPARATOR_REGEXP);
String command = removeUsernameFromCommandIfNeeded(commandSplit[0]);
if (commandRegistryMap.containsKey(command)) {
String[] parameters = Arrays.copyOfRange(commandSplit, 1, commandSplit.length);
commandRegistryMap.get(command).processMessage(absSender, message, parameters);
return true;
} else if (defaultConsumer != null) {
defaultConsumer.accept(absSender, message);
return true;
}
}
}
return false;
}
示例13: processNonCommandUpdate
import org.telegram.telegrambots.api.objects.Message; //导入依赖的package包/类
@Override
public void processNonCommandUpdate(Update update) {
if (update.hasMessage()) {
Message message = update.getMessage();
if (!DatabaseManager.getInstance().getUserStateForCommandsBot(message.getFrom().getId())) {
return;
}
if (message.hasText()) {
SendMessage echoMessage = new SendMessage();
echoMessage.setChatId(message.getChatId());
echoMessage.setText("Hey heres your message:\n" + message.getText());
try {
sendMessage(echoMessage);
} catch (TelegramApiException e) {
BotLogger.error(LOGTAG, e);
}
}
}
}
示例14: onWaitingChannelMessage
import org.telegram.telegrambots.api.objects.Message; //导入依赖的package包/类
private void onWaitingChannelMessage(Message message) throws InvalidObjectException {
try {
if (message.getText().equals(CANCEL_COMMAND)) {
userState.remove(message.getFrom().getId());
sendHelpMessage(message.getChatId(), message.getMessageId(), null);
} else {
if (message.getText().startsWith("@") && !message.getText().trim().contains(" ")) {
sendMessage(getMessageToChannelSent(message));
sendMessageToChannel(message.getText(), message);
userState.remove(message.getFrom().getId());
} else {
sendMessage(getWrongUsernameMessage(message));
}
}
} catch (TelegramApiException e) {
BotLogger.error(LOGTAG, e);
}
}
示例15: onSetLanguageCommand
import org.telegram.telegrambots.api.objects.Message; //导入依赖的package包/类
private void onSetLanguageCommand(Message message, String language) throws InvalidObjectException {
SendMessage sendMessageRequest = new SendMessage();
sendMessageRequest.setChatId(message.getChatId());
ReplyKeyboardMarkup replyKeyboardMarkup = new ReplyKeyboardMarkup();
List<LocalisationService.Language> languages = LocalisationService.getSupportedLanguages();
List<KeyboardRow> commands = new ArrayList<>();
for (LocalisationService.Language languageItem : languages) {
KeyboardRow commandRow = new KeyboardRow();
commandRow.add(languageItem.getCode() + " --> " + languageItem.getName());
commands.add(commandRow);
}
replyKeyboardMarkup.setResizeKeyboard(true);
replyKeyboardMarkup.setOneTimeKeyboard(true);
replyKeyboardMarkup.setKeyboard(commands);
replyKeyboardMarkup.setSelective(true);
sendMessageRequest.setReplyMarkup(replyKeyboardMarkup);
sendMessageRequest.setText(LocalisationService.getString("chooselanguage", language));
try {
sendMessage(sendMessageRequest);
languageMessages.add(message.getFrom().getId());
} catch (TelegramApiException e) {
BotLogger.error(LOGTAG, e);
}
}