Giter VIP home page Giter VIP logo

Comments (11)

esindger avatar esindger commented on July 2, 2024

You usually can obtain access_hash from the users field of some updates.

import { ag, api } from 'airgram'

airgram.updates.use((ctx: ag.UpdateContext<any>, next) => {
  if ('users' in ctx.update) {
    ctx.update.users.forEach((user: api.UserUnion) => {
       // save user.access_hash to somewhere
    })
  }
  return next()
});

from airgram.

volodalexey avatar volodalexey commented on July 2, 2024

Ok. Got it.
I get full updates event, that includes updateNewMessage.
Full updates event object:

{ _: 'updates',
  updates:
   [ { _: 'updateNewMessage',
       message: [Object],
       pts: 570,
       pts_count: 1 } ],
  users:
   [ { _: 'user',
       pFlags: {},
       flags: 4194411,
       id: 123456789,
       access_hash: '999999...999',
       first_name: '...',
       username: '...',
       photo: [Object],
       status: [Object],
       lang_code: 'ru' },
     { _: 'user',
       pFlags: [Object],
       flags: 17419,
       self: true,
       bot: true,
       id: 999999999,
       access_hash: '999999...999',
       first_name: '...',
       username: '...',
       bot_info_version: 1 } ],
  chats: [],
  date: 1544620388,
  seq: 0 }

from airgram.

volodalexey avatar volodalexey commented on July 2, 2024

@esindger If I do not want to use all updates middleware, but only updateNewMessage, in ctx.update I get only "nested update":

{ _: 'updateNewMessage',
       message: [Object],
       pts: 570,
       pts_count: 1 }

However full update event is:

{ _: 'updates',
  updates:
   [ { _: 'updateNewMessage',
       message: [Object],
       pts: 570,
       pts_count: 1 } ],
  users:
   [ { _: 'user', ... },
     { _: 'user', ... } ], }

Is it possible to get access to "full" update event inside updateNewMessage handler?
E.g. ctx.client.a.b.c.fullUpdateEvent

from airgram.

volodalexey avatar volodalexey commented on July 2, 2024

Nice to see fast support in this project! Thanks.

Actually, maybe I do not need "full" event object with users. What I need is "simple" reply() function.
Like in Telegraf reply().

  reply (...args) {
    this.assert(this.chat, 'reply')
    return this.telegram.sendMessage(this.chat.id, ...args)
  }

This function is used in a lot of examples:

bot.start((ctx) => ctx.reply('Welcome'))
bot.help((ctx) => ctx.reply('Send me a sticker'))
bot.on('sticker', (ctx) => ctx.reply('👍'))

The same I want to achieve with Airgram (however Airgram will be more powefull, so I can use it for bot and fo user). E.g. somewhere in updateNewMessage handler:

type OnlyOneMessageUpdateContext = /* here I want context that will contain only one message at time, so airgram will easily detect to which message it must reply */ UpdateContext;

airgram.updates.on(`updateNewMessage`, (ctx: OnlyOneMessageUpdateContext) => {
  if (update.message === '/start') {
    await ctx.reply('Hello!');
    await ctx.reply('Nice to meet you!'); // reply to the same message twice
  }
});
  1. Airgram should detect to whom airgram must reply (whether message was sent from chat or directly from user). I see that using bot API is much simpler in this case, just to use sendMessage and send chat or user id. With MTProto (Airgram) this will be harder task.
    Probably detect message type as
to_id: { _: 'peerChat', chat_id: ... } // then reply to chat
to_id: { _: 'peerUser', user_id: ... } // then reply to user
  1. Because of async handlers I should be confident that current context contains the same message (the same users array).

But anyway, if I will get access to users array in updateNewMessage I will try to implement reply() function on my own.

from airgram.

esindger avatar esindger commented on July 2, 2024

Is it possible to get access to "full" update event inside updateNewMessage handler?

Since v0.1.9 you have access to the parent update by ctx.parent property.

airgram.updates.on(`updateNewMessage`, (ctx: ag.UpdateContext) => {
  console.log(ctx.parent)
});

from airgram.

esindger avatar esindger commented on July 2, 2024

For now, there is no plan to implement reply() function but we can refactor code to put creating of context into separate method createContext. Then you will be able to inherit this method and extend context.

from airgram.

volodalexey avatar volodalexey commented on July 2, 2024

Since v0.1.9 you have access to the parent update by ctx.parent property.

Awesome! Thanks!

For now, there is no plan to implement reply() function but we can refactor code to put creating of context into separate method createContext. Then you will be able to inherit this method and extend context.

Got it. If you will make it - it would be nice.

from airgram.

volodalexey avatar volodalexey commented on July 2, 2024

@esindger

airgram.updates.on(`updateNewMessage`, (ctx: ag.UpdateContext) => {
  console.log(ctx.parent)
});

In typescript I get compile error:

[ts] Property 'parent' does not exist on type 'UpdateContext<UpdatesResponse>'. [2339]

Am I missing something?

from airgram.

esindger avatar esindger commented on July 2, 2024

In typescript I get compile error:
[ts] Property 'parent' does not exist on type 'UpdateContext'. [2339]
Am I missing something?

Please check out package.lock which version is installed. This will work start from version 0.1.9.

from airgram.

esindger avatar esindger commented on July 2, 2024

Now you can extend updates context. Please check out the example

from airgram.

volodalexey avatar volodalexey commented on July 2, 2024

Please check out package.lock which version is installed. This will work start from version 0.1.9.

Yep. With version 0.1.9 removed package-lock.json and run npm i - helped. Thanks!

Now you can extend updates context. Please check out the example

Awesome! Thanks!

from airgram.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.