vkdart 1.1.1
vkdart: ^1.1.1 copied to clipboard
Package helps simplify working with the VK API. Completely wraps VK methods, has event support and much more.
VkDart #
Package helps to make working with the VK API easier.
Chat discussion of the package - https://t.me/vk_dart
Navigation: #
Features #
- Supports all methods. Has interfaces of all public VK methods.
- Reliable. Functionality of the package is wrapped in Unit tests
- Event support It is possible to catch VK events, support for user and group events
- Developing. Functionality is becoming more!
Get Started #
Install #
First, let's install package.
The simplest use: #
import 'package:vkdart/vkdart.dart';
void main() async {
final accessToken = ''; // Group Token, User Token.
final vk = VkDart(token: accessToken);
final api = vk.getApi();
final user = await api.users.get({'user_id': 'durov'});
final name = user.data[0]['first_name'];
// surname
final surname = user.data[0]['last_name'];
print('User details: Name = $name, Surname = $surname');
}
Using API #
Creating an API Instance #
To use VK Api methods, you need to create an instance of the Api
class, there are two ways:
vk.getApi();
// and
Api(token: /* */, language: /* LangApi */, version: /* */);
The function getApi()
creates an instance with default parameters.
default lang
is LangApi.ru**, version
is 5.131.
Calling methods #
For requests to API methods, you can use two methods, the request()
(it is advisable use if there no method in interfaces)
api.request('name_method', /* Map<String, Object> parameters */);
Example:
api.request('users.get', {'user_id': 'durov'}); // instance Future<ApiResponse<dynamic>>
or already existing request interfaces.
api.<selection>.<name>(/* parameters */);
Example:
api.users.get({'user_id': 'durov'}); // instance Future<ApiResponse<dynamic>>
Request response #
When calling methods, response is wrapped in an instance of the ApiResponse
class, which has a response and requestOptions field.
final user = await api.users.get({'user_id': 'durov'});
print('Data: ${user.data}'); // type user.data = dynamic
print('Request Options: ${user.requestOptions}'); // access token remove!
Example above, output data type is dynamic, order to specify the response type, specify it in the generic function.
api.users.get<List<dynamic>>(/* params */);
// or
api.request<List<dynamic>>('name method', /* ... */);
Receiving Events #
Using Longpoll API #
Elementary example of use:
final longpoll = Longpoll(api, groupId: 1234);
longpoll.subscribe((event) {
/* event is Map<String, dynamic> and List<dynamic> */
if (event['type'] == 'message_new') {
print('new message!');
}
});
longpoll.start();
More detailed example
Longpoll
supports UserLongpoll, GroupLongpoll, the example above shows the second type of event receiving.
To receive user events, do not specify the groupId
parameter.
Longpoll(api, /* not groupId! */);
Note: In this case, the event
type will be List
,
Note: If you use a group token but do not specify the groupId
parameter, the event
type will be List
Using Callback API #
Elementary example of use:
final callback = Callback(vk.getApi());
callback.subscribe((event) {
if (event['type'] == 'message_new') {
print('new message!');
}
});
callback.start();
More detailed example
By default, the server runs on localhost, on port 80, if the certificate 443 is specified.
Install certificate:
callback.start(port: /* ... */, securityContext: SecurityContext(/* ... */));
Documentation for SecurityContext
Future plans #
In the future I plan to implement the VK API
for convenient use:
- Create contexts for received events
- Wrap received events in middleware.
- Implement the VK Api keyboard interface
- Create a command constructor to simplify the creation of chatbots.
Bugs and feature requests #
Please send a bug report to issue tracker