Closed dundas closed 6 years ago
Copied from example
const { MTProto } = require('telegram-mtproto');
const { Storage } = require('mtproto-storage-fs');
const app = { storage: new Storage('./storage.json') }
const phone = {
num : '+9996620001',
code: '22222'
}
const api = {
layer : 57,
initConnection : 0x69796de9,
api_id : 49631
}
const server = {
dev: true
}
const client = MTProto({ server, api, app })
async function login(client, phone){
const { phone_code_hash } = await client('auth.sendCode', {
phone_number : phone.num,
current_number: false,
api_id : 49631,
api_hash : 'fb050b8f6771e15bfda5df2409931569'
})
const sigin = await client('auth.signIn', {
phone_number : phone.num,
phone_code_hash: phone_code_hash,
phone_code : phone.code
})
console.log('sigin ', sigin);
}
(async function() {
if (!(await app.storage.get('signedin'))) {
login(client, phone).catch(console.error);
app.storage.set('signedin', true)
}
})()
const l = console.log
const tstmsg = 'Test' + Math.random();
(async function () {
await client('messages.sendMessage', {
peer: { '_': 'inputPeerSelf' },
message: tstmsg,
random_id: Math.random() * 10e7 >> 0
}).then(l).catch(l);
await client('messages.getHistory', {
peer: { '_': 'inputPeerSelf' },
limit: 3
}).then(x => x.messages[0])
.then(x => l(tstmsg + ' == ' + x.message, tstmsg == x.message)
.catch(l)
})()
I've tried working with this I can't get it to receive messages. Have you been able to get this to work?
Yes, this sends random number and receives it and then compares them.
Just npm install telegram-mtproto@latest; node f.js
, where f.js
is file with this code
I think this is a better way to get random ID for request:
const { nextRandomInt } = require('telegram-mtproto/lib/bin');
client('messages.sendMessage', {
peer,
message,
random_id: [nextRandomInt(0xFFFFFFFF), nextRandomInt(0xFFFFFFFF)],
});
Still struggling with this library. Would anyone be willing to help me get this set up or share code for log in, get phone code, and send message end to end?
I'd send you a $25 Amazon or Starbucks card for your assistance :), or Paypal if you prefer. Let me know thanks!