Telegram API: how to save ApiState to save signIn state
I used telegram api from this source: https://github.com/voleon/telegram-trivia-bot But my problem is how to get the user to login. Because after the application ceases to require a mobile phone from the user and receive an activation code from an SMS message. I have implemented Serializable to persist the ApiState object. but this method didn't solve my problem. this is the code for my ApiState:
package engine;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import org.telegram.api.TLConfig;
import org.telegram.api.TLDcOption;
import org.telegram.api.engine.storage.AbsApiState;
import org.telegram.mtproto.state.AbsMTProtoState;
import org.telegram.mtproto.state.ConnectionInfo;
import org.telegram.mtproto.state.KnownSalt;
/**
* Created by ex3ndr on 13.01.14.
*/
public class MemoryApiState implements AbsApiState,java.io.Serializable {
public HashMap<Integer, ConnectionInfo[]> connections = new HashMap<Integer, ConnectionInfo[]>();
private HashMap<Integer, byte[]> keys = new HashMap<Integer, byte[]>();
private HashMap<Integer, Boolean> isAuth = new HashMap<Integer, Boolean>();
private int primaryDc = 1;
public MemoryApiState(boolean isTest) {
connections.put(1, new ConnectionInfo[]{
new ConnectionInfo(1, 0, isTest ? "149.154.167.40" : "149.154.167.50", 443)
});
}
@Override
public synchronized int getPrimaryDc() {
return primaryDc;
}
@Override
public synchronized void setPrimaryDc(int dc) {
primaryDc = dc;
}
@Override
public synchronized boolean isAuthenticated(int dcId) {
if (isAuth.containsKey(dcId)) {
return isAuth.get(dcId);
}
return false;
}
@Override
public synchronized void setAuthenticated(int dcId, boolean auth) {
isAuth.put(dcId, auth);
}
@Override
public synchronized void updateSettings(TLConfig config) {
connections.clear();
HashMap<Integer, ArrayList<ConnectionInfo>> tConnections = new HashMap<Integer, ArrayList<ConnectionInfo>>();
int id = 0;
for (TLDcOption option : config.getDcOptions()) {
if (!tConnections.containsKey(option.getId())) {
tConnections.put(option.getId(), new ArrayList<ConnectionInfo>());
}
tConnections.get(option.getId()).add(new ConnectionInfo(id++, 0, option.getIpAddress(), option.getPort()));
}
for (Integer dc : tConnections.keySet()) {
connections.put(dc, tConnections.get(dc).toArray(new ConnectionInfo[0]));
}
}
@Override
public synchronized byte[] getAuthKey(int dcId) {
return keys.get(dcId);
}
@Override
public synchronized void putAuthKey(int dcId, byte[] key) {
keys.put(dcId, key);
}
@Override
public synchronized ConnectionInfo[] getAvailableConnections(int dcId) {
if (!connections.containsKey(dcId)) {
return new ConnectionInfo[0];
}
return connections.get(dcId);
}
@Override
public synchronized AbsMTProtoState getMtProtoState(final int dcId) {
return new AbsMTProtoState() {
private KnownSalt[] knownSalts = new KnownSalt[0];
@Override
public byte[] getAuthKey() {
return MemoryApiState.this.getAuthKey(dcId);
}
@Override
public ConnectionInfo[] getAvailableConnections() {
return MemoryApiState.this.getAvailableConnections(dcId);
}
@Override
public KnownSalt[] readKnownSalts() {
return knownSalts;
}
@Override
protected void writeKnownSalts(KnownSalt[] salts) {
knownSalts = salts;
}
};
}
@Override
public synchronized void resetAuth() {
isAuth.clear();
}
@Override
public synchronized void reset() {
isAuth.clear();
keys.clear();
}
public void saveObject()
{
try
{
FileOutputStream fileOut =
new FileOutputStream("apistate3.tmp");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(this);
out.close();
fileOut.close();
System.out.printf("Serialized data is saved");
}catch(IOException i)
{
i.printStackTrace();
}
}
public MemoryApiState readObject()
{
try
{
FileInputStream fileIn = new FileInputStream("apistate3.tmp");
ObjectInputStream in = new ObjectInputStream(fileIn);
MemoryApiState obj = (MemoryApiState) in.readObject();
in.close();
fileIn.close();
return obj;
}catch(IOException i)
{
i.printStackTrace();
return null;
}catch(ClassNotFoundException c)
{
System.out.println("Employee class not found");
c.printStackTrace();
return null;
}
}
}
+3
source to share
1 answer
hi: you can save the state in a file with any extension like this:
private void SaveState(String fileName, MemoryApiState mas)
{
try
{
FileOutputStream fileOut = new FileOutputStream(fileName + ".sta");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(mas);
out.close();
fileOut.close();
}
catch (IOException i)
{
i.printStackTrace();
}
}
and load the saved state from file like this:
private MemoryApiState LoadState(String fileName)
{
try
{
FileInputStream fileIn = new FileInputStream(fileName + ".sta");
ObjectInputStream in = new ObjectInputStream(fileIn);
MemoryApiState mas = (MemoryApiState)in.readObject();
in.close();
fileIn.close();
return mas;
}
catch (IOException i)
{
return null;
}
catch (ClassNotFoundException c)
{
c.printStackTrace();
}
return null;
}
+1
source to share