Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to run custom code at bot startup? #123

Open
aus70 opened this issue May 24, 2023 · 5 comments
Open

How to run custom code at bot startup? #123

aus70 opened this issue May 24, 2023 · 5 comments

Comments

@aus70
Copy link
Contributor

aus70 commented May 24, 2023

I'd like to run set_my_command, set_my_description, set_my_name and some other initialization code at bot startup. What's the best way to do it?
Thanks!

@rockneurotiko
Copy link
Owner

Currently there are not an easy way of dealing with this, but I'll add an optional init/1 callback on the bot so this inital setup is easy

@aus70
Copy link
Contributor Author

aus70 commented May 24, 2023

that'd be great, thanks!

@manuel-rubio
Copy link

The problem I found with init/1 is that it's running from the Supervisor init, and that means the Dispatcher is still not running. Even more, it's a mess if we want to use Phoenix.PubSub for receiving information inside of the Dispatcher because it only could be done by performing a request to the Dispatcher, not when the dispatcher is up and running.

I suggest init/1 callback for Bot be moved to Dispatcher when the connection is up and working, or that we could define any hooks/triggers/callbacks for when the connection is up and running (handle_up/1) and previously to that (init/1 but inside of the Dispatcher).

@rockneurotiko
Copy link
Owner

@manuel-rubio What's the use case for needing the Dispatcher started in the init/1 callback?

If you are trying to link a bot with Phoenix.PubSub in order to receive broadcast messages, I don't think subscribing from the dispatcher is a good thing. I would definitely go with my own Supervisor, with the Bot and a GenServer to handle the subscription, then the GenServer can send messages to the bot manually (Either with BotModule.message(<origin>, <message>) or with GenServer.call(<:bot_name>, <message>)

@manuel-rubio
Copy link

I did it in that way, but it's weird to have a GenServer only for sending messages to another process.

defmodule MyProject.Bot.Proxy do
  use GenServer

  def init([]) do
    Phoenix.PubSub.subscribe(MyProject.PubSub, "topic1")
    {:ok, nil}
  end

  def handle_info(info, nil) do
    send(MyProject.Bot.name(), info)
    {:ok, nil}
  end
end

It's not providing value at all and it's needed only because I cannot run startup code inside of the Dispatcher process.

My problem with that is I put in the init/1 the PubSub subscriptions and suddenly we have messages from the supervisor saying it receives an unexpected message, it sounded not very intuitive for me that init/1 was running in the supervisor process and handle/2 is running in the Dispatcher process, based on GenServer I was expecting both running on the same process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants