visciang / telegram

Telegram library for the Elixir language
MIT License
204 stars 27 forks source link

feature: proactively start a `ChatBot` #162

Closed samm81 closed 11 months ago

samm81 commented 11 months ago

hello again :)

opening an issue to discuss a feature proposal. I've already found a workaround, but I thought to start a discussion about this use case & a more kosher api for it.

I have a bot which I would like to start proactively - for a user (me) where the id is known ahead of time. this bot is a ChatBot and keeps state, messaging me every so often with a question which I respond to. so I want it to message me even if I haven't messaged it since the last restart.

after doing some digging my workaround is to just simulate sending it a message during application start:

bots = [{MyBot, [token: @token, max_bot_concurrency: 1]}]

children = [
  {Telegram.Poller, bots: bots},
  {Task,
   fn ->
     MyBot.dispatch_update(
       [{:init, %{"chat" => %{"id" => @chat_id_me}, action: :init}}],
       @token
     )
   end}
]

is this use case valid enough that maybe ChatBot should support it in a non-hacky manner?

I'm not exactly sure what the api would look like if so. maybe some function like ChatBot.start(chatbot_behavior, chat_id) (ChatBot.start(MyBot, @chat_id_me) in the example above), which would call Chat.Session.Supervisor.start_child/2 ?

hope this made sense!