Code Monkey home page Code Monkey logo

Comments (11)

MajMcCloud avatar MajMcCloud commented on June 18, 2024 1

@palashjhabak

I will close this issue now. For any other questions, open up a new one or come into our telegram group: https://t.me/tgbotbase

from telegrambotframework.

Xilo14 avatar Xilo14 commented on June 18, 2024

Hi,

First of all thanks for an excellent framework.

I wanted to know if I want to send user a Form proactively without user asking for it, how do I do? All I have is User's device/chat Id stored in my database from a previous conversation and do not have any open session with user currently.

Did I understand the question correctly? Do you want to send a message to a person who didn't activate your bot, but you have that person's id? This is not possible. Here is a link to the documentation https://core.telegram.org/bots#4-how-are-bots-different-from-humans.
There may be a chance to get around this if you use the telegram api instead of the telegram bot api. But I'm not sure

from telegrambotframework.

palashjhabak avatar palashjhabak commented on June 18, 2024

@Xilosof No not that, user has interacted with my bot previously and then I have their Id, so thats sorted. Scenario is to send Forms without user asking for it, for example lets say I am building a flight price tracking service and want to give user a form with buttons to directly book flight when price falls below a certain price, then how do I do it? In this scenario, there is no ActiveForm for this user.

from telegrambotframework.

MajMcCloud avatar MajMcCloud commented on June 18, 2024

@palashjhabak @Xilosof

Thanks for your request.

So there are actually multiple ways to archive something like this with the bot and the current framework "as is". I will write it down later with another comment.

Cheers

PS: maybe try our telegram group (https://t.me/tgbotbase), would be receiving faster response next time 😀

from telegrambotframework.

palashjhabak avatar palashjhabak commented on June 18, 2024

I tried to achieve it by using InvokeMessageLoop which you very recently introcuded in the framework , but the main challenge I am facing is the switching of forms context, which is messing things up.

from telegrambotframework.

MajMcCloud avatar MajMcCloud commented on June 18, 2024

Sure, give me some time to refactor my thoughts and then I'm send you some concepts. Maybe today, lately tomorrow.

from telegrambotframework.

MajMcCloud avatar MajMcCloud commented on June 18, 2024

@palashjhabak

here we go, I hade two ways actually in mind to achive what you want to have (or thinking of):
I believe the second one is what shows your current situation

Something prior:
Please keep in mind that "forms" does not really exist in any telegram bot framework or the telegram api itself. What I have done is created a session mechanism which will help you with serialization and deserialitaion to "simulate" this kind of technology.
So it is highly recommended to use a SessionSerializer.

  1. Your are using already Session serialization:

This brings a lot of benefits, why?

When a user interacts with your bot the framework creates a session and a context around this "situation".
In default it will only stay as long as the bot runs. To iimprove here, you have to take a look into the readme at "session serialization" and choose a serializer or write one on your own, when you want to save this stuff inside a database for example.

Then you can, when ever you want, sent a message to the user via the "normal" telegram API with some buttons.
This do not must happen from the bot itself, it is important that it goes to the user.

In the framework you got an "UnhandledCall" Event which will get invoked on an inline message button click, when any of the current forms has not changed the EventArgs property handled to true . You can work with this. Cause inline buttons works with the "value" field not just only the text one (like replykeyboard ones), you can choose a value like "pc_" (for price change) and then some internal id (in total should be up to 64 bytes).

i.e. pc_12345

Then within that "UnhandledCall" event you can, catch it with the string operation "StartsWith" and move the user to the form you want to. Add the ID 12345 internally (could be a synonym as well) to that form and you are free to go.

What will happend:

  • price change happens
  • send the message with "price change"
  • user will some times click the inline keyboard button
  • your bot get the unhandled call event
  • with the device id and the ID from the button value field you know what "price" has changed
  • move the user over and do the "business stuff"
  1. You are not using any session serialization:

Most of the stuff is quite similiar to above, With small changes.

When your not "saving" sessions (which is really easy to do), you have to start the session for the user. (Which only works when the user has contacted your bot in the first place, as you already know)

As you stated correctly, there is no ActiveForm yet, so you have to create one.

You can add a null check here if a session already exists. If not, create one via:

var session = await < BotBase Instance>.Sessions.StartSession<A default Form, maybe an empty FormBase one>(DeviceID);

and then you got an ActiveForm in that "session" variable for this device.

Create your business form, add the properties from the "UnhandledCall" button value as well and then.

go as always: session.ActiveForm.NavigateTo(...)

The rest should become clear on trying out.

Have fun and good luck!.
Floh

PS: InvokeMessageLoop only works when your User session is in memory cache or your using the session serialization.
PS2: You can use that "GetSession" first to detect, if there is a session already running (in memory)

from telegrambotframework.

palashjhabak avatar palashjhabak commented on June 18, 2024

hey @MajMcCloud First of all thanks for taking out the time to reply let alone a so detailed reply.

Although I had implemented it using a solution that was somewhat similar to your second suggested solution, but I do agree with you that saving the session is very easy and is advantageous in implementing the kind of scenario that I want. I will definitely change my code to save sessions. Thanks a lot for writing such a detailed solution.

Now my followup question is lets say I send multiple forms to user very frequently, is it currently possible to switch the context to that particular form based on which form a user has interacted with assuming that the user can also interact with forms which are not the latest forms that were sent to them.

from telegrambotframework.

MajMcCloud avatar MajMcCloud commented on June 18, 2024

It sounds like your mixing multiple things into one question. Not sure.

So first, when sending a message to a user which is not a form (like a notification) inside any context, you will get ever that UnhandledCall event raised. It just depends on all other forms build up.

i.e. when sending 3 different messages (where every one lead to a different form at the end), the system doesn't care when the user clicks the button. It will work in any direction or order as you want it to.

Keep in mind, if you have multiple notifications (messages) for the same user/price it would make sense to:

  1. Edit the existing message (you just need to keep the messageId)
  2. delete the existing message (you just need to keep the messageId)

Maybe more fluent, for UI purposes.

"is it currently possible to switch the context to that particular form based on which form a user has interacted":

Sure! In general it will go to the latest form in the Action event first, and then to the UnhandledCall event. As I said in my previous message.

Hopefully that helps, if not please clarify the question/questions.

PS: maybe take a look at the message loop inside the BotBase.cs class

from telegrambotframework.

MajMcCloud avatar MajMcCloud commented on June 18, 2024

@palashjhabak

Hey!

anything you need help with ? Or can I close this ticket?

from telegrambotframework.

palashjhabak avatar palashjhabak commented on June 18, 2024

@palashjhabak

Hey!

anything you need help with ? Or can I close this ticket?

Hey sorry missed your message. Thank a lot for the detailed explaination. :) I was able to solve my issues.

from telegrambotframework.

Related Issues (18)

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.