1
+ import {
2
+ DynamoDBClient ,
3
+ GetItemCommand ,
4
+ PutItemCommand ,
5
+ } from "@aws-sdk/client-dynamodb" ;
6
+ import { unmarshall } from "@aws-sdk/util-dynamodb" ;
1
7
import axios from "axios" ;
2
8
import { getFromEnv } from "../lib/cdk-stack" ;
3
9
10
+ const ddb = new DynamoDBClient ( { } ) ;
11
+
4
12
export const handler = async ( event : any ) => {
5
13
const body = JSON . parse ( event . body ) as Body ;
6
14
const chatId = body . message . chat . id ;
7
15
const text = body . message . text ;
16
+ const user = body . message . chat . username ;
8
17
console . log ( text ) ;
9
18
10
19
const addressRegex = new RegExp ( / ^ .* ( 0 x [ a - z A - Z 0 - 9 ] { 64 } $ ) / g) ;
@@ -14,7 +23,7 @@ export const handler = async (event: any) => {
14
23
await sendErrorMessagetoUser ( chatId ) ;
15
24
} else {
16
25
await sendConfirmationMessagetoUser ( chatId , match [ 1 ] ) ;
17
- await addAddresstoDb ( match [ 1 ] ) ;
26
+ await addAddresstoDb ( chatId , match [ 1 ] , user ) ;
18
27
19
28
const addressToWatch = match [ 1 ] ;
20
29
console . log ( addressToWatch ) ;
@@ -28,16 +37,7 @@ export const handler = async (event: any) => {
28
37
} ;
29
38
} ;
30
39
31
- interface Body {
32
- message : {
33
- chat : {
34
- id : string ;
35
- } ;
36
- text : string ;
37
- } ;
38
- }
39
-
40
- async function sendErrorMessagetoUser ( chatId : string ) {
40
+ async function sendErrorMessagetoUser ( chatId : number ) {
41
41
const errorMessage =
42
42
"Address not found. Please include your Starknet address following the command /watch" ;
43
43
const BOT_API_KEY = getFromEnv ( "BOT_API_KEY" ) ;
@@ -46,7 +46,7 @@ async function sendErrorMessagetoUser(chatId: string) {
46
46
console . log ( res ) ;
47
47
}
48
48
49
- async function sendConfirmationMessagetoUser ( chatId : string , match : string ) {
49
+ async function sendConfirmationMessagetoUser ( chatId : number , match : string ) {
50
50
const addressWatched = match ;
51
51
const confirmationMessage = `Watching address ${ addressWatched } ...` ;
52
52
const BOT_API_KEY = getFromEnv ( "BOT_API_KEY" ) ;
@@ -55,7 +55,28 @@ async function sendConfirmationMessagetoUser(chatId: string, match: string) {
55
55
console . log ( res ) ;
56
56
}
57
57
58
- async function addAddresstoDb ( match : string ) {
59
- const addressWatched = match ;
60
- return ;
58
+ async function addAddresstoDb ( chatId : number , addressWatched : string , username : string ) {
59
+
60
+ const params = {
61
+ TableName : "CdkStack-AccountsToWatch0A702DCE-Z6XSJDP9YT94" ,
62
+ Item : {
63
+ accountAddress : { S : addressWatched } ,
64
+ chatId : { N : chatId . toString ( ) } ,
65
+ username : { S : username } ,
66
+ } ,
67
+ } ;
68
+
69
+ const res = await ddb . send ( new PutItemCommand ( params ) ) ;
70
+
71
+ return null ;
72
+ }
73
+
74
+ interface Body {
75
+ message : {
76
+ chat : {
77
+ id : number ;
78
+ username : string ;
79
+ } ;
80
+ text : string ;
81
+ } ;
61
82
}
0 commit comments