Skip to content

Commit

Permalink
fmt: .clang-tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
orzklv committed Aug 15, 2023
1 parent f9e535c commit 0f439e0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 30 deletions.
2 changes: 2 additions & 0 deletions experiment/telegram.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
@interface Telegram : NSObject

- (instancetype)init:(NSString *)token;

- (void)startPolling;

- (void)sendMessage:(NSString *)chatId text:(NSString *)text;

@end
63 changes: 33 additions & 30 deletions experiment/telegram.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
static NSString *const kTelegramApiBaseUrl = @"https://api.telegram.org/bot";

@implementation Telegram {
NSString *_botToken;
NSInteger _lastUpdateId;
NSURLSession *_session;
NSString *_botToken;
NSInteger _lastUpdateId;
NSURLSession *_session;
}

- (instancetype)init:(NSString *)token {
self = [super init];
if (self) {
_botToken = token;
_lastUpdateId = 0;
_session = [NSURLSession sharedSession];
}
return self;
self = [super init];
if (self) {
_botToken = token;
_lastUpdateId = 0;
_session = [NSURLSession sharedSession];
}
return self;
}

- (void)startPolling {
Expand All @@ -36,11 +36,12 @@ - (void)getUpdates {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSArray *updates = json[@"result"];
for (NSDictionary *update in updates) {
self->_lastUpdateId = [update[@"update_id"] integerValue];
self->_lastUpdateId = [update[@"update_id"] integerValue];
NSDictionary *message = update[@"message"];
NSString *chatID = [NSString stringWithFormat:@"%@", message[@"chat"][@"id"]];
NSString *text = message[@"text"];
[self sendMessage:chatID text:text];
[self sendMessage:chatID text:@"Hello from Objective-C!"];
}

[self getUpdates];
Expand All @@ -49,28 +50,30 @@ - (void)getUpdates {
}

- (void)sendMessage:(NSString *)chatId text:(NSString *)text {
NSString *urlString = [NSString stringWithFormat:@"%@%@/sendMessage", kTelegramApiBaseUrl, _botToken];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
NSString *urlString = [NSString stringWithFormat:@"%@%@/sendMessage", kTelegramApiBaseUrl, _botToken];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";

NSDictionary *params = @{@"chat_id": chatId, @"text": text};
NSData *bodyData = [NSJSONSerialization dataWithJSONObject:params options:0 error:nil];
request.HTTPBody = bodyData;
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSDictionary *params = @{@"chat_id": chatId, @"text": text};
NSData *bodyData = [NSJSONSerialization dataWithJSONObject:params options:0 error:nil];
request.HTTPBody = bodyData;
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

NSURLSessionDataTask *task = [_session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (error) {
NSLog(@"Error sending message: %@", error);
return;
}
NSURLSessionDataTask *task = [_session dataTaskWithRequest:request completionHandler:^(NSData *_Nullable data,
NSURLResponse *_Nullable response,
NSError *_Nullable error) {
if (error) {
NSLog(@"Error sending message: %@", error);
return;
}

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
if (![json[@"ok"] boolValue]) {
NSLog(@"Failed to send message: %@", json[@"description"]);
}
}];
[task resume];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
if (![json[@"ok"] boolValue]) {
NSLog(@"Failed to send message: %@", json[@"description"]);
}
}];
[task resume];
}

@end

0 comments on commit 0f439e0

Please sign in to comment.