Skip to content
This repository has been archived by the owner on Mar 19, 2022. It is now read-only.

Commit

Permalink
Added some samples
Browse files Browse the repository at this point in the history
  • Loading branch information
badoualy committed Sep 22, 2016
1 parent cbc5a1a commit c1e1ad8
Show file tree
Hide file tree
Showing 11 changed files with 542 additions and 182 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Expand Up @@ -169,4 +169,7 @@ gradle-app.setting

# Cache of project
.gradletasknamecache
version.properties
version.properties
src/main/resources/
src/test/
src/main/java/com/github/badoualy/telegram/sample/SampleSignIn.java
Expand Up @@ -133,5 +133,9 @@ interface TelegramClient : TelegramApi {

/** Convenience method to download a file synchronously */
@Throws(RpcErrorException::class, IOException::class)
fun downloadSync(inputLocation: InputFileLocation, size: Int, partSize: Int = 512 * 1024, outputStream: OutputStream)
fun downloadSync(inputLocation: InputFileLocation, size: Int, outputStream: OutputStream) = downloadSync(inputLocation, size, 512 * 1024, outputStream)

/** Convenience method to download a file synchronously */
@Throws(RpcErrorException::class, IOException::class)
fun downloadSync(inputLocation: InputFileLocation, size: Int, partSize: Int, outputStream: OutputStream)
}
2 changes: 1 addition & 1 deletion sample/.gitignore
Expand Up @@ -6,6 +6,6 @@ auth*.key
src/test/java/com/github/badoualy/telegram/C.java
*.save
*.jpg
Sample*.java
/src/test/**/Sample*.java
/src/main/resources/*
tmp
179 changes: 0 additions & 179 deletions sample/src/main/java/com/github/badoualy/telegram/KotlogramSample.java

This file was deleted.

118 changes: 118 additions & 0 deletions sample/src/main/java/com/github/badoualy/telegram/sample/C.java
@@ -0,0 +1,118 @@
package com.github.badoualy.telegram.sample;

import com.github.badoualy.telegram.api.TelegramApiStorage;
import com.github.badoualy.telegram.api.TelegramApp;
import com.github.badoualy.telegram.mtproto.auth.AuthKey;
import com.github.badoualy.telegram.mtproto.model.DataCenter;
import com.github.badoualy.telegram.mtproto.model.MTSession;

import org.apache.commons.io.FileUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

public final class C {

// Get them from Telegram's console
public static final int API_ID = 0;
public static final String API_HASH = "<YOUR_HASH_HERE>";

// What you want to appear in the "all sessions" screen
public static final String APP_VERSION = "AppVersion";
public static final String MODEL = "Model";
public static final String SYSTEM_VERSION = "SysVer";
public static final String LANG_CODE = "en";

public static TelegramApp application = new TelegramApp(API_ID, API_HASH, MODEL, SYSTEM_VERSION, APP_VERSION, LANG_CODE);

// Phone number used for tests
public static final String PHONE_NUMBER = "+00000000000"; // International format

public static final File ROOT_DIR = new File("sample" + File.separator);
public static final File AUTH_KEY_FILE = new File(ROOT_DIR, "auth.key");
public static final File NEAREST_DC_FILE = new File(ROOT_DIR, "dc.save");

public static class ApiStorage implements TelegramApiStorage {

@Override
public void saveAuthKey(@NotNull AuthKey authKey) {
try {
FileUtils.writeByteArrayToFile(AUTH_KEY_FILE, authKey.getKey());
} catch (IOException e) {
e.printStackTrace();
}
}

@Nullable
@Override
public AuthKey loadAuthKey() {
try {
return new AuthKey(FileUtils.readFileToByteArray(AUTH_KEY_FILE));
} catch (IOException e) {
if (!(e instanceof FileNotFoundException))
e.printStackTrace();
}

return null;
}

@Override
public void saveDc(@NotNull DataCenter dataCenter) {
try {
FileUtils.write(NEAREST_DC_FILE, dataCenter.toString());
} catch (IOException e) {
e.printStackTrace();
}
}

@Nullable
@Override
public DataCenter loadDc() {
try {
String[] infos = FileUtils.readFileToString(NEAREST_DC_FILE).split(":");
return new DataCenter(infos[0], Integer.parseInt(infos[1]));
} catch (IOException e) {
if (!(e instanceof FileNotFoundException))
e.printStackTrace();
}

return null;
}

@Override
public void deleteAuthKey() {
try {
FileUtils.forceDelete(AUTH_KEY_FILE);
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public void deleteDc() {
try {
FileUtils.forceDelete(NEAREST_DC_FILE);
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public void saveSession(@Nullable MTSession session) {

}

@Nullable
@Override
public MTSession loadSession() {
return null;
}
}

private C() {

}
}

0 comments on commit c1e1ad8

Please sign in to comment.