How to send a chat transcript history via email af...
# 🤝help
b
I have created a sub workflow for sending chat transcript in email but the captured variable values are empty after timeout but they have values in Main workflow.
a
Hi @broad-teacher-42540 , great question. There are two things to do in Botpress: 1. you need to save the transcript somewhere. Assuming it's just text, you can create a before outgoing message hook (top left) and adding this :
Copy code
if (!event.state.session.fullHistory) {
    event.state.session.fullHistory = ''
}
event.state.session.fullHistory = event.state.session.fullHistory + `user : ${event.payload.text}\nassistant : ${outgoingEvent.payload.text}` + '\n'
That will save the transcript to event.state.session.fullHistory 2. Then, in Explorer / Timeout, add an Execute Code card. You'll need to call an email sending service
Copy code
`
const SENDGRID_API_KEY = bot.sendgridApiKey // you have to set this up in sendGrid
const from_email = bot.sendEmailsFrom
const to_email = workflow.userEmail

const response = await axios.post(
  'https://api.sendgrid.com/v3/mail/send',
  {
    personalizations: [
      {
        to: [
          {
            email: to_email
          }
        ]
      }
    ],
    from: {
      email: from_email
    },
    subject: 'Your conversation timed out. Here's the transcript',
    content: [
      {
        type: 'text/plain',
        value: event.state.session.fullHistory
      }
    ]
  },
  {
    headers: {
      Authorization: 'Bearer ' + SENDGRID_API_KEY,
      'Content-Type': 'application/json'
    }
  }
)
That's it. You'll have to ask for the user's email at the beginning and save it to userEmail (although you can change the variable names, so long as you adjust the code), configure the email in Sendgrid, and add it, and the sendgrid api key to the bot variables (in settings).
Let me know if that works for you!
b
Thanks for the details. Let me try this
Where do I add fullHistory string property to sessionstate?
@acceptable-gold-88171
a
the hook (step 1 above) is what saves it to the variable. It's a bit of a hack 😅 but it should work. You don't need to declare it anywhere when doing it like this.
b
Got it
h
broad-teacher-42540 Is this code working?
103 Views