Closed stranck closed 7 years ago
Maybe i will push some my bots to github later. You can use readme, its very easy
Another question: Can I have the message_id when I send a message using "bot.sendMessage(chat, text);"
SendResponse sendResponse = bot.sendMessage(chatId, "short message sending"); Message message = sendResponse.message(); message.id();
hey, would be nice if you could ad a little Bot example
I've tried to set up a little example using this APIs but where do I take the chatId?? I have no chat at the beginning of the program..
@fpompermaier write message to your bot in telegram client. After this u will be able to answer and will see chatId in getUpdates.
I view now the update for 2.0 bot. The question is: how can I change the text of an already sended message?
Thanks @pengrad!
@stranck bot.execute(new EditMessageText(chatId, messageId, text));
@fpompermaier
private Integer getUserId( final Update update ) {
if ( update.message() != null ) {
return update.message().from().id();
} else if ( update.getCallback_query() != null ) {
return update.getCallback_query().getFrom().id();
} else if ( update.chosenInlineResult() != null ) {
return update.chosenInlineResult().from().id();
} else if ( update.inlineQuery() != null ) {
return update.inlineQuery().from().id();
}
return -1;
}
I use this code to send a message:
SendResponse sendResponse = bot.execute(new SendMessage(channel, "text"));
But it return this:
How can I put the chatId into a variable?
@stranck Seems you are using not last version of library. Try 2.0.1
How can I disable web page preview? Like bot.execute(new EditMessageText(channel, mesasge_id, liveEnd).parseMode(ParseMode.HTML).disableWebPagePreview(true));
?
@stranck yes
Hi, i would also appreciate to see an example of a bot. At least an echo-bot, which simply repeats everything he receives. Maybe some of you guys can post such an example here?
@localhoster you can try to make this echo-bot. I will help if you get troubles.
@pengrad How can I get the offset of an update?
Hi @pengrad , sure:) I´ve got this, which is actually working well. But i have doubts because of its "traffic efficiency". Will it be enough if i built a timer, which halts the while-loop for a second? Or is it a better way to make bot wait for incoming messages without generating tons of equerries? I´ve heard something about long-polling and webhooks but the stuff isn´t so self explaining like "while-loop":D
public class Start {
public static void main(String[] args) {
TelegramBot bot = TelegramBotAdapter.build(botToken);
String chatId = chatId;
Integer updateId = 0;
SendResponse sendResponse = bot.execute(
new SendMessage(chatId, "Hi! I am an echo bot."));
while (true) {
GetUpdatesResponse updatesResponse = bot.execute(new GetUpdates().limit(0).offset(updateId).timeout(0));
List<Update> updates = updatesResponse.updates();
if (updates.size() > 0) {
for (Update update : updates ) {
sendResponse = bot.execute(new SendMessage(chatId, "You sad " + update.message().text()));
updateId = update.updateId() +1;
}
}
}
}
}
@localhoster
bot.execute(getUpdates.offset(updateId), new Callback<GetUpdates, GetUpdatesResponse>() {
@Override
public void onResponse(GetUpdates request, GetUpdatesResponse response) {
}
@Override
public void onFailure(GetUpdates request, IOException e) {
}
});
@pengrad I'm also quite interested by an example to see how the API is intended to be used. I itererated on the example given above (thanks @localhoster !), one version without and with callbacks. I'll be happy to hear comments and best practices advise. Especially the callback version, seems unnatural, there must be some better way.
public static void example(String token) {
TelegramBot bot = TelegramBotAdapter.build(token);
int updateId = 0;
int timeoutInSecond = 7;
while (true) {
System.out.println("Checking for updates...");
GetUpdatesResponse updatesResponse = bot.execute(
new GetUpdates().limit(100).offset(updateId).timeout(timeoutInSecond));
List<Update> updates = updatesResponse.updates();
if (updates.size() > 0) {
for (Update update : updates) {
System.out.println("Update: " + update);
if (update.message() != null) {
bot.execute(
new SendMessage(
update.message().from().id(),
String.format(
"You said %s",
update.message().text() == null
? "nothing"
: update.message().text())));
}
updateId = update.updateId() + 1;
}
}
}
}
import com.pengrad.telegrambot.Callback;
import com.pengrad.telegrambot.TelegramBot;
import com.pengrad.telegrambot.TelegramBotAdapter;
import com.pengrad.telegrambot.model.Update;
import com.pengrad.telegrambot.request.GetUpdates;
import com.pengrad.telegrambot.request.SendMessage;
import com.pengrad.telegrambot.response.GetUpdatesResponse;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
public class Main {
public static void main(String[] args) {
System.out.println("Starting");
try {
new EchoBot().run("token");
} catch (Exception e) {
e.printStackTrace();
}
}
private static class EchoBot {
private static int LONG_POOLING_TIMEOUT_IN_SECONDS = 7;
private AtomicInteger updateId = new AtomicInteger(0);
public void run(String token) throws InterruptedException {
TelegramBot bot = TelegramBotAdapter.build(token);
while (true) {
System.out.println("Checking for updates...");
bot.execute(
new GetUpdates()
.limit(100)
.offset(updateId.get())
.timeout(LONG_POOLING_TIMEOUT_IN_SECONDS),
new Callback<GetUpdates, GetUpdatesResponse>() {
@Override
public void onResponse(GetUpdates request, GetUpdatesResponse response) {
for (Update update : response.updates()) {
System.err.println("Update: " + update);
if (update.message() != null) {
bot.execute(
new SendMessage(
update.message().from().id(),
String.format(
"You said %s",
update.message().text() == null
? "nothing"
: update.message().text())));
}
updateId.set(update.updateId() + 1);
}
}
@Override
public void onFailure(GetUpdates request, IOException e) {
e.printStackTrace();
}
});
Thread.sleep(LONG_POOLING_TIMEOUT_IN_SECONDS * 1000);
}
}
}
}
Another thing: how can I convert from chat to string?
@stranck You can search for JSON to Java. Here is an link that will help you convert a chat to String. It is pretty easy to understand. https://www.tutorialspoint.com/json/json_java_example.htm
I've posted some examples in sample
module
Where I can find example of telegram bot using your library?