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

Upgrading to use new Marketing API contacts list #391

Open
justinkchen opened this issue Aug 5, 2019 · 10 comments · May be fixed by #476
Open

Upgrading to use new Marketing API contacts list #391

justinkchen opened this issue Aug 5, 2019 · 10 comments · May be fixed by #476
Labels
status: help wanted requesting help from the community type: community enhancement feature request not on Twilio's roadmap

Comments

@justinkchen
Copy link

Issue Summary

I'm wondering what the timeline is expected to be for the sendgrid-ruby gem to support the Marketing Campaign v2?

Steps to Reproduce

N/A

Technical details:

  • sendgrid-ruby Version: master (latest commit: [commit number])
  • Ruby Version: 2.5.3
@tannerhallman
Copy link

tannerhallman commented Feb 6, 2020

Any updates on this? There's no documentation on the new Marketing campaigns and I'm pulling my hair out. Bout to rage quit and rewrite app using Mailchimp. It's been 6 months...

Update: I did find this https://sendgrid.com/docs/API_Reference/api_v3.html contacts which doesn't appear to be documented very well. I still can't get much to work with it though.

Another Update: I finally, after guessing a lot, got the contacts uploading to a list using the new Marketing.

See my example for those poor souls after me:

sg = SendGrid::API.new(api_key: ENV["SENDGRID_API_KEY"])
users = [{first_name: 'Tanner', last_name: 'Hallman', email: '[email protected]}]
list_ids = ["2984y3u32-2334932-34344"] # some crazy long UUID of the list you're trying to send these contacts to (you can grab it from the URL bar on the dashboard) (not the old Integer IDs)
body = {list_ids: list_ids, contacts: users}
response = sg.client.marketing.contacts.put(request_body: body)
Rails.logger.debug response.status_code
Rails.logger.debug response.body
Rails.logger.debug response.headers

@ooochie
Copy link

ooochie commented Feb 6, 2020

@tannerhallman I, too, have been waiting for updates here. I ended up moving forward using this gem as-is, but writing an intermediate layer in my own app to access the new endpoints. The way this gem is written allows you to chain the methods together to access the endpoint you need, so you can just initialize your client and go from there, using the API docs you referenced under "NEW MARKETING CAMPAIGNS." Here's what I mean:

#initialize your client
@client = SendGrid::API.new(api_key: YOUR_API_KEY).client

#Get a single contact by its ID
#GET /marketing/contacts/{id}
@client.marketing.contacts.send(id).get

#delete a contact
#DELETE /marketing/contacts
@client.marketing.contacts.delete({query_params: {ids: id}})

#search for a contact
#POST /marketing/contacts/search
@client.marketing.contacts.search.post(request_body: {"query"=>"email = '#{email}'"})

#ETC

This API still leaves a lot to be desired and seems like it's not quite finished, but this is working for me in the interim.

@thinkingserious
Copy link
Contributor

Hello @justinkchen, @tannerhallman and @ooochie,

First, BIG THANKS to @tannerhallman and @ooochie for posting solutions!!

Secondly, to answer @justinkchen, pull requests to add this feature are welcome and will be reviewed based on priority, but Twilio SendGrid is not actively building new functionality for the library.

With best regards,

Elmer

@thinkingserious thinkingserious added help wanted status: help wanted requesting help from the community type: community enhancement feature request not on Twilio's roadmap labels Mar 19, 2020
@tannerhallman
Copy link

tannerhallman commented Mar 19, 2020

Wait so you are not going to be building support for the new marketing campaign into this library? Is SendGrid dropping support for Ruby?

@childish-sambino
Copy link
Contributor

@tannerhallman No, not dropping support for Ruby or this library. Just that building new functionality here is lower on the priority list at this time.

@kjohnston
Copy link

I'm looking to come back to SendGrid for the marketing contacts, lists, segments and automation as it's something I can't get from Mailgun, Postmark or MailChimp. It looks like the API documentation for these endpoints is coming together. I'm happy to see a Ruby gem and appreciate the company-supported, community-built, approach here - everyone has to prioritize. But, I have to voice that without some more company support you're forcing busy people to decide between contributing to this project vs. hand-rolling only what they need in isolation. Please consider allocating some more Twilio SendGrid resource to the project to reduce the barrier to (re-)entry.

@rickychilcott
Copy link

If someone comes along and wants to be able to upload custom fields using @tannerhallman's code, you'll need to do a bit more work:

$sg = SendGrid::API.new(api_key: ENV["SENDGRID_API_KEY"])

def sendgrid_custom_field_definitions
  @sendgrid_custom_field_definitions ||=
    $sg
      .marketing
      .field_definitions
      .get
      .parsed_body
      .fetch(:custom_fields)
end

def transform_custom_fields(contact_groups)
  contact_groups.map do |contact|
    transformed_contact = contact.dup

    # Convert from "custom_field_name" to sendgrid"s custom field "id"
    transformed_contact[:custom_fields] =
      transformed_contact[:custom_fields]
        .transform_keys do |key|
        field_definition =
          sendgrid_custom_field_definitions.find do |fd|
            fd[:name].to_s == key.to_s
          end

        field_definition[:id]
      end

    transformed_contact
  end
end

users_with_custom_fields = [{first_name: "Ricky", last_name: "Chilcott", email: "[email protected], custom_fields: {"age" => 30, "ice_cream_flavor" => "chocolate"}}]
list_ids = ["2984y3u32-2334932-34344"] # some crazy long UUID of the list you"re trying to send these contacts to (you can grab it from the URL bar on the dashboard) (not the old Integer IDs)

users_with_transformed_custom_fields = transform_custom_fields(users_with_custom_fields)
body = {list_ids: list_ids, contacts: users_with_transformed_custom_fields}
response = sg.client.marketing.contacts.put(request_body: body)

Rails.logger.debug response.status_code
Rails.logger.debug response.body
Rails.logger.debug response.headers

I've made it rely on SendGrid's field_definitions endpoint to lookup the appropriate mappings.

Hoping this helps someone else. Ideally, sendgrid-ruby would do this transformation for us -- ideally not even relying on us wrapping custom fields in their own sub-object.

@netwire88
Copy link

Any update on the documentation? This https://github.com/sendgrid/sendgrid-ruby/blob/main/USAGE.md#contactdb looks outdated. I get 403 access forbidden error.

@JohnLegrandRichards
Copy link

This seems to be just a documentation issue. Chaining the methods through the client is all there. It just seems to not be well documented in the example folders. I will take the time this week to add a new campaigns folder in the documentation as well as a nested contacts folder. I will then include examples so people stop getting confused.

@david-mears-2
Copy link

@JohnLegrandRichards I think you are right that this is just a documentation issue. Did you make any progress on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: help wanted requesting help from the community type: community enhancement feature request not on Twilio's roadmap
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants