rockneurotiko/ex_gram

Constructing a new poll

Closed this issue ยท 6 comments

What methods are available for making new poll ?

I can see

    model(Poll, [
      {:id, :string},
      {:question, :string},
      {:options, {:array, PollOption}},
      {:total_voter_count, :integer},
      {:is_closed, :boolean},
      {:is_anonymous, :boolean},
      {:type, :string},
      {:allows_multiple_answers, :boolean},
      {:correct_option_id, :integer, :optional},
      {:explanation, :string, :optional},
      {:explanation_entities, {:array, MessageEntity}, :optional},
      {:open_period, :integer, :optional},
      {:close_date, :integer, :optional}
    ])

implemented. What's the corresponding function?

๐Ÿ‘‹๐Ÿผ

To create a poll you have to use the sendPoll method:

method(
    :post,
    "sendPoll",
    [
      {chat_id, [:integer, :string]},
      {question, [:string]},
      {options, [{:array, :string}]},
      {is_anonymous, [:boolean], :optional},
      {type, [:string], :optional},
      {allows_multiple_answers, [:boolean], :optional},
      {correct_option_id, [:integer], :optional},
      {explanation, [:string], :optional},
      {explanation_parse_mode, [:string], :optional},
      {explanation_entities, [{:array, MessageEntity}], :optional},
      {open_period, [:integer], :optional},
      {close_date, [:integer], :optional},
      {is_closed, [:boolean], :optional},
      {disable_notification, [:boolean], :optional},
      {reply_to_message_id, [:integer], :optional},
      {allow_sending_without_reply, [:boolean], :optional},
      {reply_markup, [InlineKeyboardMarkup, ReplyKeyboardMarkup, ReplyKeyboardRemove, ForceReply],
       :optional}
    ],
    ExGram.Model.Message
  )

Something like:

ExGram.send_poll(chat_id, "Question?", ["Option1", "Option2"])

You can find the method's help in the iex repl:

iex(8)> h ExGram.send_poll

            def send_poll(chat_id, question, options, options \\ [])            

  @spec send_poll(
          chat_id :: integer() | String.t(),
          question :: String.t(),
          options :: [String.t()],
          options :: [
            is_anonymous: boolean(),
            type: String.t(),
            allows_multiple_answers: boolean(),
            correct_option_id: integer(),
            explanation: String.t(),
            explanation_parse_mode: String.t(),
            explanation_entities: [ExGram.Model.MessageEntity.t()],
            open_period: integer(),
            close_date: integer(),
            is_closed: boolean(),
            disable_notification: boolean(),
            reply_to_message_id: integer(),
            allow_sending_without_reply: boolean(),
            reply_markup:
              ExGram.Model.InlineKeyboardMarkup.t()
              | ExGram.Model.ReplyKeyboardMarkup.t()
              | ExGram.Model.ReplyKeyboardRemove.t()
              | ExGram.Model.ForceReply.t(),
            adapter: atom(),
            bot: atom(),
            token: String.t(),
            debug: boolean(),
            check_params: boolean()
          ]
        ) :: {:ok, ExGram.Model.Message.t()} | {:error, ExGram.Error.t()}

Check the documentation of this method in
https://core.telegram.org/bots/api#sendpoll

Thanks!

One more question :
I see h ExGram.send_poll in iex -S mix
but cannot find its implementation method(:post, ..... in the directory. (used Ctrl+F)

These are all functions I see.
image

Am I missing something?

You can ask all the questions you want, no problem.

In order to avoid repetition, this library uses scraping + macros to create the basic models and functionality.

In fact, this line creates the method send_poll with the parameters needed: https://github.com/rockneurotiko/ex_gram/blob/master/lib/ex_gram.ex#L401

EDIT: Currently there are 77 methods, maybe you are not finding it because of the formatter the line has a line break

Line Break it was!

I just skimmed through the .py file you use to scrape - Appreciate this approach to avoid repetition !

Thank you so much!

I usually specify them as a keyword list, I think you can use a map too, but as a keyword list is cleaner:

ExGram.send_poll(chat_id, "Question?", ["Option1", "Option2"], type: "quiz", correct_option_id: 0) # You need to set a correct option for quizes

This helps! Thanks.