enricostara / telegram.link

Telegram API library (javascript, node.js)
www.telegram.link
MIT License
334 stars 59 forks source link

send a message to a phone number... #7

Closed kiaksarg closed 8 years ago

kiaksarg commented 9 years ago

Hi .. First of all i have to say thank you for telegram.link Actually i have some problem with telegram.link and now i can't send a message from it through telegram(the only thing that i need for now).As a matter of fact i'm a Asp.net MVC programmer and Node.Js is not my expert but when i saw your project on Github i started to learn Node.Js so i'm just beginner in Nod.Js and i'm really confused how to send a message from telegram.link by the way 1- i declare var telegramLink = require('telegram.link')(); and create client (client.isReady()return true)

2-then i call client.createAuthKey()and it return a key(when i have to use AuthKey? )

3-but client.auth.sendCode()doesn't send any cod for my mobile number('+98939...5241')

pleas help me that how can i send a message to a phone number with this

thanx for every thing and sorry for that my English is not well(-_-).....

Codes.... app.get("/createAuthKey" , function (req, resp, next) { console.log("Get_createAuthKey") ; client.createAuthKey(function(e){ console.log(e); console.log('createauthkey') fs.appendFile(process.cwd() + "\createauthkey.txt" , "\n--------------------- \n "+ e, function(err){ if (err) {console.log(err) ; } else { console.log("append createauthkey! :D ") ; } })

})
resp.end("createAuthKey");

}) ; ////////////////////////////////////////

app.get("/SendCode" , function (req, resp, next) { console.log("Get_sendCode") ; client.auth.sendCode('+98939....5241',0,'en',function(e){ fs.appendFile(process.cwd() + "\sendCode.txt" , "\n------------------------ \n "+ e , function(err){ if (err) {console.log(err) ; } else { console.log("append sendCode! :D ") ; } })

})
resp.end("SendCode");

}) ; ///////////////////////////////////////////////////////////////////////

app.get("/SignIn" , function (req, resp, next) { console.log("Get_sendCode") ; client.auth.signIn('+98939...5241',req.body['phone-code-hash'],req.body['phone-code'],function(e){ fs.appendFile(process.cwd() + "\SignIn.txt" , "\n------------------------ \n "+e , function(err){ if (err) {console.log(err) ; } else { console.log("append SignIn! :D ") ; } })

})
resp.end("SignIn");

}) ;

//////////////////////////////////////////////////////////////////// app.get("/SendMessage" , function (req, resp, next) { console.log("Get_SendMessage") ;

var peer = new telegramLink.type.InputPeerContact({
    props: {
        user_id: 98939...5241
    }
});

client.messages.sendMessage(
    peer,
    'My UTF8 first msg!!',
    3571334511  // random id...
).then(function (sentMsg) {
        console.log('sentMsg:', sentMsg.toPrintable());
        fs.appendFile(process.cwd() + "\\sentMsg.txt" , "\n------------------------ \n "+sentMsg , function(err){
            if (err) {console.log(err) ; }
            else {
                console.log("append sentMsg! :D ") ;
            }
        })
    });
resp.end("SendMessage");

}) ; ////////////////////////////////////////

longstone commented 9 years ago

have you tried without leading '+' ? see https://core.telegram.org/method/auth.sendCode (auth.sendCode "79991234567" 1 32 "test-hash" "en")

kiaksarg commented 9 years ago

thanks to longstone.Yes but at the first when i wanted start i said to my self that telegram have a api like another websites and i can call theme with ajax, server side and etc simply but when i saw their documentation i didn't understand how can i call or use their api in .Net Framework and actually the only thing that i need is "send a message to a phone number" then i saw telegram.link and i started to learn node.js but.....by the way is there any tips or path that help me how can i send a message to a number in telegram.link(Node.Js) or .Net through Telegram (of course i prefer C#) thnx...

kiaksarg commented 9 years ago

hi... Finally i could send a message(with .termgram(https://github.com/enricostara/termgram) but there is a problem with get a GroupChat ) but with user_id not a phone Number actually now i am looking for a method to get a user by phone Number but i can't finde something like this...all of these Constructors:

inputPeerEmpty inputPeerSelf inputPeerContact inputPeerForeign inputPeerChat

want user_id as parameters ... please help that how can i get user_id from Phone number in international format...thanx

mory91 commented 9 years ago

Hello and salam :)))).... Can i ask you how did you manage to send messages to user ids by this api?

