@@ -35,42 +35,19 @@ Want to try it yourself? Just define a method like processMessage in your agent,
3535Want to build an AI agent that can receive and respond to emails automatically ? With the new email adaptor and onEmail lifecycle method , now you can .
3636
3737` ` ` ts
38- export class EmailAgent extends Agent<Env, EmailAgentState> {
39- initialState = {
40- emailCount: 0,
41- emails: [],
42- autoReplyEnabled: true,
43- lastUpdated: new Date(),
44- };
45-
38+ export class EmailAgent extends Agent {
4639 async onEmail(email: AgentEmail) {
4740 const raw = await email.getRaw();
4841 const parsed = await PostalMime.parse(raw);
4942
50- this.setState({
51- ...this.state,
52- emailCount: this.state.emailCount + 1,
53- emails: [
54- ...this.state.emails.slice(-9),
55- {
56- from: parsed.from?.address ?? email.from,
57- subject: parsed.subject ?? "No Subject",
58- text: parsed.text,
59- html: parsed.html,
60- to: email.to,
61- timestamp: new Date(),
62- messageId: parsed.messageId,
63- },
64- ],
65- lastUpdated: new Date(),
43+ // create a response based on the email contents
44+ // and then send a reply
45+
46+ await this.replyToEmail(email, {
47+ fromName: "Email Agent",
48+ body: ` Thanks for your email ! You ' ve sent us "${parsed.subject}". We' ll process it shortly .` ,
6649 });
6750
68- if (this.state.autoReplyEnabled && !this.isAutoReply(parsed)) {
69- await this.replyToEmail(email, {
70- fromName: "Email Agent",
71- body: ` Thanks for your email ! You ' ve sent us "${parsed.subject}". We' ll process it shortly .` ,
72- });
73- }
7451 }
7552}
7653` ` `
0 commit comments