本文介绍了电报API:如何保持ApiState保存登录状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从以下来源使用了电报API: https://github.com/voleon/telegram-trivia-bot 但是我的问题是,如何保持用户登录.因为在停止应用程序之后,需要用户租用者手机并从SMS消息中获取激活码.我已经实现了Serializable用于保存ApiState对象.但是这种方法不能解决我的问题.这是我的ApiState的代码:

I used telegram api from this source :https://github.com/voleon/telegram-trivia-botBut my problem is , how keep the user signed in.Because after the App stop need user renter Mobile phone and get activation code from SMS message.I have implemented Serializable for saving ApiState object. but this method not solved 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;
        }
    }

}

推荐答案

hi:您可以将状态保存在具有任何扩展名的文件中,如下所示:

hi : you can save state in file with any extension as follow:

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 than load saved state from file as follow :

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;
 }

这篇关于电报API:如何保持ApiState保存登录状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 08:12