https://discord.gg/botpress logo
#🤝help
How to send a chat transcript history via email after time out?
# 🤝help
b

broad-teacher-42540

05/30/2023, 8:37 PM
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

acceptable-gold-88171

05/30/2023, 9:05 PM
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

broad-teacher-42540

05/30/2023, 9:09 PM
Thanks for the details. Let me try this
Where do I add fullHistory string property to sessionstate?
@acceptable-gold-88171
a

acceptable-gold-88171

05/30/2023, 9:42 PM
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

broad-teacher-42540

05/30/2023, 9:43 PM
Got it
h

happy-easter-86380

09/21/2023, 5:54 PM
broad-teacher-42540 Is this code working?
90 Views