Skip to content
This repository has been archived by the owner on Apr 8, 2020. It is now read-only.

Commit

Permalink
file upload or file_id
Browse files Browse the repository at this point in the history
documentation core.telegram.org/bots/api#sending-files
  • Loading branch information
h4child committed Mar 30, 2018
1 parent fbde54b commit 8f074bc
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 31 deletions.
2 changes: 1 addition & 1 deletion include/framebot/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

#include <framebot/framebot.h>

int api_tg_exist(const char *filename);
char * file_extension_download(char *tgname, char *namefile);
char * vsboolean_param_parser (char *base, int psize, ...);
char * api_ltoa(long int n);
char * api_itoa(int n);
int api_atoi(char * str_int);
Expand Down
4 changes: 2 additions & 2 deletions src/framebot.c
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ int set_chat_photo(Bot *bot, char * chat_id, char *filename){

input = call_method_upload(bot->token, ifile);
if(!input)
return NULL;
return 0;

s_json = start_json(input->content);
mem_store_free(input);
Expand Down Expand Up @@ -1051,7 +1051,7 @@ Message * send_document(Bot * bot, char * chat_id, char * filename, char * capti
refjson *s_json;

input = call_method_upload(bot->token, ifile);
if(!inpu t)
if(!input)
return NULL;

s_json = start_json(input->content);
Expand Down
38 changes: 30 additions & 8 deletions src/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,11 @@ MemStore * call_method_upload(const char * token, IFile ifile){
if(ifile.photo.filename != NULL){
field = curl_mime_addpart(form);
curl_mime_name(field, "photo");
curl_mime_filedata(field, ifile.photo.filename);
if(api_tg_exist(ifile.photo.filename))
curl_mime_filedata(field, ifile.photo.filename);
else
curl_mime_data(field, ifile.photo.filename, CURL_ZERO_TERMINATED);

}

/* Photo caption */
Expand Down Expand Up @@ -206,7 +210,10 @@ MemStore * call_method_upload(const char * token, IFile ifile){
if(ifile.audio.filename != NULL){
field = curl_mime_addpart(form);
curl_mime_name(field, "audio");
curl_mime_filedata(field, ifile.audio.filename);
if(api_tg_exist(ifile.audio.filename))
curl_mime_filedata(field, ifile.audio.filename);
else
curl_mime_data(field, ifile.audio.filename, CURL_ZERO_TERMINATED);
}

/* Audio caption */
Expand Down Expand Up @@ -271,7 +278,10 @@ MemStore * call_method_upload(const char * token, IFile ifile){
if(ifile.document.filename != NULL){
field = curl_mime_addpart(form);
curl_mime_name(field, "document");
curl_mime_filedata(field, ifile.document.filename);
if(api_tg_exist(ifile.document.filename))
curl_mime_filedata(field, ifile.document.filename);
else
curl_mime_data(field, ifile.document.filename, CURL_ZERO_TERMINATED);
}

/* Document caption */
Expand Down Expand Up @@ -315,7 +325,10 @@ MemStore * call_method_upload(const char * token, IFile ifile){
if(ifile.video.filename != NULL){
field = curl_mime_addpart(form);
curl_mime_name(field, "video");
curl_mime_filedata(field, ifile.video.filename);
if(api_tg_exist(ifile.video.filename))
curl_mime_filedata(field, ifile.video.filename);
else
curl_mime_data(field, ifile.video.filename, CURL_ZERO_TERMINATED);
}

/* Document caption */
Expand Down Expand Up @@ -380,7 +393,10 @@ MemStore * call_method_upload(const char * token, IFile ifile){
if(ifile.voice.filename != NULL){
field = curl_mime_addpart(form);
curl_mime_name(field, "voice");
curl_mime_filedata(field, ifile.voice.filename);
if(api_tg_exist(ifile.voice.filename))
curl_mime_filedata(field, ifile.voice.filename);
else
curl_mime_data(field, ifile.voice.filename, CURL_ZERO_TERMINATED);
}

/* voice caption */
Expand Down Expand Up @@ -431,7 +447,10 @@ MemStore * call_method_upload(const char * token, IFile ifile){
if(ifile.videonote.filename != NULL){
field = curl_mime_addpart(form);
curl_mime_name(field, "video_note");
curl_mime_filedata(field, ifile.videonote.filename);
if(api_tg_exist(ifile.videonote.filename))
curl_mime_filedata(field, ifile.videonote.filename);
else
curl_mime_data(field, ifile.videonote.filename, CURL_ZERO_TERMINATED);
}

/* Duration of the voice in seconds */
Expand Down Expand Up @@ -472,7 +491,7 @@ MemStore * call_method_upload(const char * token, IFile ifile){
strcpy(method, "setChatPhoto");

/* Fill in the file upload field */
if(ifile.videonote.chat_id != NULL){
if(ifile.chatphoto.chat_id != NULL){
field = curl_mime_addpart(form);
curl_mime_name(field, "chat_id");
curl_mime_data(field, ifile.chatphoto.chat_id, CURL_ZERO_TERMINATED);
Expand All @@ -482,7 +501,10 @@ MemStore * call_method_upload(const char * token, IFile ifile){
if(ifile.videonote.filename != NULL){
field = curl_mime_addpart(form);
curl_mime_name(field, "video_note");
curl_mime_filedata(field, ifile.chatphoto.filename);
if(api_tg_exist(ifile.chatphoto.filename))
curl_mime_filedata(field, ifile.chatphoto.filename);
else
curl_mime_data(field, ifile.chatphoto.filename, CURL_ZERO_TERMINATED);
}
break;

Expand Down
27 changes: 7 additions & 20 deletions src/util.c
Original file line number Diff line number Diff line change
@@ -1,28 +1,15 @@
#include <framebot/util.h>

// format = (char *param_alias), (bool param), (char *param_alias), (bool param), ...
char *vsboolean_param_parser (char *base, int psize, ...) {
va_list params;
va_start(params, psize);

int i;
int api_tg_exist(const char *filename){
FILE *b;

char *alias, *tmp, *base_tmp = base;
bool param;
b = fopen(filename, "r");

for (i = 0; i < psize; i++) {
alias = va_arg(params, char*);
param = va_arg(params, bool);

if (param) {
tmp = format("&%s=True", alias);
} else {
tmp = format("&%s=False", alias);
}

base_tmp = realloc_string(base_tmp, tmp);
free(tmp);
if(b){
fclose(b);
return 1;
}

return base_tmp;
return 0;
}

0 comments on commit 8f074bc

Please sign in to comment.