Exemplo n.º 1
0
telebot_error_e telebot_get_me(telebot_handler_t handle, telebot_user_t **me)
{
    telebot_hdata_t * _handle = (telebot_hdata_t *)handle;
    if (_handle == NULL)
        return TELEBOT_ERROR_NOT_SUPPORTED;

    if (me == NULL)
        return TELEBOT_ERROR_INVALID_PARAMETER;

    *me = (telebot_user_t*)malloc(sizeof(telebot_user_t));
    if (*me == NULL)
        return TELEBOT_ERROR_OUT_OF_MEMORY;

    telebot_error_e ret = telebot_core_get_me(_handle->core_h);
    if (ret != TELEBOT_ERROR_NONE) {
        tb_sfree(*me);
        return ret;
    }

    struct json_object *obj = telebot_parser_str_to_obj(_handle->core_h->resp_data);
    tb_sfree_zcnt(_handle->core_h->resp_data, _handle->core_h->resp_size);

    if (obj == NULL) {
        tb_sfree(*me);
        return TELEBOT_ERROR_OPERATION_FAILED;
    }

    struct json_object *ok;
    if (!json_object_object_get_ex(obj, "ok", &ok)) {
        json_object_put(obj);
        tb_sfree(*me);
        return TELEBOT_ERROR_OPERATION_FAILED;
    }

    if (!json_object_get_boolean(ok)) {
        json_object_put(obj);
        tb_sfree(*me);
        return TELEBOT_ERROR_OPERATION_FAILED;
    }

    struct json_object *result;
    if (!json_object_object_get_ex(obj, "result", &result)){
        json_object_put(obj);
        tb_sfree(*me);
        return TELEBOT_ERROR_OPERATION_FAILED;
    }

    ret = telebot_parser_get_user(result, *me);
    json_object_put(obj);

    if (ret != TELEBOT_ERROR_NONE) {
        tb_sfree(*me);
        return ret;
    }

    return TELEBOT_ERROR_NONE;
}
Exemplo n.º 2
0
telebot_error_e telebot_parser_get_message(struct json_object *obj,
        telebot_message_t *msg)
{
    if (obj == NULL)
        return TELEBOT_ERROR_INVALID_PARAMETER;

    if (msg == NULL)
        return TELEBOT_ERROR_INVALID_PARAMETER;
    memset(msg, 0, sizeof(telebot_message_t));

    struct json_object *message_id;
    if (!json_object_object_get_ex(obj, "message_id", &message_id)) {
        ERR("Failed to get <message_id> from message object");
        return TELEBOT_ERROR_OPERATION_FAILED;
    }
    msg->message_id = json_object_get_int(message_id);
    json_object_put(message_id);

    int ret;
    struct json_object *from;
    if (json_object_object_get_ex(obj, "from", &from)) {
        ret = telebot_parser_get_user(from , &(msg->from));
        if (ret != TELEBOT_ERROR_NONE)
            ERR("Failed to get <from user> from message object");
        json_object_put(from);
    }

    struct json_object *date;
    if (json_object_object_get_ex(obj, "date", &date)) {
        msg->date = json_object_get_int(date);
        json_object_put(date);
    }

    struct json_object *chat;
    if (json_object_object_get_ex(obj, "chat", &chat)) {
        ret = telebot_parser_get_chat(chat , &(msg->chat));
        if (ret != TELEBOT_ERROR_NONE)
            ERR("Failed to get <chat> from message object");
        json_object_put(chat);
    }

    struct json_object *forward_from;
    if (json_object_object_get_ex(obj, "forward_from", &forward_from)) {
        ret = telebot_parser_get_user(forward_from , &(msg->forward_from));
        if (ret != TELEBOT_ERROR_NONE)
            ERR("Failed to get <forward from> from message object");
        json_object_put(forward_from);
    }

    struct json_object *forward_date;
    if (json_object_object_get_ex(obj, "forward_date", &forward_date)) {
        msg->forward_date = json_object_get_int(forward_date);
        json_object_put(forward_date);
    }

/* FIXME: allocating memory with alloca(), it is really wrong. */
/*
    struct json_object *reply_to_message;
    if (json_object_object_get_ex(obj, "reply_to_message", &reply_to_message)) {
        telebot_message_t *reply = alloca(sizeof(telebot_message_t));
        ret = telebot_parser_get_message(reply_to_message , reply);
        if (ret != TELEBOT_ERROR_NONE)
            ERR("Failed to get <reply_to_message> from message object");
        msg->reply_to_message = reply;
    }
*/

    struct json_object *text;
    if (json_object_object_get_ex(obj, "text", &text)) {
        snprintf(msg->text, TELEBOT_MESSAGE_TEXT_SIZE, "%s",
                json_object_get_string(text));
        json_object_put(text);
    }

    struct json_object *audio;
    if (json_object_object_get_ex(obj, "audio", &audio)) {
        ret = telebot_parser_get_audio(audio , &(msg->audio));
        if (ret != TELEBOT_ERROR_NONE)
            ERR("Failed to get <audio> from message object");
        json_object_put(audio);
    }

    struct json_object *document;
    if (json_object_object_get_ex(obj, "document", &document)) {
        ret = telebot_parser_get_document(document , &(msg->document));
        if (ret != TELEBOT_ERROR_NONE)
            ERR("Failed to get <document> from message object");
        json_object_put(document);
    }

    struct json_object *photo;
    if (json_object_object_get_ex(obj, "photo", &photo)) {
        ret = telebot_parser_get_photos(photo , msg->photo,
                TELEBOT_MESSAGE_PHOTO_SIZE);
        if (ret != TELEBOT_ERROR_NONE)
            ERR("Failed to get <photo> from message object");
        json_object_put(photo);
    }

    struct json_object *video;
    if (json_object_object_get_ex(obj, "video", &video)) {
        ret = telebot_parser_get_video(video , &(msg->video));
        if (ret != TELEBOT_ERROR_NONE)
            ERR("Failed to get <video> from message object");
        json_object_put(video);
    }

    struct json_object *voice;
    if (json_object_object_get_ex(obj, "voice", &voice)) {
        ret = telebot_parser_get_voice(voice , &(msg->voice));
        if (ret != TELEBOT_ERROR_NONE)
            ERR("Failed to get <voice> from message object");
        json_object_put(voice);
    }

    struct json_object *caption;
    if (json_object_object_get_ex(obj, "caption", &caption)) {
        snprintf(msg->caption, TELEBOT_MESSAGE_CAPTION_SIZE, "%s",
                json_object_get_string(caption));
        json_object_put(caption);
    }

    struct json_object *contact;
    if (json_object_object_get_ex(obj, "contact", &contact)) {
        ret = telebot_parser_get_contact(contact , &(msg->contact));
        if (ret != TELEBOT_ERROR_NONE)
            ERR("Failed to get <contact> from message object");
        json_object_put(contact);
    }

    struct json_object *location;
    if (json_object_object_get_ex(obj, "location", &location)) {
        ret = telebot_parser_get_location(location , &(msg->location));
        if (ret != TELEBOT_ERROR_NONE)
            ERR("Failed to get <location> from message object");
        json_object_put(location);
    }

    struct json_object *ncp;
    if (json_object_object_get_ex(obj, "new_chat_participant", &ncp)) {
        ret = telebot_parser_get_user(ncp , &(msg->new_chat_participant));
        if (ret != TELEBOT_ERROR_NONE)
            ERR("Failed to get <new_chat_participant> from message object");
        json_object_put(ncp);
    }

    struct json_object *lcp;
    if (json_object_object_get_ex(obj, "left_chat_participant", &lcp)) {
        ret = telebot_parser_get_user(lcp , &(msg->left_chat_participant));
        if (ret != TELEBOT_ERROR_NONE)
            ERR("Failed to get <left_chat_participant> from message object");
        json_object_put(lcp);
    }

    struct json_object *nct;
    if (json_object_object_get_ex(obj, "new_chat_title", &nct)) {
        snprintf(msg->new_chat_title, TELEBOT_CHAT_TITLE_SIZE, "%s",
                json_object_get_string(nct));
        json_object_put(nct);
    }

    struct json_object *new_chat_photo;
    if (json_object_object_get_ex(obj, "new_chat_photo", &new_chat_photo)) {
        ret = telebot_parser_get_photos(new_chat_photo , msg->new_chat_photo,
                TELEBOT_MESSAGE_NEW_CHAT_PHOTO_SIZE);
        if (ret != TELEBOT_ERROR_NONE)
            ERR("Failed to get <left_chat_participant> from message object");
        json_object_put(new_chat_photo);
    }

    struct json_object *del_chat_photo;
    if (json_object_object_get_ex(obj, "delete_chat_photo", &del_chat_photo)) {
        msg->delete_chat_photo = json_object_get_boolean(del_chat_photo);
        json_object_put(del_chat_photo);
    }

    struct json_object *gcc;
    if (json_object_object_get_ex(obj, "group_chat_created", &gcc)) {
        msg->group_chat_created = json_object_get_boolean(gcc);
        json_object_put(gcc);
    }

    struct json_object *sgcc;
    if (json_object_object_get_ex(obj, "supergroup_chat_created", &sgcc)) {
        msg->supergroup_chat_created = json_object_get_boolean(sgcc);
        json_object_put(sgcc);
    }

    struct json_object *cacc;
    if (json_object_object_get_ex(obj, "channel_chat_created", &cacc)) {
        msg->channel_chat_created = json_object_get_boolean(cacc);
        json_object_put(cacc);
    }

    struct json_object *mtci;
    if (json_object_object_get_ex(obj, "migrate_to_chat_id", &mtci)) {
        msg->migrate_to_chat_id = json_object_get_int(mtci);
        json_object_put(mtci);
    }

    struct json_object *mftci;
    if (json_object_object_get_ex(obj, "migrate_from_chat_id", &mftci)) {
        msg->migrate_from_chat_id = json_object_get_int(mftci);
        json_object_put(mftci);
    }

    return TELEBOT_ERROR_NONE;
}