Skip to content

Instantly share code, notes, and snippets.

@painor
Created January 2, 2020 16:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save painor/a466dcf937b03c9c7a09400fdda411c1 to your computer and use it in GitHub Desktop.
Save painor/a466dcf937b03c9c7a09400fdda411c1 to your computer and use it in GitHub Desktop.
Small login + send message script with gramJS
const readline = require("readline")
const { ResolveUsernameRequest } = require("telegram/gramjs/tl/functions/contacts")
const { SendMessageRequest } = require("telegram/gramjs/tl/functions/messages")
const { TelegramClient} = require('telegram/gramjs')
const apiId = 12345689
const apiHash = "123456789abcdfgh"
const { ConnectionTCPFull } = require('telegram/gramjs/network')
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
})
const client = new TelegramClient("session", apiId, apiHash, { connection: ConnectionTCPFull })
async function codeCallback() {
return new Promise((resolve => {
rl.question('Input the code you received : ', (answer) => {
resolve(answer)
})
}))
}
async function passwordCallback() {
return new Promise((resolve => {
rl.question('please enter your password :', (answer) => {
resolve(answer)
})
}))
}
async function phoneCallback() {
return new Promise((resolve => {
rl.question('Please enter your phone number : ', (answer) => {
resolve(answer)
})
}))
}
(async function f() {
await client.start({
phone: phoneCallback,
code: codeCallback,
password: passwordCallback,
})
const peer = await client.invoke(new ResolveUsernameRequest({
username: "username",
}))
console.log(peer)
await client.invoke(new SendMessageRequest({
peer: peer,
message: 'hello'
}))
console.log("all done")
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment