TamTam Bot API Java Client
This is Java client for TamTam Bot API. It gives you full access to API in your Java code.
Library has been built based on TamTam Bot API Schema that is OpenAPI-compliant.
Full documentation of API can be found here.
Minimum required version of Java is 8.
To use TamTam Bot API you should obtain
ACCESS_TOKEN for each bot you create.
Talk to @PrimeBot. It will helps you to create your first bot.
To start using this client add it as Maven dependency:
<dependency> <groupId>chat.tamtam</groupId> <artifactId>tamtam-bot-api</artifactId> <version>0.5.0</version> </dependency>
The easiest way to create it is to call:
TamTamBotAPI api = TamTamBotAPI.create("%ACCESS_TOKEN%);
It will create client with default Jackson-based serializer and OkHttp-based HTTP client.
If you want to use your own implementation you can implement
TamTamSerializer and initialize
TamTamClient with it:
TamTamTransportClient transportClient = …; TamTamSerializer serializer = …; TamTamClient client = new TamTamClient("%ACCESS_TOKEN%", transportClient, serializer); TamTamBotAPI botAPI = new TamTamBotAPI(client);
TamTamBotAPI provides access to all methods supported by the API. All methods return
TamTamQuery object that can be executed synchronous or asynchronous.
String fileToken = …; Long userId = …; AttachmentRequest fileAttachment = new FileAttachmentRequest(new UploadedInfo(fileToken)); List<AttachmentRequest> attachments = Collections.singletonList(fileAttachment); NewMessageBody body = new NewMessageBody("hello world!", attachments, null); SendMessageQuery sendMessageQuery = botAPI.sendMessage(body).userId(userId); // Sync SendMessageResult result = sendMessageQuery.execute(); // Async Future<SendMessageResult> futureResult = sendMessageQuery.enqueue();
Your bot is able to attach some media content to messages. It could be image, video, audio or file.
To attach media to message you should follow two steps:
- Obtain URL to upload:
UploadEndpoint endpoint = botAPI.getUploadUrl(UploadType.VIDEO).execute(); String uploadUrl = endpoint.getUrl();
- Upload binary data to this url. You can manually execute HTTP-request to send binary data to this url, or use built-in
TamTamUploadAPI uploadAPI = new TamTamUploadAPI(client); UploadedInfo uploadedInfo = uploadAPI.uploadFile(uploadUrl, new File("%FILE_PATH%")).execute();
It may take a time for server to process you file (audio/video or binary file). While file is not processed you can't attach it and will get
AttachmentNotReadyException when calling
sendMessage method. Try again until you'll get successful result.
All methods can throw two type of exceptions:
ClientException: general exception type wrapping all kinds of IO/serialization exceptions.
APIException: exception you will get if API was used incorrectly. For example, some arguments are missing or access to requested resource is denied.
No logging is performed by default. This project uses SLF4J, so you should bring adapter for logging framework you prefer. For example, add slf4j-log4j12 as dependency:
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.25</version> </dependency>
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
This project is licensed under the Apache 2.0.