GochoMugo / tgfancy

A Fancy, Higher-Level Wrapper for Telegram Bot API
MIT License
184 stars 16 forks source link

Example about getting the user_id from the username #16

Closed abbddo closed 6 years ago

abbddo commented 7 years ago

I want an example about extracting the user_id from the username this was my try

const Tgfancy = require("tgfancy");
const token = '258118704:AAGx1-***********************';
const bot = new Tgfancy(token, {
    tgfancy: {
        option: "value",
              chatIdResolution: {
                    resolve(token, chatId, callback) {

                      return callback(null, user);
                    }
                }
    },
    polling: true
});

bot.tgfancy.chatIdResolution.resolve(token, "@cc_ccx", (err, user)=>{
    if (err) {console.log(err); return;}

            console.log(user);

});

but I'm getting error says user is not defined

abbddo commented 7 years ago

I've solved it by requiring tgresolve const tgresolve = require("tg-resolve"); then use it like this

tgresolve(token, "@tgeek", function(error, result) {
    // ... handle error ...
    console.log(result.id);
});
GochoMugo commented 7 years ago

The chatIdResolution.resolve constructor option you were using allows providing a custom resolution method. Currently, we do not expose any method for resolving username but we could work on a feature to expose such a method. Maybe something like,

bot.resolveChatId("@username").then(function(result) {
    // ... do something ...
});
GochoMugo commented 6 years ago

Tgfancy#resolveChatId() added in v0.12.0