rockneurotiko/ex_gram

writing send_poll Protocol - help needed.

Opened this issue · 4 comments

Hello,

Objective:

To make send_poll pipe-able I am trying to make following changes to library.

so I want to use it like
context |> answer('anyhting') |> send_poll(id,"question",['1','2'],type:"quiz",correct_option_id: 0)

what I did?

  1. add send_poll.ex protocol implementation responses folder
  2. adding SendPoll as import alias in dsl.ex
  3. adding send_poll function in dsl.ex

Where I need help?

  1. Do I need to touch any other file?
  2. in answer Here -- What does add_answer do?

I'll create a PR once I get a response from you and sort things out!

Hello!

That's all you need to do to add a new response, indeed.

The DSL functions in dsl.ex (answer, answer_callback, ...) do two things. First creates the response structure with the correct options, and then adds the answer to the answers list in the context (using add_answer)

For example, afwer the pipe of your issue, you would end with a context with two items in the answers list, one Answer and one SendPoll (Something like %Context{answers: [%Answer{...}, %SendPoll{}]})

When you return the context at the end of your handle methods, the library will try to send all the answers (But you can do it manually with send_answers/1 at any time if you prefer)

You can see here where it happens in the dispatcher. https://github.com/rockneurotiko/ex_gram/blob/master/lib/ex_gram/dispatcher.ex#L255

Right!

I am trying to build middlewares for my specific needs. And %Context{answers: [%Answer{...}, %SendPoll{}]} is an aim.
I'll submit PR then in few hours!

Side Question

  1. Is is possible to implement Protocols in Responses folder for all methods via .py file?
    I'll be happy to chime in if you have a lead on that.

Feel free to submit any PR, I'll try to review it as soon as possible to give feedback and approve it when everything is right 😄

About generating the Responses, I don't think I would like that. I don't want all the methods to have a response, only the ones useful to have a balance between simple and useful client API. Also, some Responses really need custom code (check ExGram.Responses.EditInline), so it would be complicated to auto-generate the code.

https://github.com/rockneurotiko/ex_gram/blob/master/lib/ex_gram/dsl.ex#L24

What corner case does this method in answer cover? I felt that above two definitions were enough.

  1. Why check m as map?
  2. I see that it does include a case with NO options. (and hence passes empty list to function below.)

I might need to implement similar in send_poll function.