kiaksarg commented 9 years ago

Drod and hi (-_-) actually i use .termgram(https://github.com/enricostara/termgram) that it is written by the great enricostara to and i think it's an implementation from telegram.link.work with this is easy but there is some points.when you enter(login) to program after sign up it calls home() so as you know you can use this code for send message(but of course it is not a optimized code just for instance)

var mypeer = new telegramLink.type.InputPeerContact({
    props: {
        user_id: user_id
    }
});

    clientProxy.getClient().messages.sendMessage(
        mypeer,
        'message',       // or a buffer but you have to use a UTF-8 encoding   fs.readFileSync(process.cwd() + "\\msg.txt");
 Math.floor(Math.random()*(max-min+1)+min)// random id...
    ).then(function (sentMsg) {
            console.log('sentMsg:', sentMsg.toPrintable());
        });

but for User_id the only way that i found is working with contacts 1-getContacts() 2-importContacts()

1-getContacts()

clientProxy.getClient().contacts.getContacts("",function(container){

    for(var i=0;i<=container.users.list.length-1;i++){

        //console.log(container['users'].list[i].phone);
        fs.appendFile(process.cwd() + "\\Contacts.txt" , "\n ############# \n "+ i.toString()+"---"+container['users'].list[i].id +"--------"+container['users'].list[i].phone+"--------"+container['users'].list[i].first_name+"--------"+container['users'].list[i].last_name, function(err){
            if (err) {console.log(err) ; }
            else {
                console.log("append Users! :D ") ;
            }
        })

    }

})

2-importContacts(contacts Vector , replac Bool) Parameters contacts Vector List of contacts to import replace Bool If (boolTrue) is transmitted, the version existing on the server will be completely overwritten when the contact list is saved. Otherwise, new entries will be added to the list.

in this case we have to know about type Language(tl)(telegram-tl-node) why? how we can make a list of Contacts and pass this or type bool for this reason i'll give you some examples:

console.log('start')

var api=require('telegram.link')()('../api'); var tl = require('telegram-tl-node');

try{ ////////////////////build InputContact type //see api-tlschema.json in telegram.link var InputContactType=new tl.TypeBuilder.buildType('api', { "id": "-208488460", "predicate": "inputPhoneContact", "params": [ { "name": "client_id", "type": "long" }, { "name": "phone", "type": "string" }, { "name": "first_name", "type": "string" }, { "name": "last_name", "type": "string" } ], "type": "InputContact" }); var contact=new InputContactType({ props: { client_id:10..20648,phone:"+989...5446",first_name:"Arash",last_name:"G"}, }) ////////////////////build Bool type True

var booltype=      new tl.TypeBuilder.buildType('api', {
    "id": "-1720552011",
    "predicate": "boolTrue",
    "params": [],
    "type": "Bool"
});

var mybool=new booltype({
    prop:[]
});

///////build Vector var mycntacts= new tl.TypeVector({type: 'InputContact', list:[contact]});// list:[contact,contact2,...]

clientProxy.getClient().contacts.importContacts(mycntacts,mybool,function(container){

    console.log(container['users'])

})

} catch(e){ console.log(e) }

that's it... but i still have a problem with send media and file such as photo ....for instance i don't know how to make a md5_checksum and pass this or Divide the files if there is someone that works with this please help me.....

let's don't forget these are just a sample that are not optimized.and it is what that i understood from this so maybe..... but all of these codes have been working for me.

fazo96 commented 9 years ago

@kiaksar I suggest you learn the basics about what a checksum is and what MD5 is, then you can try using nodejs' crypto built-in library to generate the hash you need!

Also, if you want to get a user by phone number you need to add it to your contacts, but I'm not sure it's possible right now with Telegram.Link. However, you can add it using Telegram Web then you'll find it in your contacts when using telegram.link!

If you need other examples, you can take a look at the client I wrote

kasra-st commented 5 years ago

hi guys and salam ; EN : I have the same problem too I need to import a list of contacts to my telegram by using telegram web (javascript ) but I can't do this :( is there anyone who can tell me how to do this ? because I'm new to javascript

FA : agha manam hamin moshkelo daram man mikham ye list contact ro tooye telegramam import konam ba telegram web vali nmitoonam harchi say mikonam chon taze javascript yad gereftam kasi mitoone rahnamaiim kone ?