Skip to content

Commit

Permalink
Merge pull request #33 from lurory/fix/webhook
Browse files Browse the repository at this point in the history
Fix webhook
  • Loading branch information
fabianaferreira authored Oct 2, 2022
2 parents 094ba62 + 5804b8f commit a45be4c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
4 changes: 2 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'dotenv/config'
import * as bot from './src/bot.js'
import { tgBot } from './src/bot.js'
import web from './src/web.js'

process.title = 'caronaBot'
web(bot)
web(tgBot)
4 changes: 2 additions & 2 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ let tgBot: Bot
if (process.env.NODE_ENV === 'production') {
token = process.env.TOKEN as string
tgBot = new Bot(token)
tgBot.setWebHook(process.env.HEROKU_URL + token)
tgBot.setWebHook(`${process.env.APP_URL}/bot${token}`)
} else {
token = process.env.TOKEN_DEV as string
tgBot = new Bot(token, { polling: true })
Expand Down Expand Up @@ -215,4 +215,4 @@ const sendAdminMessageToGroup = async (user: Bot.User, params: Array<string>) =>
}
}

export default tgBot
export { tgBot, token }
8 changes: 6 additions & 2 deletions src/web.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import Bot from 'node-telegram-bot-api'
import express, { Request, Response } from 'express'
import { AddressInfo } from 'net'
import bodyParser from 'body-parser'

import { token } from './bot.js'

const port = process.env.PORT || '80'
const app = express()

Expand All @@ -22,8 +25,9 @@ function isAddressInfo(info: string | AddressInfo | null): info is AddressInfo {
return (info as AddressInfo).address !== undefined
}

export default (bot: any) => {
app.post('/' + bot.token, (req: Request, res: Response) => {
export default (bot: Bot) => {
app.post(`/bot${token}`, (req: Request, res: Response) => {
console.log('token: ', token)
bot.processUpdate(req.body)
res.sendStatus(200)
})
Expand Down

0 comments on commit a45be4c

Please sign in to comment.